Description
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 ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.
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 2 10 3 341 2 341 3 1105 2 1105 3 0 0
Sample Output
no no yes no yes yes
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 5 using namespace std; 6 typedef long long ll; 7 ll quick_pow(ll a,ll b,ll mod) { 8 ll ans=1; 9 while(b) { 10 if(b&1) 11 ans=(ans*a)%mod; 12 a=(a*a)%mod; 13 b>>=1; 14 } 15 return ans; 16 } 17 bool isprime(int x) { 18 for(int i=2;i*i<=x;i++) { 19 if(x%i==0) return false; 20 } 21 return true; 22 } 23 int main() { 24 // freopen("input.txt","r",stdin); 25 int a,p; 26 while(scanf("%d%d",&p,&a)!=EOF) { 27 if(a==0&&p==0) break; 28 if(isprime(p)) { 29 printf("no\n"); 30 continue; 31 32 } 33 ll z=quick_pow(a,p,p); 34 if(z==a) printf("yes\n"); 35 else printf("no\n"); 36 } 37 return 0; 38 }