Dr. Mob has just discovered a Deathly Bacteria. He named it RC-01. RC-01 has a very strange reproduction system. RC-01 lives exactly x days. Now RC-01 produces exactly p new deadly Bacteria where x = bp (where b, p are integers). More generally, x is a perfect pth power. Given the lifetime x of a mother RC-01 you are to determine the maximum number of new RC-01 which can be produced by the mother RC-01.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case starts with a line containing an integer x. You can assume that x will have magnitude at least 2 and be within the range of a 32 bit signed integer.
Output
For each case, print the case number and the largest integer p such that x is a perfect pth power.
Sample Input
3
17
1073741824
25
Sample Output
Case 1: 1
Case 2: 30
Case 3: 2
题意: 给x,让求 满足 x=b^p的p的最大值 (注意x可正可负数)
分析: 设
x=p1^x1 p2 ^x2 …. pk^xk
b=p1^b1 p2^b2…..pk^bk
由题意可得:
b1*p=x1
b2*p=x2
. .. . . .
bk*p=xk
然后我们可以知道 想要让式子成立,p | gcd(x1,x2,x3...xk) 。
所以 ,现在让求最大,如果x为正数,p=gcd(x1,x2,x3...xk)
否则 p 就是gcd(x1,x2,x3,,,xk) 的最大奇数因子。
代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 1e5+11;
const int M = 1e6+11;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3;
bool su[N+1]={1,1,0};int prm[N],sz=0;
void init(){
for(int i=2;i<N;i++){
if(!su[i]) prm[sz++]=i;
for(int j=0;j<sz;j++){
LL t=prm[j]*i;
if(t>N) break;
su[t]=1;
if(i%prm[j]==0) break;
}
}
}
LL gcd(LL a,LL b){
return !b?a:gcd(b,a%b);
}
vector<int>ve;
LL solve(LL x){
int f=1; if(x<0) f=0;
x=abs(x);
int flag=1; LL d;
for(int i=0;i<sz && prm[i]*1ll*prm[i]<=x;i++){
if(x%prm[i]==0){
int ge=0;
while(x%prm[i]==0) {ge++; x/=prm[i]; }
if(flag ) { d=ge; flag=0; }
else d=gcd(d,ge);
}
}
if(x!=1) d=1;
if(f) return d;
else {
ve.clear(); LL y=d;
for(LL i=1;i*1ll*i<=y;i++) {
if(y%i==0){
ve.push_back(i);
if(y/i!=i) ve.push_back(y/i);
}
}
sort(ve.begin(),ve.end());
for(int i=ve.size()-1;i>=0;i--){
int v=ve[i];
if(v&1) {
return v;
}
}
}
}
int main(){
init();
int t;scanf("%d",&t);
int cas=1;
while(t--){
LL x;scanf("%lld",&x);
printf("Case %d: %lld\n",cas++,solve(x));
}
return 0;
}