题目描述
We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if, for some integer b, x = b 3. More generally, x is a perfect pth power if, for some integer b, x = b p. Given an integer x you are to determine the largest p such that x is a perfect p th power.
Input
Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.
Output
For each test case, output a line giving the largest integer p such that x is a perfect p th power.
Sample Input
17
1073741824
25
0
Sample Output
1
30
2
题目大意
给你一个绝对值大于等于2的整数n,问它能构成a^b=n中最大的b是多少?
解题报告
1:先套质因数分解,算出因子等信息。
2:我们知道一个大于2的正整数可以分解为n = (p1^a1)*(p2^a2)*(p3^a3).....其中p1, p2等为质因子。要使指数ax最大,只需要对所有指数求一遍gcd就行了。
3:负数先用正整数求出答案,由于负数的指数不可能为偶数。所有持续对ans除2,直到ans为奇数。
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
ll cnt[100], fac[100];
ll getFac(ll n){
ll len = 0, s = sqrt(n);
memset(cnt, 0, sizeof(cnt));
for(ll i=2; i<=s; ++i){
if(n%i == 0){
fac[len] = i;
while(n%i == 0)n /= i, cnt[len]++;
++len;
}
}
if(n != 1){
fac[len] = n, cnt[len]++, ++len;
}
return len;
}
int main(){
ll n;
while(~scanf("%lld", &n) && n){
ll ans = 0, flag = 1;
if(n < 0)flag = -1, n = -n;
ll len = getFac(n);
for(ll i=0; i<len; ++i){
ans = __gcd(cnt[i], ans);
}
if(flag == -1){
while(ans%2==0)ans >>= 1;
}
printf("%lld\n", ans);
}
return 0;
}