立方数

题解

依稀记得这是假期的某一场比赛,那一场的这个题没有过。鸽了好久~~
%%% 9fdalao
题解很简单,先预处理素数筛,
对于一个区间 [1,1e18/4],先进行质因子分解,
剩下的数字的因子,质因子都必定为(1e18/4,1e18],并且因子都相同,
二分枚举即可

代码

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e6+55;
bool vis[maxn];
int prime[maxn];
int pcnt = 0;
void init(int n) {
    for (int i = 2; i <= n; ++ i) {
        if (!vis[i]) {
            prime[pcnt++] = i;
        }
        for (int j = 0; j < pcnt && 1ll * prime[j] * i <= n; ++ j) {
            vis[prime[j]*i] = true;
            if (i % prime[j] == 0) break;
        }
    }
}
int main() {
    init(100000);
    int t;
    cin >> t;
    while (t --) {
        ll n, m = 1;
        cin >> n;
        for (int i = 0; i < pcnt; ++ i) {
            int cnt = 0;
            while (n % prime[i] == 0) {
                n /= prime[i];
                cnt ++;
                if (cnt % 3 == 0) {
                    m *= prime[i];
                }
            }
        }
        ll ans = 1;
        ll l = 1, r = 1000000;
        while (l <= r) {
            ll mid = (l+r) >> 1;
            if (mid*mid*mid == n) {
                ans = mid;
                break;
            }
            if (mid*mid*mid > n) {
                r = mid - 1;
            }
            else l = mid + 1;
        }
        m = ans * m;
        cout << m << endl;
    }
    return 0;
}