POJ 3641 快速幂+素数

http://poj.org/problem?id=3641

练手用,结果念题不清,以为是奇偶数WA了一发

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
bool judge_prime(ll k)
{
    ll i;
    ll u=int(sqrt(k*1.0));
    for(i=2;i<=u;i++)
    {
        if(k%i==0)
            return 0;
    }
    return 1;
}
ll mod_pow(ll x,ll n,ll mod)
{
    ll res=1;
    while(n>0)
    {
        if(n&1) res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}
int main()
{
    ll num=0,a,p;
    while(~scanf("%lld %lld",&p,&a))
    {
        if(p==0&&a==0) {num=0;}
        else{
            if(judge_prime(p)) cout<<"no"<<endl;
            else{
                num=mod_pow(a,p,p);
                if(num==a) cout<<"yes"<<endl;
                else cout<<"no"<<endl;}}
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dzzy/p/5241762.html