Pseudo Prime Numbers
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 633 accepted submission (s): 258
Problem descriptionfermat's theorem states that for any prime number P and for any integer a> 1, a ^ P = a (mod P ). that is, if we raise a to the PTH power and divide by P, the remainder is. some (but not very values) Non-prime values of P, known as base-a pseudo doprimes, have this property for some. (and some, known as Carmichael numbers, are base-a pseudo Primes for all .)
Given 2 <p ≤ 1,000,000,000 and 1 <A <p, determine whether or not p is a base-a Pseudo Prime.
Inputinput contains several test cases followed by a line containing "0 0". Each test case consists of a line containing P and.
Outputfor each test case, output "yes" if p is a base-a pseudo-prime; otherwise output "no ".
Sample Input
3 210 3341 2341 31105 21105 30 0
Sample output
nonoyesnoyesyes
[Problem-solving idea] This is a good number theory question. You can learn a lot, such as binary processing of a large number of indexes, and determining prime numbers and Pseudo Prime numbers. The most useful knowledge points are: 1. If P is an odd number, there is (. ^ p) mod (m) = (a % m) * (. ^ (p-1) % m is true; 2. If P is an even number,. ^ P =. ^ (p/2) *. ^ (p/2 ).
We can say that everyone knows these two conclusions, but it is not the same thing to use.
# Include <iostream> <br/> using namespace STD; <br/> int plist [100001], pcount = 0; <br/> int prime (int n) {<br/> int I; <br/> If (n! = 2 &&! (N % 2) | (n! = 3 &&! (N % 3) | (n! = 5 &&! (N % 5) | (n! = 7 &&! (N % 7) <br/> return 0; <br/> for (I = 0; plist [I] * plist [I] <= N; I ++) <br/> If (! (N % plist [I]) <br/> return 0; <br/> return n> 1; <br/>}< br/> void initprime () {<br/> int I; <br/> for (plist [pcount ++] = 2, I = 3; I <100000; I ++) <br/> If (prime (I) <br/> plist [pcount ++] = I; <br/>}< br/>__ int64 _ mode (_ int64 A ,__ int64 m) <br/>{< br/> _ int64 ans = 1, exp = m; <br/> while (exp) <br/>{< br/> If (exp % 2 = 1) <br/> ans = (ANS * A) % m; <br/> A = (A * A) % m; <br/> exp = exp/2; <br/>}< br/> return ans; <br/>}< br/> int main () <br/>{< br/> _ int64 PP, a; <br/> int I, j; <br/> initprime (); <br/> scanf ("% i64d % i64d", & PP, & ); <br/> while (1) <br/> {<br/> If (Pp = 0 & A = 0) <br/> break; <br/> If (prime (PP) = 1) <br/>{< br/> cout <"no" <Endl; <br/>}< br/> else <br/> {<br/> If (_ mode (A, pp) =) <br/> cout <"yes" <Endl; <br/> else <br/> cout <"no" <Endl; <br/>}< br/> scanf ("% i64d % i64d", & PP, & A); <br/>}< br/> return 0; <br/>}