Find the minimum positive integer x,a^x=1 (mod M) Order template

Source: Internet
Author: User

Order of integers: set A and n are positive integers of the a^x=1, making the smallest positive integer x (mod n) The order of a modulo n

//order Template: A^x=1 (mod M), call Getjie (a,m)//Input: 10^10>a,m>1//output: No solution returned-1, with a solution to return the smallest positive integer x//complexity: O (m^ (0.5))Long LonggcdLong LongALong Longb) {    if(b==0)returnA; returnGCD (b,a%b);}//Euler function: Complexity O (n^ (0.5)), returns the number of all and N-1,n-1 in [Long LongPhiLong Longx) {    Long Longsum=x;  for(Long LongI=2; i*i<=x;i++)    {        if(x%i==0) {sum=sum-sum/i;  while(x%i==0) x/=i; }    }    if(x!=1) sum=sum-sum/x; returnsum;}Long LongMod_mul (Long LongALong LongBLong LongMoD) {    Long Longsum=0;  while(b) {if(b&1) sum= (sum+a)%MoD; b>>=1; A= (a+a)%MoD; }    returnsum;}//A^b%mod Fast PowerLong LongQuk_mul (Long LongALong LongBLong LongMoD) {    Long Longqsum=1;  while(b) {if(b&1) qsum=Mod_mul (QSUM,A,MOD); b>>=1; A=Mod_mul (A, A, Mod); }    returnqsum;}Long LongGetjie (Long LongALong LongM) {    Long LongD =gcd (a,m); if(d! =1)return-1; Long Longm=Phi (M); //and then split the M    Long Longprm[ +],prmcnt[ +]; intPcnt=0; memset (prmcnt,0,sizeof(prmcnt)); Long LongTM =m;  for(Long LongI=2; i*i<=tm;i++)    {        if(tm%i==0) {prm[pcnt]=i;  while(tm%i==0) {prmcnt[pcnt]++; TM/=i; } pcnt++; }    }    if(tm!=1) {prm[pcnt]=TM; PRMCNT[PCNT]=1; Pcnt++; }     for(intI=0; i<pcnt;i++)    {         for(intj=0; j<prmcnt[i];j++)        {            if(Quk_mul (A, M/prm[i], m) = =1) {m/=Prm[i]; }        }    }    returnm;}

Plus a list of the primes. Theoretically it will be 10 times times faster.

//order Template: A^x=1 (mod M), call Getjie (a,m)//Input: 10^10>a,m>1//output: No solution returned-1, with a solution to return the smallest positive integer x//complexity: m^ (0.5)Long Longprime[100100];Long LonggcdLong LongALong Longb) {    if(b==0)returnA; returnGCD (b,a%b);}//Euler function: Complexity O (n^ (0.5)), returns the number of all and N-1,n-1 in [Long LongPhiLong Longx) {    Long Longsum=x; Long Longp = prime[0];  for(intI=0;p *p<=x;i++)    {        if(0= = Xp) {sum=sum-sum/p;  while(x%p==0) x/=p; } P=prime[i+1]; }    if(x!=1) sum=sum-sum/x; returnsum;}Long LongMod_mul (Long LongALong LongBLong LongMoD) {    Long Longsum=0;  while(b) {if(b&1) sum= (sum+a)%MoD; b>>=1; A= (a+a)%MoD; }    returnsum;}//A^b%mod Fast PowerLong LongQuk_mul (Long LongALong LongBLong LongMoD) {    Long Longqsum=1;  while(b) {if(b&1) qsum=Mod_mul (qsum,a,mod);//mod is not a very big time. This step can remove the B>>=1; A=Mod_mul (A, A, Mod);/}returnqsum;}Long LongGetjie (Long LongALong LongM) {    Long LongD =gcd (a,m); if(d! =1)return-1; Long Longm=Phi (M); //and then split the M    Long Longprm[ +],prmcnt[ +]; intPcnt=0; memset (prmcnt,0,sizeof(prmcnt)); Long LongTM =m; Long Longp=prime[0];  for(Long LongI=0;p *p<=tm;i++)    {        if(tm%p==0) {prm[pcnt]=p;  while(tm%p==0) {prmcnt[pcnt]++; TM/=p; } pcnt++; } P=prime[i+1]; }    if(tm!=1) {prm[pcnt]=TM; PRMCNT[PCNT]=1; Pcnt++; }     for(intI=0; i<pcnt;i++)    {         for(intj=0; j<prmcnt[i];j++)        {            if(Quk_mul (A, M/prm[i], m) = =1) {m/=Prm[i]; }        }    }    returnm;}voidGetprime (Long LongUp ) {    BOOLpmark[200100]; memset (Pmark,0,sizeof(Pmark)); intPcnt=0; Prime[pcnt++]=2;  for(intI=2; i<=up;i+=2) pmark[i]=1;  for(intI=3; i<=up;i+=2)    {        if(pmark[i]==0) {prime[pcnt++ ] =i;  for(intj=i+i;j<=up;j+=i) {pmark[j]=1; }        }    }}

Find the minimum positive integer x,a^x=1 (mod M) Order template

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.