Time Limit: 0.5 second(s) | Memory Limit: 32 MB |
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 | Output for Sample Input |
3 17 1073741824 25 | Case 1: 1 Case 2: 30 Case 3: 2 |
p = gcd(e1,e2, e3,....,ei); 如果n 是负数则需要转换成正数,并且p一定是奇数才可以。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e7+7;
ll n;
int prim[maxn/10], cnt = 0;
bool vis[maxn];
ll gcd(ll a, ll b) {
if(!b) return a;
else return gcd(b, a%b);
}
void getprim(){
memset(vis, 0, sizeof(vis));
for(int i = 2; i*i < maxn; i++){
if(!vis[i]) {
for(int j = i*i; j < maxn; j += i)
vis[j] = true;
}
}
for(int i = 2; i < maxn; i++)
if(!vis[i]) prim[cnt++] = i;
}
ll getpr(ll n) {
bool f = false;
if(n < 0) n = -n, f = true;
ll ans = 0;
for(int i = 0; i < cnt && n > 1; i++)
{
if(n % prim[i] == 0) {
int c = 0;
while(n % prim[i] == 0){
n /= prim[i];
c++;
}
if(ans == 0) ans = c;
else ans = gcd(ans, c);
}
}
if(n > 1) ans = 1;
if(f){
while(ans % 2 == 0) ans /= 2;
}
return ans;
}
int main()
{
int t, cas = 0;
getprim();
cin >> t;
while(t--){
cin>>n;
printf("Case %d: %lld\n", ++cas, getpr(n));
}
return 0;
}