#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <deque>
#include <utility>
#include <bitset>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const ll INF = (ll)1e18 + 5;
ll mul (ll a, ll b) {
if(a >= INF || b >= INF) return INF;
if((double)INF / a < b) return INF;
return a * b;
}
pll solve (ll a, ll b, ll p, ll q) {
if(a * b == 0) return pll(0, 0);
pll last = solve(b, a%b, p, q);
// (b,a) = min((a,b)+p, (b,a%b)
// b <= a
if(a >= b) {
ll t = min(last.first + p, last.second + mul(q, a/b));
ll v = min(t + p, last.first + mul(q, a/b));
return pll(t, v);
}else {
return pll(last.second, last.first);
}
}
int main() {
int TC; scanf("%d", &TC);
while(TC--) {
ll a, b, p, q; scanf("%lld%lld%lld%lld", &a, &b, &p, &q);
printf("%lld\n", solve(a, b, p, q).first);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1084 KB |
Output is correct |
2 |
Correct |
0 ms |
1084 KB |
Output is correct |
3 |
Correct |
0 ms |
1084 KB |
Output is correct |
4 |
Correct |
0 ms |
1084 KB |
Output is correct |
5 |
Correct |
0 ms |
1084 KB |
Output is correct |
6 |
Correct |
0 ms |
1084 KB |
Output is correct |
7 |
Correct |
0 ms |
1084 KB |
Output is correct |
8 |
Correct |
0 ms |
1084 KB |
Output is correct |
9 |
Correct |
0 ms |
1084 KB |
Output is correct |
10 |
Correct |
0 ms |
1084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
1084 KB |
Output is correct |
2 |
Correct |
77 ms |
1084 KB |
Output is correct |
3 |
Correct |
79 ms |
1084 KB |
Output is correct |
4 |
Correct |
73 ms |
1084 KB |
Output is correct |
5 |
Correct |
70 ms |
1084 KB |
Output is correct |
6 |
Correct |
70 ms |
1084 KB |
Output is correct |
7 |
Correct |
77 ms |
1084 KB |
Output is correct |
8 |
Correct |
73 ms |
1084 KB |
Output is correct |
9 |
Correct |
72 ms |
1084 KB |
Output is correct |
10 |
Correct |
73 ms |
1084 KB |
Output is correct |
11 |
Correct |
74 ms |
1084 KB |
Output is correct |
12 |
Correct |
74 ms |
1084 KB |
Output is correct |
13 |
Correct |
74 ms |
1084 KB |
Output is correct |
14 |
Correct |
1 ms |
1084 KB |
Output is correct |
15 |
Correct |
0 ms |
1084 KB |
Output is correct |
16 |
Correct |
0 ms |
1084 KB |
Output is correct |
17 |
Correct |
0 ms |
1084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1084 KB |
Output is correct |
2 |
Correct |
0 ms |
1084 KB |
Output is correct |
3 |
Correct |
0 ms |
1084 KB |
Output is correct |
4 |
Correct |
1 ms |
1084 KB |
Output is correct |
5 |
Correct |
1 ms |
1084 KB |
Output is correct |
6 |
Correct |
0 ms |
1084 KB |
Output is correct |
7 |
Correct |
3 ms |
1084 KB |
Output is correct |
8 |
Correct |
0 ms |
1084 KB |
Output is correct |
9 |
Correct |
1 ms |
1084 KB |
Output is correct |
10 |
Correct |
0 ms |
1084 KB |
Output is correct |
11 |
Correct |
1 ms |
1084 KB |
Output is correct |
12 |
Correct |
0 ms |
1084 KB |
Output is correct |
13 |
Correct |
0 ms |
1084 KB |
Output is correct |
14 |
Correct |
1 ms |
1084 KB |
Output is correct |
15 |
Correct |
0 ms |
1084 KB |
Output is correct |
16 |
Correct |
1 ms |
1084 KB |
Output is correct |
17 |
Correct |
0 ms |
1084 KB |
Output is correct |
18 |
Correct |
0 ms |
1084 KB |
Output is correct |
19 |
Correct |
0 ms |
1084 KB |
Output is correct |
20 |
Correct |
0 ms |
1084 KB |
Output is correct |
21 |
Correct |
0 ms |
1084 KB |
Output is correct |
22 |
Correct |
1 ms |
1084 KB |
Output is correct |
23 |
Correct |
0 ms |
1084 KB |
Output is correct |
24 |
Correct |
0 ms |
1084 KB |
Output is correct |
25 |
Correct |
1 ms |
1084 KB |
Output is correct |
26 |
Correct |
0 ms |
1084 KB |
Output is correct |
27 |
Correct |
0 ms |
1084 KB |
Output is correct |
28 |
Correct |
0 ms |
1084 KB |
Output is correct |
29 |
Correct |
0 ms |
1084 KB |
Output is correct |
30 |
Correct |
1 ms |
1084 KB |
Output is correct |
31 |
Correct |
0 ms |
1084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
188 ms |
1084 KB |
Output is correct |
2 |
Correct |
0 ms |
1084 KB |
Output is correct |
3 |
Correct |
188 ms |
1084 KB |
Output is correct |
4 |
Correct |
186 ms |
1084 KB |
Output is correct |
5 |
Correct |
195 ms |
1084 KB |
Output is correct |
6 |
Correct |
186 ms |
1084 KB |
Output is correct |
7 |
Correct |
188 ms |
1084 KB |
Output is correct |
8 |
Correct |
176 ms |
1084 KB |
Output is correct |
9 |
Correct |
125 ms |
1084 KB |
Output is correct |
10 |
Correct |
190 ms |
1084 KB |
Output is correct |
11 |
Correct |
196 ms |
1084 KB |
Output is correct |
12 |
Correct |
178 ms |
1084 KB |
Output is correct |
13 |
Correct |
177 ms |
1084 KB |
Output is correct |
14 |
Correct |
202 ms |
1084 KB |
Output is correct |
15 |
Correct |
210 ms |
1084 KB |
Output is correct |
16 |
Correct |
195 ms |
1084 KB |
Output is correct |
17 |
Correct |
204 ms |
1084 KB |
Output is correct |
18 |
Correct |
209 ms |
1084 KB |
Output is correct |
19 |
Correct |
120 ms |
1084 KB |
Output is correct |
20 |
Correct |
113 ms |
1084 KB |
Output is correct |
21 |
Correct |
110 ms |
1084 KB |
Output is correct |
22 |
Correct |
122 ms |
1084 KB |
Output is correct |
23 |
Correct |
122 ms |
1084 KB |
Output is correct |
24 |
Correct |
113 ms |
1084 KB |
Output is correct |
25 |
Correct |
135 ms |
1084 KB |
Output is correct |
26 |
Correct |
112 ms |
1084 KB |
Output is correct |
27 |
Correct |
118 ms |
1084 KB |
Output is correct |
28 |
Correct |
198 ms |
1084 KB |
Output is correct |
29 |
Correct |
193 ms |
1084 KB |
Output is correct |
30 |
Correct |
208 ms |
1084 KB |
Output is correct |
31 |
Correct |
206 ms |
1084 KB |
Output is correct |
32 |
Correct |
203 ms |
1084 KB |
Output is correct |
33 |
Correct |
208 ms |
1084 KB |
Output is correct |
34 |
Correct |
121 ms |
1084 KB |
Output is correct |
35 |
Correct |
198 ms |
1084 KB |
Output is correct |