1220 - Mysterious Bacteria--LightOj1220 (gcd)

http://lightoj.com/volume_showproblem.php?problem=1220

题目大意: 给你一个x,求出满足 x=b^p, p最大是几。

分析:x=p1^a1*p2^a2*...*pn^an;

p最大是gcd(a1,a2,...,an)。

///他该诉你x,b,p都是整数,所以x,b有可能是负数。当x是负数时,p不能是偶数。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue> using namespace std;
typedef long long LL;
#define N 100000
#define ESP 1e-8
#define INF 0x3f3f3f3f
#define memset(a,b) memset(a,b,sizeof(a)) LL prime[N], k;
bool vis[N]; void Prime()
{
memset(vis, false);
k = ;
for(int i=; i<N; i++)
{
if(vis[i] == )
{
prime[k ++] = i;
for(int j=i+i; j<N; j+=i)
{
vis[j] = ;
}
}
}
} LL gcd(LL a, LL b)
{
LL r;
while(b)
{
r = a%b;
a = b;
b = r;
}
return a;
} LL solve(LL n)
{
LL ans, sum;
ans = ;
sum = ;
int flag = ; for(int i=; prime[i]*prime[i] <= n; i++)
{
if(n%prime[i] == )
{
ans = ;
while(n%prime[i] == )
{
n /= prime[i];
ans ++;
}
if(flag == )
{
sum = ans;
flag = ;
}
else if(flag == )
sum = gcd(sum, ans);
}
}
if(n>)
sum = ; return sum;
} int main()
{
int T, t=;
Prime();
scanf("%d", &T);
while(T --)
{
LL n;
scanf("%lld", &n); int b = ; if(n<)
{
n = -n;
b = ;
} LL sum = solve(n); if(b == && sum% == )
{
while(sum% == )
sum/=;
}
printf("Case %d: %lld\n", t++, sum);
}
return ;
}
上一篇:『忘了再学』Shell基础 — 2、Shell的作用与分类


下一篇:python基础整理笔记(八)