POJ1061青蛙的约会(扩展欧几里得)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <math.h> 5 #include <iostream> 6 #include <stack> 7 #include <set> 8 #include <queue> 9 #define MAX(a,b) (a) > (b)? (a):(b) 10 #define MIN(a,b) (a) < (b)? (a):(b) 11 #define mem(a) memset(a,0,sizeof(a)) 12 #define INF 1000000007 13 #define MAXN 20005 14 using namespace std; 15 16 __int64 GCD(__int64 a,__int64 b) 17 { 18 return a%b==0?b:GCD(b,a%b); 19 } 20 21 void gcd(__int64 a,__int64 b,__int64 &x,__int64 &y) 22 { 23 if(!b){x=1;y=0;} 24 else{gcd(b,a%b,y,x); y-=x*(a/b);} 25 } 26 27 int main() 28 { 29 __int64 X,Y,M,N,L; 30 while(~scanf("%I64d %I64d %I64d %I64d %I64d",&X,&Y,&M,&N,&L)) 31 { 32 __int64 a = (N-M), b = L, c = (X-Y); 33 __int64 g = GCD(a,b); 34 if(c%g){ printf("Impossible\n"); continue;} 35 __int64 x, y; 36 gcd(a, b, x, y); 37 x*=(c/g); 38 if(x>0)x%=(b/g); 39 else x=x%(b/g)+(b/g); 40 printf("%I64d\n",x); 41 } 42 return 0; 43 }