1 条题解

  • 0
    @ 2023-3-10 10:08:55
    #include <bits/stdc++.h>
    #define int long long
    using namespace std;
    
    int n, k, ans;
    vector<int> a;
    
    signed main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n >> k;
        if (k == 1)
        {
            cout << n << "\n";
            return 0;
        }
        if (k == 2)
            ans = sqrt((long double)n); // x^2
        else
            ans = 1; // 1^3
        // 枚举出满足条件的数
        for (int x = 2; x <= 1000000; x++)
        {
            // 生成初始的 x^k
            int now = 1;
            for (int i = 1; i <= max(3LL, k); i++)
            {
                if (now <= n / x)
                    now *= x;
                else
                {
                    now = n + 1;
                    break;
                }
            }
            if (now > n)
                break;
            // 枚举 now: x^(>=k)
            while (now <= n)
            {
                // 检查 now 是否是完全平方数, k 为 2 时要去掉平方数
                if (k == 2)
                {
                    int nownow = sqrt((long double)now);
                    if (nownow * nownow != now)
                        a.push_back(now);
                }
                else
                    a.push_back(now);
                if (now <= n / x)
                    now *= x;
                else
                    break;
            }
        }
        // cout << a.size() << "\n"; // (1e18,2): 1003479; (1e18,3): 1037552;
        sort(a.begin(), a.end());
        for (int i = 0; i < a.size(); i++)
        {
            if (i > 0 && a[i] == a[i - 1])
                continue;
            ans++;
        }
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    1235
    时间
    1000ms
    内存
    1024MiB
    难度
    8
    标签
    递交数
    82
    已通过
    13
    上传者