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
Source
Waterloo Local Contest, 2007.9.23
感觉好久没A题了,脑子都快生锈了,所有赶紧做做题。
求(a^p)%p==a,数据大所有用long long
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 #include <stack> 15 using namespace std; 16 #define PI acos(-1.0) 17 #define max(a,b) (a) > (b) ? (a) : (b) 18 #define min(a,b) (a) < (b) ? (a) : (b) 19 #define ll long long 20 #define eps 1e-10 21 #define N 1000000 22 #define inf 1e12 23 ll pow_mod(ll a,ll n,ll MOD) 24 { 25 if(n==0) 26 return 1%MOD; 27 ll tt=pow_mod(a,n>>1,MOD); 28 ll ans=tt*tt%MOD; 29 if(n&1) 30 ans=ans*a%MOD; 31 return ans; 32 } 33 int main() 34 { 35 ll p,a; 36 while(scanf("%I64d%I64d",&p,&a)==2){ 37 if(p==0 && a==0){ 38 break; 39 } 40 int flag=0; 41 for(int i=2;i<(int)sqrt(p+0.5);i++){ 42 if(p%i==0){ 43 flag=1; 44 break; 45 } 46 } 47 if(flag==0){ 48 printf("no "); 49 continue; 50 } 51 ll ans=pow_mod(a,p,p); 52 53 //printf("%I64d ",ans); 54 if(ans==a){ 55 printf("yes "); 56 }else{ 57 printf("no "); 58 } 59 } 60 return 0; 61 }