Description
如果pp不是素数且则称pp是伪素数,给出p,ap,a,判断pp是否为伪素数
Input
多组用例,每组用例输入两个整数p,ap,a,以0 00 0结束输入(2≤p≤109,1<a<p)(2≤p≤109,1<a<p)
Output
如果pp是伪素数则输出yesyes,否则输出nono
Sample Input
3 2
10 3
341 2
341 3
1105 2
1105 3
0 0
Sample Output
no
no
yes
no
yes
yes
Solution
简单题,素数判定+快速幂
Code
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<ctime>
using namespace std;
typedef long long ll;
int p,a;
int check(int n)
{
for(int i=2;i*i<=n;i++)
if(n%i==0)return 0;
return 1;
}
int Pow(int a,int b,int c)
{
int ans=1;
while(b)
{
if(b&1)ans=(ll)ans*a%c;
a=(ll)a*a%c;
b>>=1;
}
return ans;
}
int main()
{
while(~scanf("%d%d",&p,&a),p||a)
{
if(!check(p)&&Pow(a,p,p)==a)printf("yes\n");
else printf("no\n");
}
}