Title Link: http://poj.org/problem?id=1061
is to find contentment (X+MT)-(Y+NT) = Lk's T and K
The upper formula can be reduced to (N-M) T + Lk = x-y, satisfying the form of ax+by=c so that we can seek t and K with the extension Euclid;
Since the upper formula has a solution and only if C is gcd (A, B) = 0;
#include <iostream>#include<stdio.h>#include<string.h>#include<string>#include<vector>#include<algorithm>#include<map>#include<queue>#include<stack>#include<math.h>using namespacestd;#defineMet (A, b) memset (A, B, sizeof (a))#defineN 10053#defineINF 0x3f3f3f3fConst intMOD = 1e9+7; typedefLong LongLL; ll GCD (ll A, ll b) {returnb = =0? A:GCD (b, a%b);}voidEX_GCD (ll A, ll B, ll &x, LL &y) { if(b = =0) {x=1; Y=0; return ; } EX_GCD (b, a%b, x, y); LL T=x; X=y; Y= t-a/b*y;}intMain () {LL X, Y, L, N, M; while(SCANF ("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l)! =EOF) {LL A, B, X, Y, C; A= n-m, B = L, c = xx; LL R=gcd (A, b); if(c%R) {puts ("Impossible"); Continue; } A= A/R; b= bR; C= c/R; ///The reason they are divided by R is to let AB coprime, then the result is the equivalent of x and y c times;EX_GCD (A, B, X, y);///at this time A and B coprime, the solution is Ax+by=1, the final solution to *c;x= x*C; X= x% B;///The minimum solution is required, so the remainder of the B is to be obtained; while(x <=0) {x+=b; } printf ("%lld\n", x); } return 0;}
View Code
Frog Dating---poj1061 (extended Euclid)