Number theory algorithm templates (not updated regularly)

Source: Internet
Author: User
Tags gcd

/**********/#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<cstdlib>#include<vector>#include<stack>#include<map>using namespaceStd;typedefLong Longll;/***********gcd**********************/ll gcd (ll A,ll b) {if(b==0)        returnA; returnGCD (b,a%b);}/************* Fast Ride ***************/ll Mult_mod (ll a,ll b,ll MoD) {a%=mod; b%=MoD; ll Res=0;  while(b) {if(b&1) {res+=A; Res%=MoD; } A<<=1; if(A>=mod) a%=MoD; b>>=1; }    returnRes;}/************* Fast Power **************/ll Pow_mod (ll x,ll n,ll MoD) {if(n==1)returnX%MoD; X%=MoD; ll T=x,res=1;  while(n) {if(n&1) res=Mult_mod (RES,T,MOD); T=Mult_mod (T,T,MOD); N>>=1; }    returnRes;}/************ Extended Euclid ****************/voidEXTEND_GCD (ll a,ll b,ll &x,ll &y) {ll D;//D=GCD (A, b)    if(b==0) {x=1, y=0; D=A; }    Else{d=EXTEND_GCD (b,a%b,y,x); ll xx=x,yy=y; X=yy; Y=xx-(A/b) *yy; }}/****************** of *********** Prime sieve method*/ll L,u,prime[n];inttot;intvis[n],ans[10000005];voidIsPrime () {tot=0; memset (Vis,0,sizeof(VIS)); memset (Prime,0,sizeof(prime));  for(LL i=2; i<n;i++)    {        if(!Vis[i]) {Prime[tot++]=i;  for(LL j=i*i;j<n;j+=i) vis[j]=1; }    }}/********* Prime number Determination miller_rabin***********/BOOLTest (ll a,ll N)//the core of the Miller_rabin algorithm{ll x=n-1, t=0, Res,last;  while((x&1)==0) {x>>=1; T++; } Last=Pow_mod (a,x,n);  for(intI=0; i<t;i++) {res=pow_mod (Last,2, N); if(res==1&&last!=1&&last!=n-1)            return true; Last=Res; }    if(res!=1)return true; return false;}BOOLMillier_rabin (ll N) {if(n==2)return true; if(n==1|| (n&1)==0)return false;  for(intI=0; i<times;i++) {ll a=rand ()% (n1)+1; if(Test (a,n))return false; }    return true;}/********* Large number factor decomposition pollard_rho*************/ll Pollard_rho (ll X,ll c) {ll x0,y,i=1, k=2; X0=rand ()%x; Y=x0;  while(1) {i++; X0= (Mult_mod (x0,x0,x) +c)%x; ll D=GCD (yx0,x); if(d>1&AMP;&AMP;D&LT;X)returnD; if(y==x0) Break; if(i==k) {y=x0; K+=K; }    }    returnx;}voidFIND_FAC (ll N,intc) {    if(n==1)return; if(Millier_rabin (n)) {Factor[tot++]=N; return; } ll P=N;  while(p>=N) P=pollard_rho (p,c--);    FIND_FAC (P,C); FIND_FAC (n/p,c);}/********** Euler function **********************/intEuler (intN) {    intres=N;  for(intI=2; i*i<=n;i++)    {         while(n%i==0) {n/=i; res-= (res/i);  while(n%i==0) n/=i; }    }    if(n>1) Res-= (res/N); returnRes;}/********** Euler function **************/intEuler (intN) {    intres=N;  for(intI=2; i*i<=n;i++)    {         while(n%i==0) {n/=i; res-= (res/i);  while(n%i==0) n/=i; }    }    if(n>1) Res-= (res/N); returnRes;}/** * * * Prime sieve method + Euler hit table **********/ll E[n+5],p[n+5];BOOLvis[n+5];voidinit () {memset (E,0,sizeof(e)); Memset (P,0,sizeof(p)); memset (Vis,false,sizeof(VIS));    ll I,j; p[0]=1;//total number of recorded primesp[1]=2;  for(i=3; i<n;i+=2)    {        if(!Vis[i]) {p[++p[0]]=i;  for(j=i*i;j<n;j+=i) vis[j]=true; }} e[1]=1;  for(i=1; i<=p[0];i++) E[p[i]]=p[i]-1;//Why do you want-1?      for(i=2; i<n;i++)    {        if(!E[i]) {             for(j=1; j<=p[0];j++)            {                if(i%p[j]==0)                {                    if(i/p[j]%P[j]) e[i]=e[i/p[j]]*E[p[j]]; ElseE[i]=e[i/p[j]]*P[j];  Break; }            }        }    }}/************* Euler play table * Faster version ***************/#defineN 1000000inte[n+5];voidinit () {inti,j; Memset (E,0,sizeof(e));  for(i=2; i<=n;i++)    {        if(!E[i]) {             for(j=i;j<=n;j+=i) {if(!E[j]) e[j]=J; E[J]=e[j]/i* (I-1); }        }    }}

Number theory algorithm templates (not updated regularly)

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.