码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10622 Perfect P-th Powers

时间:2017-08-21 18:52:55      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:using   tps   color   tin   while   pow   分解质因数   ++   define   

https://vjudge.net/problem/UVA-10622

 

将n分解质因数,指数的gcd就是答案

如果n是负数,将答案除2至奇数

原理:(a*b)^p=a^p*b^p

 

 

#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 65550
using namespace std;
int gcd(int a,int b) { return !b ? a : gcd(b,a%b); }
int main()
{
    int m,sum,ans;
    long long n,nn;
    while(scanf("%lld",&nn)!=EOF)
    {
        if(!nn) return 0;
        n=abs(nn);
        m=sqrt(n);
        ans=0; 
        for(int i=2;i<=m;i++)
        {
            sum=0;
            if(n%i==0)
                 while(n%i==0) sum++,n/=i;
            ans=gcd(ans,sum);
        }
        if(!ans) 
        {
            printf("1\n");
            continue;
        }
        if(nn<0) 
        while(!(ans&1)) ans>>=1;
        printf("%d\n",ans);
    }
    
}

 

UVA 10622 Perfect P-th Powers

标签:using   tps   color   tin   while   pow   分解质因数   ++   define   

原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/7406023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!