-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPOJ1061(AC).cpp
77 lines (69 loc) · 968 Bytes
/
POJ1061(AC).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long int LL;
LL gcd(LL a, LL b)
{
if(a < 0){
return gcd(-a, b);
}
if(b < 0){
return gcd(a, -b);
}
while(true){
if(a > b){
if(a % b == 0){
return b;
}else{
a = a % b;
}
}else{
if(b % a == 0){
return a;
}else{
b = b % a;
}
}
}
}
void ext_gcd(LL a, LL b, LL &m, LL &n)
{
if(b == 0){
m = 1;
n = 0;
return;
}
ext_gcd(b, a % b, m, n);
LL t;
t = m;
m = n;
n = t - a / b * n;
}
int main()
{
LL m, n, x, y, l;
LL a, b, t, c;
LL k1, k2;
while(scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l) == 5){
a = n - m;
b = l;
c = x - y;
t = gcd(a, b);
if(c % t){
printf("Impossible\n");
continue;
}
a /= t;
b /= t;
c /= t;
ext_gcd(a, b, k1, k2);
t = c * k1 / b;
k1 = c * k1 - t * b;
if(k1 < 0){
k1 = (b - (b - k1) % b) % b;
}
printf("%lld\n", k1);
}
return 0;
}