HDU 1905 + POJ 1730 【快速幂】

来源:互联网 发布:贷款软件 编辑:程序博客网 时间:2024/06/15 15:51

Pseudoprime numbers


 

Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-apseudoprimes for all a.)

Given 2 < p ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-apseudoprime.

Input

Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a.

Output

For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".

Sample Input
3 210 3341 2341 31105 21105 30 0
Sample Output
nonoyesnoyesyes
题意:给出一个p个a,如果p是素数,则直接输出no
否则判断(a^p)%p是不是等于a
思路:裸题.
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>using namespace std;#define ll long longll ppow(ll a,ll b,ll mod){    ll ans=1;    ll base=a;    while(b!=0)    {        if(b&1!=0)            ans=ans*base%mod;        base=base*base%mod;        b>>=1;    }    return ans;}int judge(ll n){    for(int i=2;i<=sqrt(n);i++)        if(n%i==0)            return 1;    return 0;}int main(){    ll p,a;    while(cin>>p>>a)    {        if(p==0&&a==0)            break;        //cout<<judge(p)<<endl;        if(judge(p)==0)            cout<<"no"<<endl;        else if(ppow(a,p,p)==a)            cout<<"yes"<<endl;        else cout<<"no"<<endl;    }    return 0;}

Perfect Pth Powers

 
We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if, for some integer b, x = b 3. More generally, x is a perfect pth power if, for some integer b, x = b p. Given an integer x you are to determine the largest p such that x is a perfect p th power.
Input
Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.
Output
For each test case, output a line giving the largest integer p such that x is a perfect p th power.
Sample Input
171073741824250
Sample Output
1302
题意:给出一个n,判断他的最大次幂 也就是x^p=n 确定p的最大取值
思路:乍一看没什么难度,大师有坑
n是可以小于0的,对于8和-8来说最大都是3分别是2^3   (-2)^3    对于64来说+64=(2)^6    -64=(-4)^3
所以说n<0的时候p是不可能是偶数的   由于已经给出了上限 负数时只去奇数即可
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>using namespace std;#define ll long long#define mod 1000000007ll ppow(ll a,ll b){    ll ans=1;    ll base=a;    while(b!=0)    {        if(b&1!=0)            ans=ans*base%mod;        base=base*base%mod;        b>>=1;    }    return ans;}int main(){    int n;    int i;    while(cin>>n)    {        if(n==0)            break;         if(n>0)         {            for(i=31;i>=1;i--)            {                int temp=(int)(pow(1.0*n,1.0/i)+0.1);                int temp2=(int)(pow(1.0*temp,1.0*i)+0.1);                if(n==temp2)                {                    cout<<i<<endl;                    break;                }            }        }        else        {            n=-n;            for(i=31;i>=1;i-=2)            {                int temp=(int)(pow(1.0*n,1.0/i)+0.1);                int temp2=(int)(pow(1.0*temp,1.0*i)+0.1);                if(n==temp2)                {                    cout<<i<<endl;                    break;                }            }        }    }    return 0;}



原创粉丝点击