-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1297.cpp
64 lines (53 loc) · 825 Bytes
/
1297.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
#include <iostream>
using namespace std;
typedef long long L;
L gcd(L a, L b)
{
if (b == 0) {
return a;
}
return gcd(b, a%b);
}
L extend_gcd(L a, L b, L &x, L &y)
{
if (a % b ==0) {
x = 0;
y = 1;
return b;
}
L tx, ty;
L c = extend_gcd(b, a%b, tx, ty);
x = ty;
y = tx - (a/b)*ty;
return c;
}
L solve(L s1, L s2, L v1, L v2, L m)
{
long long a = v1 - v2;
long long b = m;
long long c = s2 - s1;
if (a < 0) {
a += m;
}
long long d = gcd(a, b);
if (c%d) {
return -1;
}
a = a/d;
b = b/d;
c = c/d;
L x, y;
extend_gcd(a, b, x, y);
x = (x*c) % b;
while (x < 0) {
x += b;
}
return x;
}
int main()
{
long long s1, s2, v1, v2, m;
cin >> s1 >> s2 >> v1 >> v2 >> m;
cout << solve(s1, s2, v1, v2, m) << endl;
return 0;
}