【CF1737B】B. Ela's Fitness and the Luxury Number(思维,数学)
【CF1737B】B. Ela's Fitness and the Luxury Number(思维,数学)
很简单的思维题,不过有一点要注意的是:给long long范围的数开根号时,要先把那个数转为long double,不然会有精度损失造成计算错误。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
ll l, r;
cin >> l >> r;
auto cal = [&](ll x) {
if (x == 0) return 0LL;
ll s = sqrt((long double)x);
ll ans = (s - 1) * 3;
for (ll i = s * s; i <= x; i += s) {
ans++;
}
return ans;
};
cout << cal(r) - cal(l - 1) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}

浙公网安备 33010602011771号