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可能为负,我们知道只有b的是负数并且p为奇数才保证x负数。所以我们求的时候,如果求出来的p是偶数,我们让一直模2,直到p是奇数为止。
思路: 我一开始一看数据范围,发现看可以暴力,毕竟x的范围限定了, 所以在100之内枚举就行,但是发现怎么都不对,我感觉应该是我用了pow(),然后这个题卡了精度,不能这样做。
贴一下错误代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define rev(i,s,e) for(int i=e;i>=s;i--)
using namespace std;
typedef long long LL;
int main()
{
#ifdef LOCAL_FILE
freopen("in.txt", "r", stdin);
#endif
int t,id=1;
double x;
scanf("%d",&t);
while(t--)
{
scanf("%lf",&x);
if(x == 1 || x == -1)
{
printf("Case %d: 0\n",id++);
continue;
}
int mmax = -1;
double xx = x;
if(x<0) x=-x;
rep(i,1,1000)
{
double tmp = pow(x,1.0/i);
if(fabs(tmp - (double)((int)(tmp+0.5)))<1e-10)
{
if(i>mmax) mmax = i;
}
}
if(xx<0 && mmax%2 == 0)
{
while(mmax%2==0) mmax/=2;
printf("Case %d: %d\n",id++,mmax);
}
else
printf("Case %d: %d\n",id++,mmax);
}
return 0;
}
然后发现可以质因数分解,原理如下:
任何一个正整数x = e1^p1*e2^p2*……
然后gcd(p1,p2,……pn) 就是我们的最终答案,酱紫就不会出现精度问题了,唯一需要注意的地方就是要把x 声明为long long ,否则也错误。ε=(´ο`*)))唉,这类题目就是难受,卡你卡你卡你
AC代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define rev(i,s,e) for(int i=e;i>=s;i--)
using namespace std;
const int maxn = 1e6+5;
bool p[maxn];
int prime[maxn/10];
int tot;
int num;
int a[1005];//保存质因子
int b[1005];//保存质因子的个数
void Init() //素数打表
{
tot = 0;
memset(p,true,sizeof(p));
p[0] = p[1] = false;
for(long long i=2;i<maxn;i++)
{
if(p[i]){
prime[tot++] = i;
for(long long j=i*i;j<maxn;j+=i) p[j] = false;
}
}
}
void ndec(long long x)//质因数分解
{
num = 0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;1LL*prime[i]*prime[i]<=x;i++)
{
if(!(x%prime[i]))
{
a[num] = prime[i];
while(!(x%prime[i]))
{
b[num]++;
x/=prime[i];
}
num++;
}
}
if(x!=1)
{
a[num] = x;
b[num++] = 1;
}
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int main()
{
#ifdef LOCAL_FILE
freopen("in.txt","r",stdin);
#endif // LOCAL_FILE
// ios_base::sync_with_stdio(0);
// cin.tie(0),cout.tie(0);
Init();
int t,id = 1;
long long x;
scanf("%d",&t);
while(t--)
{
int flag = 1;
scanf("%lld",&x);
if(x<0) x=-x,flag = 0;
ndec(x);
int tmp = b[0];
if(!flag)
{
if(!(tmp%2)) while(!(tmp%2)) tmp/=2;
for(int i=0;i<num;i++)
{
if(!(b[i]%2))
{
while(!(b[i]%2)) b[i]/=2;
}
tmp = gcd(tmp,b[i]);
}
}
else{
for(int i=0;i<num;i++) tmp = gcd(tmp,b[i]);
}
printf("Case %d: %d\n",id++,tmp);
}
return 0;
}