#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a, b, p, q;
map <pair<ll, ll>, ll> d;
ll f(ll x, ll y) {
if (x == 0 || y == 0) return 0;
if (d.count({ x, y })) return d[{x, y}];
ll d1 = f(y, x%y) + p, d2 = 1e18;
if (x >= y) {
if ((x / y) <= d1 / q) d2 = f(x % y, y) + (x / y)*q;
}
else {
if ((y / x) <= d1 / q) d2 = f(x, y % x) + (y / x)*q;
}
if (d1 > d2) d1 = d2;
d[{x, y}] = d1;
return d1;
}
int main() {
int T;
for (scanf("%d", &T); T--; ) {
scanf("%lld%lld%lld%lld", &a, &b, &p, &q);
d.clear();
printf("%lld\n", f(a, b));
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1212 KB |
Output is correct |
2 |
Correct |
0 ms |
1212 KB |
Output is correct |
3 |
Correct |
0 ms |
1212 KB |
Output is correct |
4 |
Correct |
0 ms |
1212 KB |
Output is correct |
5 |
Correct |
0 ms |
1212 KB |
Output is correct |
6 |
Correct |
0 ms |
1212 KB |
Output is correct |
7 |
Correct |
0 ms |
1212 KB |
Output is correct |
8 |
Correct |
0 ms |
1212 KB |
Output is correct |
9 |
Correct |
0 ms |
1212 KB |
Output is correct |
10 |
Correct |
0 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
1212 KB |
Output is correct |
2 |
Correct |
193 ms |
1212 KB |
Output is correct |
3 |
Correct |
204 ms |
1212 KB |
Output is correct |
4 |
Correct |
213 ms |
1212 KB |
Output is correct |
5 |
Correct |
127 ms |
1212 KB |
Output is correct |
6 |
Correct |
205 ms |
1212 KB |
Output is correct |
7 |
Correct |
210 ms |
1212 KB |
Output is correct |
8 |
Correct |
145 ms |
1212 KB |
Output is correct |
9 |
Correct |
191 ms |
1212 KB |
Output is correct |
10 |
Correct |
152 ms |
1212 KB |
Output is correct |
11 |
Correct |
193 ms |
1212 KB |
Output is correct |
12 |
Correct |
139 ms |
1212 KB |
Output is correct |
13 |
Correct |
159 ms |
1212 KB |
Output is correct |
14 |
Correct |
0 ms |
1212 KB |
Output is correct |
15 |
Correct |
2 ms |
1212 KB |
Output is correct |
16 |
Correct |
2 ms |
1212 KB |
Output is correct |
17 |
Correct |
0 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1212 KB |
Output is correct |
2 |
Correct |
6 ms |
1212 KB |
Output is correct |
3 |
Correct |
3 ms |
1212 KB |
Output is correct |
4 |
Correct |
0 ms |
1212 KB |
Output is correct |
5 |
Correct |
5 ms |
1212 KB |
Output is correct |
6 |
Correct |
5 ms |
1212 KB |
Output is correct |
7 |
Correct |
5 ms |
1212 KB |
Output is correct |
8 |
Correct |
5 ms |
1212 KB |
Output is correct |
9 |
Correct |
6 ms |
1212 KB |
Output is correct |
10 |
Correct |
6 ms |
1212 KB |
Output is correct |
11 |
Correct |
6 ms |
1212 KB |
Output is correct |
12 |
Correct |
6 ms |
1212 KB |
Output is correct |
13 |
Correct |
4 ms |
1212 KB |
Output is correct |
14 |
Correct |
0 ms |
1212 KB |
Output is correct |
15 |
Correct |
0 ms |
1212 KB |
Output is correct |
16 |
Correct |
0 ms |
1212 KB |
Output is correct |
17 |
Correct |
0 ms |
1212 KB |
Output is correct |
18 |
Correct |
0 ms |
1212 KB |
Output is correct |
19 |
Correct |
7 ms |
1212 KB |
Output is correct |
20 |
Correct |
7 ms |
1212 KB |
Output is correct |
21 |
Correct |
4 ms |
1212 KB |
Output is correct |
22 |
Correct |
6 ms |
1212 KB |
Output is correct |
23 |
Correct |
7 ms |
1212 KB |
Output is correct |
24 |
Correct |
7 ms |
1212 KB |
Output is correct |
25 |
Correct |
5 ms |
1212 KB |
Output is correct |
26 |
Correct |
7 ms |
1212 KB |
Output is correct |
27 |
Correct |
7 ms |
1212 KB |
Output is correct |
28 |
Correct |
8 ms |
1212 KB |
Output is correct |
29 |
Correct |
7 ms |
1212 KB |
Output is correct |
30 |
Correct |
6 ms |
1212 KB |
Output is correct |
31 |
Correct |
5 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
916 ms |
1212 KB |
Output is correct |
2 |
Correct |
929 ms |
1212 KB |
Output is correct |
3 |
Correct |
920 ms |
1212 KB |
Output is correct |
4 |
Correct |
919 ms |
1212 KB |
Output is correct |
5 |
Correct |
922 ms |
1212 KB |
Output is correct |
6 |
Correct |
924 ms |
1212 KB |
Output is correct |
7 |
Correct |
920 ms |
1212 KB |
Output is correct |
8 |
Correct |
911 ms |
1212 KB |
Output is correct |
9 |
Correct |
904 ms |
1212 KB |
Output is correct |
10 |
Correct |
910 ms |
1212 KB |
Output is correct |
11 |
Correct |
896 ms |
1212 KB |
Output is correct |
12 |
Correct |
899 ms |
1212 KB |
Output is correct |
13 |
Correct |
924 ms |
1212 KB |
Output is correct |
14 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |
15 |
Halted |
0 ms |
0 KB |
- |