Number theory (Primer)

Source: Internet
Author: User
Tags greatest common divisor

Several important things to keep in mind:

1. Euclid theorem (Euclidean law)

int gcd (intint  b) {    return b==0? a:gcd (b, a%b);}

2. Extended Euclidean (Ax+by = gcd (A, B)

void E_GCD (ll A, ll B, LL &d, LL &x, LL &y) {    if(b==0) {         1 0; D = A;    }      Else {        E_GCD (b, a%B, D, y, x);        Y-= x* (A/b);    }}

3. Chinese remainder theorem

Congruence equations

x ≡a1 (mod m1)

x ≡a2 (mod m2)

... ...

x ≡ak (mod mk)

The set of all solutions to a set of equations is:

x1 = n1*a1 + n2*a2 + ... + nk*akwhere Ni mod mi = 1,ni = ai * ti, can be used Euclidean expansion theorem to seek ti. where m = m1*m2*m3 *MN;
#include <iostream>using namespacestd; //extended Euclidean theorem with parameters that can be negative    voidEXOJLD (intAintBint&x,int&y) {          //According to Euclid's theorem.        if(b = =0){//the greatest common divisor of any number with 0 is its own. x =1; Y=0; }Else{              intx1, y1; EXOJLD (b, a%B, x1, y1); if(A*b <0){//The opposite of the different numberx =-Y1; Y= A/b*y1-X1; }Else{//Same numberx =Y1; Y= x1-a/b*Y1; }          }      }      //residual theorem    intCALSYDL (intA[],intM[],intk) {          intN[K];//This can be deleted        intMM =1;//least common multiple        intresult =0;  for(inti =0; I < K; i++) {mm*=M[i]; }           for(intj =0; J < K; J + +){              intL, J; EXOJLD (mm/M[J],-M[j], L, J); N[J]= M[j] * j +1;//1N[J] = mm/m[j] * L;//2 "Note" 1 and 2 values should be equal. Result + = n[j]*A[j]; }          return(Result% mm + mm)% mm;//fall between (0, MM), this is written to prevent result initial negative, in this case can not be negative may be directly written: return result%mm;     }                  intMain () {inta[3] = {2,3,2}; intm[3] = {3,5,7}; cout<<"Results:"<<calsydl (A, M,3) <<Endl; }  

4. Euler functions(The number of all the numbers in front of a number with this number coprime)  Euler function expression formula: Euler (x) =x (1-1/P1) (1-1/P2) (1-1/P3) (1-1/P4) ... (1-1/PN), where P1,P2......PN is the all-factor of x, and X is an integer that is not 0. Euler (1) =1 (the number of unique and 1 coprime is 1 itself).
    //solving Euler functions directly    intEulerintN) {//return Euler (n)         intRes=n,a=O;  for(intI=2; i*i<=a;i++){               if(a%i==0) {res=res/i* (I-1);//Division first to prevent overflow of intermediate data                  while(a%i==0) a/=i; }           }           if(a>1) res=res/a* (A-1); returnRes; }            //selection method to play the European pull function table    #defineMax 1000001intEuler[max]; voidInit () {euler[1]=1;  for(intI=2; i<max;i++) Euler[i]=i;  for(intI=2; i<max;i++)              if(euler[i]==i) for(intj=i;j<max;j+=i) euler[j]=euler[j]/i* (I-1);//Division first to prevent overflow of intermediate data}

Number theory (Primer)

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.