A summary of number theory-EPIC masterpiece (This is a flag)

Source: Internet
Author: User
Tags gcd greatest common divisor modulus mul

1, Fast power

A fast algorithm for calculating a^b, for example, 3^5, we write 5 binary 101,3^5=3^1*1+3^2*2+3^4*1

1 ll Fast (ll A,ll b) {ll ans=1;  for (; b;b>>=1, A=mul (a,a))if(b&1) Ans=mul (ans,a); return ans;} // A quick power to a line
View Code

2. Fast Multiply

When the modulus is large, the direct multiply will explode long long, requiring fast multiplication.

that is, using floating-point computation multiples, the difference is equivalent to the result of calculating the remainder modulus 2^63, and then mold it (because the remainder does not exceed a long long)

1 Long Long ll; 2 ll Mul (ll X,ll y) {return ((x*y-(LL) (((longdouble) x*y+0.5)/ MoD) *mod)%mod+mod)%mod;} // One line Quick multiply
View Code

3, congruence principle (GCD)

Theorem: gcd (A, B) =gcd (b,a MoD)

Proof: A can be expressed as a=kb+r, then r=a MoD b

Assuming D is a number of conventions for a, B, then there are d|a, d|b, and r = a-kb, so d|r, so D is the number of conventions (B,a mod b)

Assuming D is (b,a mod b) The number of conventions, then d|b, D|r, and A=kb+r, so D is also (A, b) of the number of conventions

therefore (b) and (b,a mod b) The number of conventions is the same, and its greatest common divisor is necessarily equal, to be proven.

1 int gcd (A, b) {return !b?a:gcd (b,a%b);} // One line GCD
View Code

4. Throw graph equation

bezout theorem: Drop graph equation (two yuan once, below) Ax+by=m has the solution and only if the m| (A, B)

Extended gcd: An integer solution for AX+BY=GCD (A, A, b) of the equation of loss graphs

Proof: A*X1+B*Y1=GCD (A, B)

b*x2+ (a mod b) *y2=gcd (b,a mod b)

Because gcd (b) =gcd (b,a mod b)

a*x1+b*y1= b*x2+ (a mod b) *y2 = b*x2+ (a-a/b*b) *y2 = a*y2+b* (x2-a/b*y2)

So X1=y2,y1=x2-a/b*y2

End state: B=0,A=GCD (A, B), gcd (A, B) *x=gcd (A, b), x=1

The process of extending Euclid can be understood as the process of backtracking upward from the last state until a set of solutions to the original equation is obtained.

  1  void  exgcd (int  a,int  b,int  &x,int  &y)  2  { 3  if  (B==0 ) {x=1 ; Y=0 ; return  ;}  4  exgcd (b,a%b,x,y);  5  int  t=x;x=y;y=t-a/b*Y;  6 }  
View Code

General solution of the equation of lost graphs: if (b) =d, Ax+by=m has a set of solutions (X0,Y0) General Solution: x=x0+ (b/d) K, y=y0-(A/DK) is to try to get the positive and negative mutual cancellation, ( true • Primary School Olympiad content)

Doubts: Most beginners may have such a question (anyway I have just learned), to solve the equation is ax+by=m, and the above algorithm is the solution of AX+BY=GCD (A, B)

In fact, AX+BY=GCD (A, b) can become AX*K+BY*K=GCD (A, b) *k, so that gcd (a, b) *k=m, find K, and then X*k is the solution of ax+by=m.

5 , multiplication inverse element

Leave a hole.

6. Chinese remainder theorem

Leave a hole.

References--Wang Mengdi Henan Provincial Team Training Lecture notes on the basis of number theory

A summary of number theory-EPIC masterpiece (This is a flag)

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.