#pragma warning(disable:4996)
#include<stdio.h>
#include<algorithm>
#include <map>
using namespace std;
long long a, b, P, Q, C1, C2, TC1, TC2;
typedef long long ll;
const ll INF = (ll)1e18 + 5;
typedef pair<ll,ll> pll;
map<pll, ll> ans;
ll mul (ll a, ll b) {
if((double)INF / a < b) return INF;
return a * b;
}
long long gcd_cost_quite_fast (long long a, long long b) {
if(a == 0 || b == 0)return 0;
if(ans.count(pll(a,b)) == 1) return ans[pll(a,b)];
long long ret = 0;
if(a >= b) {
//correct
ret = gcd_cost_quite_fast(b, a%b) + P;
ret = min(ret, gcd_cost_quite_fast(a%b, b) + mul(Q, a/b));
}else {
ret = gcd_cost_quite_fast(a, b%a) + min(P+P, mul(Q, (b/a)));
}
return ans[pll(a,b)] = ret;
}
int main()
{
int T;
scanf("%d", &T);
while (T--){
scanf("%lld%lld", &a, &b);
scanf("%lld%lld", &P, &Q);
ans.clear();
printf("%lld\n",gcd_cost_quite_fast(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 |
161 ms |
1212 KB |
Output is correct |
2 |
Correct |
169 ms |
1212 KB |
Output is correct |
3 |
Correct |
171 ms |
1212 KB |
Output is correct |
4 |
Correct |
168 ms |
1212 KB |
Output is correct |
5 |
Correct |
171 ms |
1212 KB |
Output is correct |
6 |
Correct |
170 ms |
1212 KB |
Output is correct |
7 |
Correct |
171 ms |
1212 KB |
Output is correct |
8 |
Correct |
172 ms |
1212 KB |
Output is correct |
9 |
Correct |
174 ms |
1212 KB |
Output is correct |
10 |
Correct |
174 ms |
1212 KB |
Output is correct |
11 |
Correct |
174 ms |
1212 KB |
Output is correct |
12 |
Correct |
170 ms |
1212 KB |
Output is correct |
13 |
Correct |
167 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 |
2 ms |
1212 KB |
Output is correct |
17 |
Correct |
0 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1212 KB |
Output is correct |
2 |
Correct |
3 ms |
1212 KB |
Output is correct |
3 |
Correct |
2 ms |
1212 KB |
Output is correct |
4 |
Correct |
4 ms |
1212 KB |
Output is correct |
5 |
Correct |
5 ms |
1212 KB |
Output is correct |
6 |
Correct |
6 ms |
1212 KB |
Output is correct |
7 |
Correct |
5 ms |
1212 KB |
Output is correct |
8 |
Correct |
3 ms |
1212 KB |
Output is correct |
9 |
Correct |
5 ms |
1212 KB |
Output is correct |
10 |
Correct |
3 ms |
1212 KB |
Output is correct |
11 |
Correct |
5 ms |
1212 KB |
Output is correct |
12 |
Correct |
4 ms |
1212 KB |
Output is correct |
13 |
Correct |
5 ms |
1212 KB |
Output is correct |
14 |
Correct |
0 ms |
1212 KB |
Output is correct |
15 |
Correct |
1 ms |
1212 KB |
Output is correct |
16 |
Correct |
3 ms |
1212 KB |
Output is correct |
17 |
Correct |
1 ms |
1212 KB |
Output is correct |
18 |
Correct |
2 ms |
1212 KB |
Output is correct |
19 |
Correct |
3 ms |
1212 KB |
Output is correct |
20 |
Correct |
6 ms |
1212 KB |
Output is correct |
21 |
Correct |
4 ms |
1212 KB |
Output is correct |
22 |
Correct |
7 ms |
1212 KB |
Output is correct |
23 |
Correct |
6 ms |
1212 KB |
Output is correct |
24 |
Correct |
3 ms |
1212 KB |
Output is correct |
25 |
Correct |
6 ms |
1212 KB |
Output is correct |
26 |
Correct |
3 ms |
1212 KB |
Output is correct |
27 |
Correct |
5 ms |
1212 KB |
Output is correct |
28 |
Correct |
6 ms |
1212 KB |
Output is correct |
29 |
Correct |
6 ms |
1212 KB |
Output is correct |
30 |
Correct |
5 ms |
1212 KB |
Output is correct |
31 |
Correct |
6 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
796 ms |
1212 KB |
Output is correct |
2 |
Correct |
801 ms |
1212 KB |
Output is correct |
3 |
Correct |
831 ms |
1212 KB |
Output is correct |
4 |
Correct |
791 ms |
1212 KB |
Output is correct |
5 |
Correct |
793 ms |
1212 KB |
Output is correct |
6 |
Correct |
800 ms |
1212 KB |
Output is correct |
7 |
Correct |
798 ms |
1212 KB |
Output is correct |
8 |
Correct |
784 ms |
1212 KB |
Output is correct |
9 |
Correct |
808 ms |
1212 KB |
Output is correct |
10 |
Correct |
792 ms |
1212 KB |
Output is correct |
11 |
Correct |
790 ms |
1212 KB |
Output is correct |
12 |
Correct |
782 ms |
1212 KB |
Output is correct |
13 |
Correct |
795 ms |
1212 KB |
Output is correct |
14 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |
15 |
Correct |
991 ms |
1212 KB |
Output is correct |
16 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |
17 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |
18 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |
19 |
Correct |
242 ms |
1212 KB |
Output is correct |
20 |
Correct |
253 ms |
1212 KB |
Output is correct |
21 |
Correct |
246 ms |
1212 KB |
Output is correct |
22 |
Correct |
244 ms |
1212 KB |
Output is correct |
23 |
Correct |
320 ms |
1212 KB |
Output is correct |
24 |
Correct |
330 ms |
1212 KB |
Output is correct |
25 |
Correct |
311 ms |
1212 KB |
Output is correct |
26 |
Correct |
310 ms |
1212 KB |
Output is correct |
27 |
Correct |
333 ms |
1212 KB |
Output is correct |
28 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |
29 |
Correct |
976 ms |
1212 KB |
Output is correct |
30 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |
31 |
Correct |
912 ms |
1212 KB |
Output is correct |
32 |
Correct |
961 ms |
1212 KB |
Output is correct |
33 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |
34 |
Correct |
979 ms |
1212 KB |
Output is correct |
35 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |