打表求助 回关注
查看原帖
打表求助 回关注
1021623
AVLw楼主2024/4/10 20:53

csdn看的。 int s = (R + 1) / 2; int e = static_cast(sqrt(L)) + 1; 这里s e为什么要取这些值怎么来的没看懂 打表完找规律

#include <iostream>
#include <algorithm> 
#include<map>
#include<cmath> 
#include<vector>
typedef long long LL;
using namespace std;

int getNums1(int L, int R) {
    
    int s = (R + 1) / 2;
    int e = static_cast<int>(sqrt(L)) + 1;
    int ans = 0;
    vector<bool> have(R - L + 1, false);
    for (int i = s; i >= e; i--) {
        for (int j = i - 1; j >= 0; j--) {
            int a = (i + j) * (i - j);
            if (a >= L && a <= R && !have[a - L]) {
                ans++;
                have[a - L] = true;
            } else if (a > R) {
                break;
            }
        }
    }
    return ans;
}

int main() {
	
	int l,r;
	cin>>l>>r;
	cout<<getNums1(l,r)<<'\n';


 	
    return 0;
}


2024/4/10 20:53
加载中...