#include<stdio.h>
#define min(a,b) (a<b?a:b)
long long a, b, p, q, Q[250], DP[250][2];
int T, cost;
int main(){
int x, y, r;
int i, s;
scanf("%lld", &T);
while(T--){
scanf("%lld %lld %lld %lld", &a, &b, &p, &q);
x=(a>b?a:b);
y=(a>b?b:a);
i=0;
do{
r=x%y;
Q[i]=x/y;
x=y;
y=r;
i++;
}while(r);
s=i;
if(a>=b){
DP[0][0]=0;
DP[0][1]=1000000000000000000;
}
else{
DP[0][0]=p;
DP[0][1]=0;
}
for(i=0; i<s; i++){
DP[i+1][0]=min(DP[i][0]+p, DP[i][1]+Q[i]*q);
DP[i+1][1]=DP[i][0]+Q[i]*q;
}
printf("%lld\n", min(DP[s][0], DP[s][1]));
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1088 KB |
Output is correct |
2 |
Correct |
0 ms |
1088 KB |
Output is correct |
3 |
Correct |
0 ms |
1088 KB |
Output is correct |
4 |
Correct |
0 ms |
1088 KB |
Output is correct |
5 |
Correct |
0 ms |
1088 KB |
Output is correct |
6 |
Correct |
0 ms |
1088 KB |
Output is correct |
7 |
Correct |
0 ms |
1088 KB |
Output is correct |
8 |
Correct |
0 ms |
1088 KB |
Output is correct |
9 |
Correct |
0 ms |
1088 KB |
Output is correct |
10 |
Correct |
0 ms |
1088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
1088 KB |
Output is correct |
2 |
Correct |
69 ms |
1088 KB |
Output is correct |
3 |
Correct |
70 ms |
1088 KB |
Output is correct |
4 |
Correct |
63 ms |
1088 KB |
Output is correct |
5 |
Correct |
0 ms |
1088 KB |
Output is correct |
6 |
Correct |
63 ms |
1088 KB |
Output is correct |
7 |
Correct |
67 ms |
1088 KB |
Output is correct |
8 |
Correct |
67 ms |
1088 KB |
Output is correct |
9 |
Correct |
55 ms |
1088 KB |
Output is correct |
10 |
Correct |
62 ms |
1088 KB |
Output is correct |
11 |
Correct |
64 ms |
1088 KB |
Output is correct |
12 |
Correct |
61 ms |
1088 KB |
Output is correct |
13 |
Correct |
63 ms |
1088 KB |
Output is correct |
14 |
Correct |
0 ms |
1088 KB |
Output is correct |
15 |
Correct |
0 ms |
1088 KB |
Output is correct |
16 |
Correct |
0 ms |
1088 KB |
Output is correct |
17 |
Correct |
0 ms |
1088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1088 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1088 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
150 ms |
1088 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |