题目链接:http://lightoj.com/volume_showproblem.php?problem=1220
题目大意:输入T组数据,每组一个整数
n
(可能为负数),求满足
思路:
当n>0, 对n进行质因数分解,然后最大的
p
就是所有质因子出现次数的最大公因数,即
当n<0, 易知
ans
必须为奇数, 若
ans
为偶数,由
pans1=(p1x)y(y为所求)
考虑对
ans
分解质因数,则必为
ans=2e1⋅pe22⋅pe33...
形式,故对
ans
一直除以2直到它为奇数即求得
y
<script type="math/tex" id="MathJax-Element-85">y</script>可。
代码:
//代码太简单,不打备注了。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#define LL long long
using namespace std;
const int Max=1e5+10;
bool vis[Max];
int prime[Max], mem;
void get_prime(){
for(int a=2; a<Max; a++){
if(!vis[a]){
prime[mem++]=a;
for(int b=a<<1; b<Max; b+=a){
vis[b]=1;
}
}
}
}
int gcd(int a,int b){
return b==0?a:gcd(b, a%b);
}
int main(){
get_prime();
//for(int a=0; a<10; a++)cout<<prime[a]<<' ';
int T, cases=1;
scanf("%d", &T);
while(T--){
LL n;
scanf("%lld", &n);
int flag=0;
if(n<0){
flag=1;
n=-n;
}
int ans=-1;
for(int a=0; a<mem; a++){
if(prime[a]>n)break;
int e=0;
while(n%prime[a]==0){
e++;
n/=prime[a];
}
if(e){
ans= (ans==-1?e:gcd(ans, e));
}
}
if(n!=1)ans=1;
if(flag){
while(ans%2==0){
ans/=2;
}
}
printf("Case %d: %d\n", cases++, ans);
}
return 0;
}