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