pku1061 青蛙的约会

http://poj.org/problem?id=1061

数论,单变元模线性方程

 1 #include <stdio.h>
 2 
 3 long long extend_gcd(long long a, long long b, long long &x, long long &y)
 4 {
 5     if(b == 0)
 6     {
 7         x = 1;
 8         y = 0;
 9         return a;
10     }
11     else
12     {
13         long long r = extend_gcd(b, a%b, y, x);
14         y -= x*(a/b);
15         return r;
16     }
17 }
18 
19 long long line_mod_equation(long long a, long long b, long long n)
20 {
21     long long x, y;
22     long long d = extend_gcd(a, n, x, y);
23     if(b % d == 0)
24     {
25         x %= n;
26         x += n;
27         x %= n;
28         x = (x*(b/d) % (n/d));
29         x = (x%(n/d) + (n/d)) % (n/d);
30         return x;
31     }
32     else
33     {
34         return -1;
35     }
36 }
37 
38 int main()
39 {
40     long long a, b, n, temp;
41     long long X, Y, M, N, L;
42     scanf("%lld%lld%lld%lld%lld", &X, &Y, &M, &N, &L);
43     a = N - M;
44     b = X - Y;
45     n = L;
46     temp = line_mod_equation(a, b, n);
47     if(temp == -1)
48     {
49         printf("Impossible\n");
50     }
51     else
52     {
53         printf("%lld\n", temp);
54     }
55     return 0;
56 }

 

posted @ 2013-05-31 11:31  Yuan1991  阅读(145)  评论(0编辑  收藏  举报