Number theory/the Second wave

Source: Internet
Author: User

Extended Euclidean algorithm.

void exgcd (int a,int b,int&x,int&y) {if (!b) {X=1;y=0;return;} EXGCD (b,a%b,x,y); int temp=x;x=y;y=temp-a/b*y;}

1) ax+by=c.

The condition of the solution is C%GCD (A, b) ==0, because AX+BY=GCD (a, B) must have a solution.

The solution is x0,y0, then the general solution x=x0+ (B/GCD (A, a) *t y=y0-(A/GCD (b)) *t

2) ax≡1 (mod m)

X is a on the inverse of M, i.e. Ax-my=1, if GCD (a,m)!=1 no solution

The solution is x0, then the general solution x=x0+m*t A about m inverse is about M congruence. Then the minimum positive integer solution ∈ (0,m), then x= (X0%abs (m) +abs (m))%abs (m);

noip2012 equation: Find the minimum positive integer solution of ax≡1 (mod n).

3) Seek ax≡t (mod b);

Can find the solution of AX+BY=GCD (A, B), if T%GCD (A, B)! =0 no solution. Otherwise the minimum integer solution x= (X0%abs (B/GCD) +abs (B/GCD))%abs (B/GCD) * (C/GCD)

eg

int cal (int a,int b,int c) {int x,y;int gcd=exgcd (a,b,x,y); X*=c/gcd;b/=gcd;if (b<0) B=-b;return (x%b+b)%b;}

  

Number theory/the Second wave

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.