#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
/*
唯一分解定理的运用
x=b^P 求P的最大值(-2^31<=x<=2^31)
x=p1^s1*p2^s2*……*pk^sk
P=gcd(s1,s2,……,sk)
if x为质数 than P=1
if(x<0) P只能为奇数,while(n%2==0) P/=2
*/
const int maxn=1e6+5;
const int maxm=1e7+5;
int prime[maxn];//素数
bool is_prime[maxm];//i是否是素数
int p;//素数个数
//素数筛
void sieve(int n)
{
p=0;
for(int i=0; i<n; i++) is_prime[i]=1;
is_prime[0]=is_prime[1]=0;
for(int i=2; i<n; i++)
{
if(is_prime[i])
{
prime[p++]=i;
for(int j=2*i; j<n; j+=i)
{
is_prime[j]=0;
}
}
}
}
int gcd(int a,int b)
{
return a%b==0?b:gcd(b,a%b);
}
int solve(LL a)
{
int cnt;
int ans=0;
LL t;
bool flag=0;//a是否小于0
if(a<0)
{
t=-a;
flag=1;
}
else
{
t=a;
}
//运用唯一分解定理进行分解
for(int i=0; i<p&&prime[i]*prime[i]<=t; i++)
{
if(t%prime[i]==0)
{
cnt=0;//计算质因数i的指数
while(t%prime[i]==0)
{
t/=prime[i];
cnt++;
}
if(ans==0)
{
ans=cnt;
}
else
{
ans=gcd(ans,cnt);//取所有指数的最大公约数
}
}
}
if(t>1)//如果a是质数
{
//ans=gcd(ans,1);
ans=1;
}
if(flag)//a为负数
{
while(ans%2==0)//一直除以2,ans为奇数为止
{
ans/=2;
}
}
return ans;
}
int main()
{
sieve(maxm);
int T;
LL n;
scanf("%d",&T);
int kase=0;
while(T--)
{
scanf("%lld",&n);
printf("Case %d: %d\n",++kase,solve(n));
}
return 0;
}
LightOJ - 1220 Mysterious Bacteria (唯一分解定理)
最新推荐文章于 2019-04-21 19:55:01 发布