您的位置:首页 > 运维架构

POJ 3641 Pseudoprime numbers

2016-04-12 22:32 447 查看
p是素数直接输出no,然后判断a^p%p和a是否相等。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;

long long mod_exp(long long a, long long b, long long c)
{
long long res, t;
res = 1 % c;
t = a % c;
while (b)
{
if (b & 1) res = res * t % c;
t = t * t % c;
b >>= 1;
}
return res;
}

bool prime(long long x)
{
for(long long i=2;i*i<=x;i++)
if(x%i==0) return 0;
return 1;
}

int main()
{
long long p,a;
while(~scanf("%lld%lld",&p,&a))
{
if(p==0&&a==0) break;
if(prime(p)) printf("no\n");
else if(mod_exp(a,p,p)==a) printf("yes\n");
else printf("no\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: