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就是(x1,x2,....,xk)的最大公约数.
若为负数,p就必须为奇数,所以先对x化为正数,再对p/2,直到p为奇数
再由唯一分解定理可求
///debug测试样例;
/*
13
17
1073741824
25
2147483647
-2147483648
32
-32
64
-64
4
-4
324
-46656
Case 1: 1
Case 2: 30
Case 3: 2
Case 4: 1
Case 5: 31
Case 6: 5
Case 7: 5
Case 8: 6
Case 9: 3
Case 10: 2
Case 11: 1
Case 12: 2
Case 13: 3
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn=1e6+10;
int vis[maxn],s[maxn],a[maxn];
ll x;
int k=0;
void init()//打表
{
vis[1]=1;
for(int i=2; i<=1000000; i++)
if(vis[i]==0)
{
s[k++]=i;
for(int j=i*2; j<=1000000; j=j+i)
vis[j]=1;
}
}
int gcd(int x,int y)//gcd
{
if(y==0) return x;
else return gcd(y,x%y);
}
int main()
{
int t,op=1;
scanf("%d",&t);
init();
while(t--)
{
scanf("%lld",&x);
int flag=0;
if(x<0)//负数时
{
x=-x;
flag=1;
}
int ans=0;//p的值
for(int i=0;s[i]*s[i]<=x&&i<k;i++)
{
int res=0,f=0;
while(x%s[i]==0)
{
f=1;res++;
x/=s[i];
}
if(f)
{
if(ans==0) ans=res;
else ans=gcd(res,ans);
}
if(x==0||x==1) break;
}
if(x>1) //因子是它本身时
ans=1;
if(flag)//负数
while(ans%2==0)
ans/=2;
printf("Case %d: %d\n",op++,ans);
}
}
该博客探讨了一种数学问题,涉及寻找给定正整数x的最大幂次数p,使得x可以表示为某个整数的p次幂。通过唯一分解定理和最大公约数的计算,解决了负数和正数情况下的问题。文章提供了C++代码实现,并通过一系列测试用例验证了算法的正确性。
1123

被折叠的 条评论
为什么被折叠?



