【CF1737B】B. Ela's Fitness and the Luxury Number(思维,数学)

【CF1737B】B. Ela's Fitness and the Luxury Number(思维,数学)

题目链接:Problem - B - Codeforces

很简单的思维题,不过有一点要注意的是:给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;
}
posted @ 2022-10-30 19:32  blockche  阅读(85)  评论(0)    收藏  举报