poj1061 青蛙的约会

思路:

用扩展欧几里得定理解模线性方程。

http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 typedef long long ll;
 6 
 7 ll abs(ll x)
 8 {
 9     return x < 0 ? -x : x;
10 }
11 
12 ll extgcd(ll a, ll b, ll & x, ll & y)
13 {
14     int d = a;
15     if (!b)
16     {
17         x = 1; y = 0;
18     }
19     else
20     {
21         d = extgcd(b, a % b, y, x);
22         y -= (a / b) * x;
23     }
24     return d;
25 }
26 
27 int main()
28 {
29     ll x, y, m, n, L;
30     cin >> x >> y >> m >> n >> L;
31     ll ans, p, q;
32     ans = extgcd((m - n), L, p, q);
33     if ((y - x) % ans) puts("Impossible");
34     else
35     {
36         ll mod = abs(L / ans);
37         p = (p * (y - x) / ans % mod + mod) % mod;
38         cout << p << endl;
39     }
40     return 0;
41 }
原文地址:https://www.cnblogs.com/wangyiming/p/7188436.html