http://poj.org/problem?id=2891
Test instructions: Solving a number x makes x%8 = 7,x%11 = 9; If x exists, the output is the smallest integer solution. otherwise output-1;
Ps:
Idea: This is not a simple question of China's residual theorem, since the input AI is not necessarily 22 coprime, while the condition of the Chinese remainder theorem is the divisor 22 coprime. This is a general model linear equation Group, forX MoD m1=r1X MoD m2=r2... ..... ..... ..X MoD Mn=rnfirst, let's look at two formulas.X mod m1=r1 ... ..... ... ... .... ... .... ... .... ... ..... ..... ..... ..... ..... ..... ... .. ..... ..... ............. (1)X mod m2=r2 ... ..... ... ... .... ... .... ... .... ... ..... ..... ..... ..... ..... ..... ... .. ..... ..... ............. (2)Then there areX=m1*k1+r1 ... .... ... .... ... ... ... ... .... ... .... ... .... ..... .... ..... ..... ..... ... .. ..... .......-.....-.....-....... (*)X=M2*K2+R2so M1*K1+R1=M2*K2+R2tidy up, getM1*K1-M2*K2=R2-R1order (a,b,x,y,m) = (M1,M2,K1,K2,R2-R1). The original becomesax+by=mAre you familiar? At this point, because GCD (A, B) =1 is not necessarily established, GCD (A, B) | M is not necessarily set up. So we should get a first sentence. GCD (A, B) | m not set, then!!。 Equation No solution!
!!
。 Otherwise, continue down. Solve (x, y) and k1=x back (*). Get x. so x is a special solution to these two equations, the general solution is the X ' =X+K*LCM (m1,m2) This formula again deformation, get X ' mod LCM (m1,m2) =x This equation comes out. It shows that we have implemented the merging of (1) (2) two equations.
make M=LCM (m1,m2). the combined equation can be recorded as X mod M = R r=r2-r1. Then, expand to N equations. with the combined equation and other equations to merge in this way, the last one can be left with only a single equation X mod m=r, where M=LCM (m1,m2,..., mn). So, X is a special solution to the linear equations of the original model, and the general solution is X ' =x+k*m. Suppose, to get the smallest positive integer solution of X, it is still the original method:x%=m; if (x<0) x+=m;
#include <stdio.h> #include <iostream> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #include <string.h> #include <queue> #include <string > #include <stdlib.h> #define LL long long#define _ll __int64#define eps 1e-8using namespace std;const int INF = 0x 3f3f3f3f;const int maxn = 10;_ll k;_ll m;_ll extend_gcd (_ll a,_ll b,_ll &x,_ll &y) {if (b = = 0) {x = 1; y = 0; return A; } else {_ll r = extend_gcd (b,a%b,x,y); _ll t = x; x = y; y = t-a/b*y; return R; }}int Main () {_ll a1,m1,a2,m2,x,y,i,d; while (scanf ("%lld", &k)! = EOF) {BOOL flag = true; scanf ("%lld%lld", &M1,&A1); for (i = 1; i < K; i++) {scanf ("%lld%lld", &M2,&A2); D = EXTEND_GCD (m1,m2,x,y); if ((A2-A1)%d! = 0) flag = false; _ll t = m2/d; x *= (A2-A1)/D; x = (x%t + t)%t; Notice how the new m1,a1 is derived a1 = X*M1+A1; m1 = m1*m2/d; A1 = (a1%m1+m1)%m1; } if (flag = = true) printf ("%lld\n", A1); else printf (" -1\n"); } return 0;}
POJ 2891 Strange to Express integers (Chinese remainder theorem)