51Nod 1135-Huangen (Fast solution of the original root of a prime number)

Source: Internet
Author: User
Tags bitset

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.