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 xdays. 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
题目大意:给一个数n,让你求n可以表示为x的p次幂,求p的最大值。
思路:唯一分解定理:
注意: x可能为负,我们知道只有b的是负数并且p为奇数才保证x负数。所以我们求的时候,如果求出来的p是偶数,我们让一直模2,直到p是奇数为止。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
ll su[80000],book[1000010];
ll r=0;
void prime()//素数打表
{
memset(book,0,sizeof(book));
book[0]=book[1]=1;
for(ll i=2; i<1000010; i++)
{
if(!book[i])
{
su[r++]=i;
for(ll j=i*2; j<1000010; j+=i)
book[j]=1;
}
}
}
ll gcd(ll a,ll b)//最大公约数
{
return b==0?a:gcd(b,a%b);
}
ll ans;
int flag;
void solve(ll a)
{
for(int i=0; i<r&&a; i++)
{
int num=0;
if(su[i]*su[i]>a)
break;
while(a%su[i]==0)
{
a/=su[i];
num++;
}
if(flag)//n为负数的时候
{
while(num%2==0&&num)
num/=2;
}
if(num&&!ans)
ans=num;
else if(num)
ans=gcd(ans,num);
}
if(a>1)//**********
ans=1;
}
int main()
{
int c;
prime();
int o=1;
scanf("%d",&c);
while(c--)
{
ll n;
scanf("%lld",&n);
flag=0;
if(n<0)
n=-n,flag=1;
ans=0;
solve(n);
printf("Case %d: %lld\n",o++,ans);
}
return 0;
}