80pts尽力了
查看原帖
80pts尽力了
1397544
ycyzc14楼主2025/7/23 10:14

先试了试暴力法,结果只有30pts


#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
signed main() {
    int n;
    cin >> n;
    while(n--) {
        int x;
        cin >> x;
        if(x == 0) {
            cout << 0 << endl;
            continue;
        }
        int ans = 1;
        for(int i = cbrt(x); i >= 1; --i) {
            if(x % (i*i*i) == 0) {
                ans = i;
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

又分解立方因子优化了一下算法,上了60pts


#include<bits/stdc++.h>
using namespace std;
long long largest(long long x) {
	if (x == 0) return 0;
	long long ans = 1;
	for (long long i = 2; i * i * i <= x; ++i) {
		while (x % (i * i * i) == 0) {
			ans *= i;
			x /= (i * i * i);
		}
	}
	long long cbrt_x = cbrt(x);
	if (cbrt_x * cbrt_x * cbrt_x == x) {
		ans *= cbrt_x;
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	while (n--) {
		long long x;
		cin >> x;
		cout << largest(x) << endl;
	}
	return 0;
}

最后预处理指数,优化立方根查找,还是喜提TLE

#include<bits/stdc++.h>
using namespace std;
vector<long long> P;
void Z(long long L) {
    vector<bool> I(L+1, true);
    for(long long i=2; i*i<=L; ++i) {
        if(I[i]) {
            for(long long j=i*i; j<=L; j+=i) {
                I[j] = false;
            }
        }
    }
    for(long long i=2; i<=L; ++i) {
        if(I[i]) P.push_back(i);
    }
}
long long l(long long x) {
    if(x == 0) return 0;
    long long ans = 1;
    for(long long p : P) {
        if(p*p*p > x) break;
        while(x % (p*p*p) == 0) {
            ans *= p;
            x /= (p*p*p);
        }
    }
    long long c = cbrt(x);
    if(c * c * c == x) {
        ans *= c;
    }
    return ans;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    Z(1e6); 
    int n;
    cin >> n;
    while(n--) {
        long long x;
        cin >> x;
        cout << l(x) << endl;
    }
    return 0;
}

2025/7/23 10:14
加载中...