poj 1061 青蛙约会

扩展欧几里德算法

代码:

 1 #include <cstdio>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 long long gcd_x(long long  a,long long b)
 7 {
 8     if(b == 0) return a;
 9     else
10     return gcd_x(b,a%b);
11 }
12 
13 long long judge(long long a,long long b,long long &x,long long &y)
14 {
15     if(b == 0)
16     {
17         x = 1;
18         y = 0;
19         return a;
20     }
21         long long t = judge(b,a%b,x,y);
22         long long k = x;
23         x = y;
24         y = k - a/b*y;
25         return t;
26 }
27 
28 int main()
29 {
30     long long x,y,m,n,l;
31     while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l) != EOF)
32     {
33         long long a,b,c,d;
34         a = n-m;
35         b = l;
36         c = gcd_x(a,b);
37         d = x - y;
38         if(d % c) printf("Impossible\n");
39         else
40         {
41             d /= c;
42             a /= c;
43             b /= c;
44             long long  x1 = 0,x2= 0;
45             judge(a,b,x1,x2);
46             x1 = (x1*d)%b;
47 
48              if(x1 < 0)
49                 x1 += b<0?-b:b;
50              printf("%lld\n",x1);
51         }
52 
53 
54 
55     }
56     return 0;
57 }
原文地址:https://www.cnblogs.com/yyroom/p/3016928.html