Mysterious Bacteria
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,让你求满足b^p = x 中最大的正整数p。
分析:
根据整数唯一分解定理可以得出 x=p1^ k1 + p2^ k2+…+pn^ kn,由于 x=p^k 中的 p 是唯一的,因此可分解完所有的 ki 后,求 GCD(k1,k2,…,kn) 即为答案
例如:当 x=17 时,有 x=2^ (4+1),由于要让 x=p^ k,因此应为 17=17^1,所以 k=GCD(4,1)
特别注意, x可能为负,我们知道只有b的是负数并且p为奇数才保证x负数。所以我们求的时候,如果求出来的p是偶数,我们让一直模2,直到p是奇数为止。
#include<bits/stdc++.h>
using namespace std;
#define N 1000001
#define LL long long
int prime[N],cnt;
bool bprime[N];
void makePrime() {
memset(bprime,true,sizeof(bprime));
bprime[0]=false;
bprime[1]=false;
for(int i=2; i<=N; i++) {
if(bprime[i]) {
prime[cnt++]=i;
for(int j=i*2; j<=N; j+=i)
bprime[j]=false;
}
}
}
int GCD(int a, int b) {
return b?GCD(b,a%b):a;
}
int main() {
makePrime();
int t;
scanf("%d",&t);
int Case=1;
while(t--) {
LL x;
scanf("%lld",&x);
bool flag=false;
if(x<0) { //负数的情况
x=-x;
flag=true;
}
int res=0;
for(int i=0; (LL)prime[i]*prime[i]<=x&&i<cnt; i++) {
if(x%prime[i]==0) { //枚举每一组的prime^k
int k=0;
while(x%prime[i]==0) {
k++;
x/=prime[i];
}
//求p=GCD(k1,k2,...,kn)
if(res==0)
res=k;
else
res=GCD(res,k);
}
}
if(x>1)
res=1;
if(flag) {
while(res%2==0)
res/=2;
}
printf("Case %d: %d\n",Case++,res);
}
return 0;
}
唯一分解定理:
唯一分解定理,也叫算术基本定理,指的是任何n>=2,都可以分解为n=p1p2p3*…pn,其中pi为质数。
其包括两个断言:
断言1:任何数n可以以某种方式分解成素数乘积。(因为一个数肯定是由合数和质数构成的,合数又可以分解成质数和合数,最后递归下去就会变成质数的乘积
如 36 -〉 4 * 9 或者 6 * 6 -〉 2 * 3 * 2 * 3 -〉2^ 2* 3^ 2。
最后化成了质数相乘的形式)
断言2:仅有一种这样的因数分解。(除因数重排外)。
唯一分解定理的两个小应用
1,求出数n的因子个数
(1+a1) * (1+a2) * (1+a3 ) * (1+a4)… * (1+an)
a1,a2,这些分别是素数因子的幂次数
因为当我的a1=3时那我n的因子肯定会有 p1^ 0 p1^ 1 p1^ 2 p1^ 3 这四个数
然后再和p2的个数搭配起来就是两个数的因子数相乘了 p1 ^ x 可以与 p2 ^ y 随意搭配,所以进行乘法
2.求所有的因子之和
这个其实也就是和上面这个一样的道理,不过我们求的是和,所以我们要把所有的因子和求出来
公式:(q1^ 0+q1 ^ 1+q1^ 2…q1^ a1)(q2^ 0+q2^ 1+q2^ 2…q2^ a2)…* (qn^ 0+qn^ n+qn^ 2…qn^ an)
因为每一项都有个1就代表是原来的自己那一项,后面都是组合项