给出一个32位整型数 n,形如 n = x^p 的最大的 p;
质因数分解;
TLE,DISCUSS 中有的用枚举做的(要考虑浮点数精度问题),但是质因数分解应该也可以啊,为什么老是TLE?求大牛指点。
# include <stdio.h> # include <math.h> int gcd(int a, int b){return !b ? a:gcd(b, a%b);} int m; char t[60005]; int p[10005]; void solve(long long int n); void build_ptable(void); int main() { long long int n; build_ptable(); while (~scanf("%lld", &n) && n) solve(n); return 0; } void build_ptable(void) { int i, j; m = 0; for (i = 2; i <= 60000; ++i) { if (!t[i]) { p[++m] = i; for (j = i*2; j <= 60000; j += i) t[j] = 1; } } } void solve(long long int n) { int f, i, c, k, tmp; f = 0; for (i = 1; abs(n)>=p[i] && i<=m; ++i) { c = 0; while (n%p[i] == 0) { n /= p[i]; ++c; } if (!f) { tmp = c; f = 1; } k = gcd(tmp, c); tmp = k; } if (n<0) while (!(k&0x1)) k /= 2; printf("%d\n", k); }