Description
Fermat ' s theorem states that for any prime number P and for any integer a > 1, ap = a (mod p). That's, 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, the Some a. (and some, known as Carmichael Numbers, is 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, the output "yes" if P is a base-a pseudoprime; Otherwise output "no".
Sample Input
3 210 3341 2341 31105 21105 30 0
Sample Output
Nonoyesnoyesyes
If P is a prime number, output no; If P is not a prime number, determine if a^p is equal to P.
1#include <cstdio>2#include <math.h>3 __int64 F (__int64 A,__int64 B)4 {5__int64 c=b,t=1;6 while(b)7 {8 if(b%2!=0)9 {Tent=t*a%C; One } Aa=a*a%C; -B/=2; - } the returnt%C; - } - __int64 F2 (__int64 a) - { + __int64 i; - if(A <=1|| A2==0)return 0; + for(i=3; I<=sqrt (a); i++) A { at if(a% i = =0)return 0; - } - return 1; - } - intMain () - { in - __int64 p,a; to while(SCANF ("%i64d%i64d", &p,&a) && p &&a) + { - if(F2 (p) = =1) printf ("no\n"); the Else * { $ if(f (a,p) = = a) printf ("yes\n");Panax Notoginseng Else -printf"no\n"); the } + A } the}
POJ3641 (Fast power) to determine whether a^p = A (mod p) is established