Pseudoprime numbers
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 7000 |
|
Accepted: 2855 |
Description
Fermat ' s theorem states that for any prime numberPand for any integera> 1,AP=a (Mod&NBSP; p ). That is, if we raise&NBSP; a To The&NBSP; p th power and Divide by&NBSP; p , the remainder is &NBSP; a . Some (but not very many) Non-prime values Of&NBSP; p , known as base- a< Span class= "Apple-converted-space" >&NBSP; pseudoprimes, with this property for Some&NBSP; a . (and some, known as Carmichael Numbers, is base- a pseudoprimes for All&NBSP; 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
Source
Waterloo Local Contest, 2007.9.23
1#include <cstdio>2#include <cmath>3#include <cstring>4#include <iostream>5 using namespacestd;6 #defineMax 10000000007typedefLong Longll;8 BOOLIsPrime (intN) {//Judging the prime number, the problem max is too large, not suitable for the table method9 if(n==0|| n==1){Ten return false; One } A if(n==2){ - return true; - } the if(n%2==0) - return false; - intI=3, HALF=SQRT (n1.0); - for(; i<=half;i+=2){ + if(n%i==0){ - return false; + } A } at return true; - } -ll work (ll A,ll b,ll k) {//a^b mod k quick Power template - if(a==0){ - return 0; - } in if(b==0){ - return 1; to } +ll x=a%k,ans=1; - for(;b>0; b/=2){ the if(b%2){ *Ans= (ans*x)%k;//This must be the last to arrive . $ }Panax Notoginsengx=x*x%K; - } the returnans; + } A intMain () the { + intP,a; - while(cin>>p>>a&&p&&a) { $ //Cout<<isprime (p) <<endl; $ if(!isprime (p) && (Work (a,p,p) ==a%p)) {//pay attention to prime number judgment -cout<<"Yes"<<Endl; - } the Else{ -cout<<"No"<<Endl;Wuyi } the } - return 0; Wu}
POJ 3641 pseudoprime Numbers