所以,x 所在的区间也就是 [max{0,y3−k},y3+k]。但是题目要求 a≤x2≤b,所以最终 x 的取值范围就是 [max{y3−k,a},min{y3+k,b}]。我们都知道 l∼r 之间的整数的个数为 r−l+1,那么我们就可以解出这道题了。
算法总时间复杂度 O(3b−a)。
CODE:
C++
#include <bits/stdc++.h>using namespace std;
#define int long longsigned main() {
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int a, b, k, ans = 0;
cin >> a >> b >> k;
int l = ceil(cbrt(a)), r = cbrt(b);
for (int y = l; y <= r; y++) {
ans += floor(sqrt(min(y * y * y + k, b))) - ceil(sqrt(max(y * y * y - k, a))) + 1;
}
cout << ans;
return 0;
}