Give 2 numbers, p and a,2<p<=1e9,1<a<p
If p satisfies the following 2 conditions, the output is yes, otherwise the output no
1.P is not a prime number
2. A^p=a (mod p)
First condition is judged first:
Originally wanted to use an array is_prime[i] means I is not prime, obviously, here p is too large, the array cannot be opened
If P is not a prime number,
Then P must have p=b*c, of which b<=c,
(sqrt (p)) ^2=b*c, then B<=sqrt (p) <=10^4.5<10^5
So if there is a number B in 10^5 to satisfy b<p&&p%b==0, p is not prime
Further, as long as the validation 10^5 existence prime B1 satisfies b1<p&&p%b1==0, stating that P is not a prime number, that is, satisfies the first condition
So as long as all the prime numbers inside the 10^5 are found, placed in the array prime, for p, traversing one side can, note: Prime[i]>=p, to break
If the first condition is satisfied, the second condition is validated, and a fast power can be done.
1#include <cstdio>2#include <cstring>3#include <cmath>4 5 using namespacestd;6 7 #defineLL Long Long8 9 Const intmaxn=1e5;Ten BOOLIS_PRIME[MAXN]; One intPrime[maxn>>1]; A - intinit_prime () - { the inti; - inttot=1; - intE= (int) (sqrt (0.0+MAXN) +1); -memset (Is_prime,true,sizeofis_prime); +is_prime[1]=false; -is_prime[2]=true; +prime[tot++]=2; A for(i=4; i<maxn;i+=2) atis_prime[i]=false; - for(i=3; i<e;i+=2) - { - if(Is_prime[i]) - { -prime[tot++]=i; in ints; - for(intj=i*i,s=2*i;j<maxn;j+=s) tois_prime[j]=false; + } - } the for(; i<maxn;i+=2) * { $ if(Is_prime[i])Panax Notoginsengprime[tot++]=i; - } the returntot; + } A the ll Quick_pow (ll A,ll p) + { -LL mod=p; $LL ret=1; $ while(p>0) - { - if(p&1) theret=ret*a%MoD; -a=a*a%MoD;Wuyip>>=1; the } - returnret; Wu } - About voidSolveinttot,ll p,ll a) $ { - BOOLflag=false; - for(intI=1; i<tot;i++) - { A if(prime[i]>=p) + Break; the if(p%prime[i]==0) - { $flag=true; the Break; the } the } the if(!flag) - { inprintf"no\n"); the return ; the } AboutLL ret=Quick_pow (a,p); the if(ret%p==a%p) the { theprintf"yes\n"); + } - Else the {Bayiprintf"no\n"); the } the return ; - } - the intMain () the { the LL p,a; the inttot=init_prime (); - while(SCANF ("%lld%lld",&p,&a)) the { the if(!p&&!a) the Break;94 solve (tot,p,a); the } the return 0; the}
View Code
POJ 3641 Prime number playing table + fast power simple problem