poj 1061 青蛙的约会

求线性同余方程的最小非负解;

gcd(a,n) = d;

若d|b则有d个解,分布在[0,n-1]上,周期为n/d,所以取[0,n/d-1]上的即可(取模)。
/* 谁让你试int型,WA活该 */
 1 # include <stdio.h>
2
3 long long int d, xx, yy;
4
5 long long int extended_euclid(long long int a, long long int b)
6 {
7 long long int t;
8 if (!b) d = a, xx = 1, yy = 0;
9 else
10 {
11 extended_euclid(b, a % b);
12 t = xx;
13 xx = yy;
14 yy = t - (a/b)*yy;
15 }
16 }
17
18 int main()
19 {
20 long long int a, b, t;
21 long long int x, y, m, n, L;
22
23 while (~scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &L))
24 {
25 a = (m - n + L) % L;
26 b = (y - x + L) % L;
27 extended_euclid(a, L);
28 if (b % d) printf("Impossible\n");
29 else
30 {
31 x = (xx*(b/d)) % L;
32 t = L / d;
33 x = (x%t + t) % t;
34 printf("%lld\n", x);
35 }
36 }
37
38 return 0;
39 }

你可能感兴趣的:(poj)