Title Address: 51Nod 1135
1. The original root definition: Set M>1,GCD (a,m) = 1, so that the minimum set of R, called a pair of modulo m of the order.
2. Theorem: If modulo m has an original root, then he has a total of the original root.
3. Theorem: If P is prime, then the prime number p must have the original root, and modulo p of the original root is a.
4. Theorem: Suppose that M is a positive integer, A is an integer, if the order of a-mode m equals, then A is called a primary root of modulo m.
5. The necessary and sufficient conditions for the original root of die m: M=2,4,p^a,2*p^a ....
The method of finding the primary root of modulus P: decomposition of P-1 factor, i.e. p-1= (P1^A1)(P2^A2) . (Pk^ak). , if the constant is established, then G is the original root of P (for composite, only need to change the p-1 can be)
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#include <bitset>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-7;Const intmaxn=1e6+Ten;intPRIME[MAXN];//Storage primeintSPRIME[MAXN];//Storage of P-1 factor bitset<Maxn>pri//Results only 0 and 1, judging whether it is a prime numberintK//Record the number of MAXN within aintCnt//number of recording element factorsvoidIs_prime () {pri.Set();//Mark all binary numbers as 1 for(intI=2; i<maxn; i++) {if(Pri[i]) {prime[k++]=i; for(intJ=i+i; j<maxn; J+=i) pri[j]=0; } }}voidDivide (intN//decompose n into a vegetarian factor{cnt=0;intT= (int)sqrt(1.0*N); for(intI=0; prime[i]<=t; i++) {if(n%prime[i]==0) {Sprime[cnt++]=prime[i]; while(n%prime[i]==0)//Because there may be multiple peime[i]N/=prime[i]; } }if(n>1) Sprime[cnt++]=n;//May only own a vegetarian factor}ll Modexp (LL a,ll B,intMoD//Fast power take -over redundancy{LL res=1; while(b>0) {a=a%mod;if(b&1) Res=res*a%mod; B=b>>1; A=a*a%mod; }returnRes;}intMain () {intP Is_prime (); while(~scanf("%d", &p)) {Divide (P-1); for(intg=2; g<p; g++) {intflag=1; for(intI=0; i<cnt; i++) {intT= (P-1)/sprime[i];if(Modexp (g,t,p) = =1) {flag=0; Break; } }if(flag) {introot=g;printf("%d\n", root); Break;//Remove break is to ask for all the original root, plus break is to find the smallest original root,} } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
51Nod 1135-Huangen (Fast solution of the original root of a prime number)