### 题目描述

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-a pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)

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

Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a. For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".

3 2
10 3
341 2
341 3
1105 2
1105 3
0 0

no
no
yes
no
yes
yes

### 代码如下

``````#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;

int f(long long n){
int k=sqrt(n);
int i;
if(n==1)return 0;
for(i=2;i<=k;i++){
if(n%i==0)return 0;
}
return 1;
}

int main(){
long long a,b;
long long a2,b2;
long long ans;
while(~scanf("%lld%lld",&b,&a)){
a2=a;
b2=b;
if(a==0&&b==0) break;
if(f(b))printf("no\n");//如果b是素数，输出no
else{//运用快速幂
ans=1;
while(b>0){
if(b&1)//判断是否为奇数，相当于 if(b%2==1)
ans=(ans*a)%b2;
a=(a*a)%b2;
b=b>>1;//二进制向右移一位，相当于 b=b/2;
}
if(ans%b2==a2)printf("yes\n");
else printf("no\n");
}
}
return 0;
}``````