//
// main.cpp
// 2. Min-cost GCD (mcg)
//
// Created by KJBS2 on 2014. 12. 24..
// Copyright (c) 2014년 KJBS2. All rights reserved.
//
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
#define MAX_LL ((ll)1*0x7fffffff*0x7fffffff)
ll A, B, P, Q;
ll X, Y, Z, W;
void PROCESS() {
scanf("%lld%lld%lld%lld", &A, &B, &P, &Q);
X = 0; Y = MAX_LL;
for(; A!=0 && B!=0;) {
ll C = A % B, D = A / B;
Z = X + P;
if( (MAX_LL - X) / Q >= D) W = X + D * Q;
else W = MAX_LL;
if(Y != MAX_LL) {
ll temp;
temp = Y + P + P;
Z = min(Z, temp);
if( (MAX_LL - Y) / Q >= D) temp = Y + D * Q;
else temp = MAX_LL;
Z = min(Z, temp);
if( (MAX_LL - Y - P) / Q >= D) temp = Y + P + D * Q;
else temp = MAX_LL;
W = min(W, temp);
}
X = Z;
Y = W;
A = B; B = C;
}
printf("%lld\n", min(X, Y) );
}
int main() {
int T; scanf("%d", &T);
for(int i=0; i<T; i++)
PROCESS();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
1084 KB |
Output is correct |
2 |
Correct |
91 ms |
1084 KB |
Output is correct |
3 |
Correct |
97 ms |
1084 KB |
Output is correct |
4 |
Correct |
93 ms |
1084 KB |
Output is correct |
5 |
Correct |
82 ms |
1084 KB |
Output is correct |
6 |
Correct |
91 ms |
1084 KB |
Output is correct |
7 |
Correct |
87 ms |
1084 KB |
Output is correct |
8 |
Correct |
94 ms |
1084 KB |
Output is correct |
9 |
Correct |
86 ms |
1084 KB |
Output is correct |
10 |
Correct |
91 ms |
1084 KB |
Output is correct |
11 |
Correct |
91 ms |
1084 KB |
Output is correct |
12 |
Correct |
86 ms |
1084 KB |
Output is correct |
13 |
Correct |
90 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1084 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
2 ms |
1084 KB |
Output is correct |
4 |
Correct |
2 ms |
1084 KB |
Output is correct |
5 |
Correct |
0 ms |
1084 KB |
Output is correct |
6 |
Correct |
2 ms |
1084 KB |
Output is correct |
7 |
Correct |
2 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 |
11 |
Correct |
2 ms |
1084 KB |
Output is correct |
12 |
Correct |
0 ms |
1084 KB |
Output is correct |
13 |
Correct |
2 ms |
1084 KB |
Output is correct |
14 |
Correct |
0 ms |
1084 KB |
Output is correct |
15 |
Correct |
1 ms |
1084 KB |
Output is correct |
16 |
Correct |
0 ms |
1084 KB |
Output is correct |
17 |
Correct |
0 ms |
1084 KB |
Output is correct |
18 |
Correct |
1 ms |
1084 KB |
Output is correct |
19 |
Correct |
2 ms |
1084 KB |
Output is correct |
20 |
Correct |
0 ms |
1084 KB |
Output is correct |
21 |
Correct |
2 ms |
1084 KB |
Output is correct |
22 |
Correct |
2 ms |
1084 KB |
Output is correct |
23 |
Correct |
1 ms |
1084 KB |
Output is correct |
24 |
Correct |
0 ms |
1084 KB |
Output is correct |
25 |
Correct |
0 ms |
1084 KB |
Output is correct |
26 |
Correct |
2 ms |
1084 KB |
Output is correct |
27 |
Correct |
0 ms |
1084 KB |
Output is correct |
28 |
Correct |
1 ms |
1084 KB |
Output is correct |
29 |
Correct |
2 ms |
1084 KB |
Output is correct |
30 |
Correct |
2 ms |
1084 KB |
Output is correct |
31 |
Correct |
1 ms |
1084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
263 ms |
1084 KB |
Output is correct |
2 |
Correct |
273 ms |
1084 KB |
Output is correct |
3 |
Correct |
262 ms |
1084 KB |
Output is correct |
4 |
Correct |
271 ms |
1084 KB |
Output is correct |
5 |
Correct |
239 ms |
1084 KB |
Output is correct |
6 |
Correct |
258 ms |
1084 KB |
Output is correct |
7 |
Correct |
253 ms |
1084 KB |
Output is correct |
8 |
Correct |
263 ms |
1084 KB |
Output is correct |
9 |
Correct |
246 ms |
1084 KB |
Output is correct |
10 |
Correct |
263 ms |
1084 KB |
Output is correct |
11 |
Correct |
268 ms |
1084 KB |
Output is correct |
12 |
Correct |
263 ms |
1084 KB |
Output is correct |
13 |
Correct |
271 ms |
1084 KB |
Output is correct |
14 |
Correct |
285 ms |
1084 KB |
Output is correct |
15 |
Correct |
296 ms |
1084 KB |
Output is correct |
16 |
Correct |
291 ms |
1084 KB |
Output is correct |
17 |
Correct |
294 ms |
1084 KB |
Output is correct |
18 |
Correct |
300 ms |
1084 KB |
Output is correct |
19 |
Correct |
139 ms |
1084 KB |
Output is correct |
20 |
Correct |
118 ms |
1084 KB |
Output is correct |
21 |
Correct |
126 ms |
1084 KB |
Output is correct |
22 |
Correct |
133 ms |
1084 KB |
Output is correct |
23 |
Correct |
167 ms |
1084 KB |
Output is correct |
24 |
Correct |
0 ms |
1084 KB |
Output is correct |
25 |
Correct |
135 ms |
1084 KB |
Output is correct |
26 |
Correct |
100 ms |
1084 KB |
Output is correct |
27 |
Correct |
139 ms |
1084 KB |
Output is correct |
28 |
Correct |
301 ms |
1084 KB |
Output is correct |
29 |
Correct |
280 ms |
1084 KB |
Output is correct |
30 |
Correct |
290 ms |
1084 KB |
Output is correct |
31 |
Correct |
283 ms |
1084 KB |
Output is correct |
32 |
Correct |
283 ms |
1084 KB |
Output is correct |
33 |
Correct |
296 ms |
1084 KB |
Output is correct |
34 |
Correct |
283 ms |
1084 KB |
Output is correct |
35 |
Correct |
301 ms |
1084 KB |
Output is correct |