#include <stdio.h>
#include <algorithm>
#include <map>
using namespace std;
long long a,b,p,q;
std::map<pair<long long, long long>, long long> chk;
long long go(long long a, long long b)
{
if (a == 0 || b == 0) return 0;
if (a == b) return p < q ? p : q;
if (chk.count(make_pair(a,b))) return chk[make_pair(a,b)];
long long &r = chk[make_pair(a,b)];
if (a > b){
long long d = a / b;
r = p + go(b,a%b);
r = min(r, q * d + go(a-b*d,b));
}
else{
long long d = b / a;
r = p * 2 + go(a,b%a);
r = min(r, q * d + go(a,b-a*d));
}
return r;
}
int main()
{
int T; scanf ("%d",&T); while (T--){
scanf ("%lld %lld %lld %lld",&a,&b,&p,&q);
chk.clear();
printf ("%lld\n",go(a,b));
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
176 ms |
1212 KB |
Output is correct |
2 |
Correct |
186 ms |
1212 KB |
Output is correct |
3 |
Correct |
181 ms |
1212 KB |
Output is correct |
4 |
Correct |
185 ms |
1212 KB |
Output is correct |
5 |
Correct |
188 ms |
1212 KB |
Output is correct |
6 |
Correct |
180 ms |
1212 KB |
Output is correct |
7 |
Correct |
182 ms |
1212 KB |
Output is correct |
8 |
Correct |
167 ms |
1212 KB |
Output is correct |
9 |
Correct |
182 ms |
1212 KB |
Output is correct |
10 |
Correct |
181 ms |
1212 KB |
Output is correct |
11 |
Correct |
189 ms |
1212 KB |
Output is correct |
12 |
Correct |
185 ms |
1212 KB |
Output is correct |
13 |
Correct |
187 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 |
2 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1212 KB |
Output is correct |
2 |
Incorrect |
5 ms |
1212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
902 ms |
1212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |