#pragma warning(disable:4996)
#include<stdio.h>
#include<algorithm>
#define INF 999999999999999999LL
using namespace std;
long long a, b, sub, mod, C1, C2, TC1, TC2;
long long Mul(long long a, long long b){
if (a && INF / a > b)return INF;
return a*b;
}
int main()
{
int T;
scanf("%d", &T);
while (T--){
scanf("%lld%lld", &a, &b);
scanf("%lld%lld", &mod, &sub);
C1 = 0, C2 = INF;
while (a && b){
if (a == b){
C1 = C1 + min(sub, mod);
C2 = C2 + min(sub, mod);
a -= b;
continue;
}
if (a < b){
if (C2 > C1 + mod)C2 = C1 + mod;
TC1 = min(C2 + mod, C1 + Mul(b / a, sub));
TC2 = min(C2 + Mul(b / a,sub), TC1 + mod);
b = b%a;
}
else{
if (C1 > C2 + mod)C1 = C2 + mod;
TC2 = min(C1 + mod, C2 + Mul(a / b, sub));
TC1 = min(C1 + Mul(a / b,sub), TC2 + mod);
a = a%b;
}
C1 = TC1, C2 = TC2;
}
printf("%lld\n", min(C1, C2));
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
1084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
1084 KB |
Output is correct |
2 |
Incorrect |
84 ms |
1084 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
243 ms |
1084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |