F - x = a^b

原题链接

题解

假如 \(b=2\),则有 \(\sqrt[2]{n}\) 个这样的数

假如 \(b=3\),则有 \(\sqrt[3]{n}\) 个这样的数

所以总共有 \(\sum_{k=2}^{log_2(n)}\sqrt{k}\) 个这样的数

考虑去重

由于 \(\sqrt[3]{n}\leq 1e6\),所以 \(a\) 不会超过 \(1e6\),所以我们可以统计该范围内的所有 \(a^k,k\gt 2\) 的数,然后去除那些完全平方数

细节:

\(sqrt\) 精度会炸,要用 \(sqrtl\)

code

#include<bits/stdc++.h>
using namespace std;

#define int long long


void solve() {
    int n;
    cin >> n;

    int ans =sqrtl(n);

    unordered_set<int> mp;
    for (int a = 2; a <= 1e6; a++)
    {
        int tem = a * a * a;
        while (tem <= n)
        {
            int sq = sqrtl(tem);
            if(sq*sq!=tem) mp.insert(tem);
            if (tem > n / a) break;  // 防止溢出
            tem *= a;
        }
    }
    ans += mp.size();
    cout << ans << '\n';
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int TT = 1;
    // cin >> TT;
    while (TT--) solve();
    return 0;
}



posted @ 2024-08-08 16:57  纯粹的  阅读(13)  评论(0)    收藏  举报