#include <map>
#include <cstdio>
#include <algorithm>
using namespace std;
map<pair<long long, long long>, long long> dp;
long long a, b, p, q;
const long long INF = 1000000ll * 1000000ll * 500000ll;
long long getAns(long long a, long long b)
{
if(a == 0 || b == 0) return 0;
pair<long long, long long> cur = make_pair(a, b);
if(dp.count(cur)) return dp[cur];
long long ret = p + getAns(b, a % b);
if(a >= b)
{
long long howMany = a / b;
if(howMany <= ret / (double)q)
{
long long cand = howMany * q + getAns(a % b, b);
ret = min(cand, ret);
}
}
else
{
long long howMany = b / a;
if(howMany <= ret / (double)q)
{
long long cand = howMany * q + getAns(a, b % a);
ret = min(cand, ret);
}
}
return dp[cur] = ret;
}
int main(void)
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%lld %lld %lld %lld", &a, &b, &p, &q);
dp.clear();
printf("%lld\n", getAns(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 |
119 ms |
1212 KB |
Output is correct |
2 |
Correct |
193 ms |
1212 KB |
Output is correct |
3 |
Correct |
193 ms |
1212 KB |
Output is correct |
4 |
Correct |
186 ms |
1212 KB |
Output is correct |
5 |
Correct |
119 ms |
1212 KB |
Output is correct |
6 |
Correct |
190 ms |
1212 KB |
Output is correct |
7 |
Correct |
182 ms |
1212 KB |
Output is correct |
8 |
Correct |
126 ms |
1212 KB |
Output is correct |
9 |
Correct |
188 ms |
1212 KB |
Output is correct |
10 |
Correct |
147 ms |
1212 KB |
Output is correct |
11 |
Correct |
183 ms |
1212 KB |
Output is correct |
12 |
Correct |
108 ms |
1212 KB |
Output is correct |
13 |
Correct |
170 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 |
0 ms |
1212 KB |
Output is correct |
17 |
Correct |
1 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1212 KB |
Output is correct |
2 |
Correct |
5 ms |
1212 KB |
Output is correct |
3 |
Correct |
6 ms |
1212 KB |
Output is correct |
4 |
Correct |
0 ms |
1212 KB |
Output is correct |
5 |
Correct |
3 ms |
1212 KB |
Output is correct |
6 |
Correct |
3 ms |
1212 KB |
Output is correct |
7 |
Correct |
3 ms |
1212 KB |
Output is correct |
8 |
Correct |
4 ms |
1212 KB |
Output is correct |
9 |
Correct |
6 ms |
1212 KB |
Output is correct |
10 |
Correct |
6 ms |
1212 KB |
Output is correct |
11 |
Correct |
4 ms |
1212 KB |
Output is correct |
12 |
Correct |
0 ms |
1212 KB |
Output is correct |
13 |
Correct |
0 ms |
1212 KB |
Output is correct |
14 |
Correct |
0 ms |
1212 KB |
Output is correct |
15 |
Correct |
2 ms |
1212 KB |
Output is correct |
16 |
Correct |
0 ms |
1212 KB |
Output is correct |
17 |
Correct |
0 ms |
1212 KB |
Output is correct |
18 |
Correct |
2 ms |
1212 KB |
Output is correct |
19 |
Correct |
8 ms |
1212 KB |
Output is correct |
20 |
Correct |
6 ms |
1212 KB |
Output is correct |
21 |
Correct |
5 ms |
1212 KB |
Output is correct |
22 |
Correct |
6 ms |
1212 KB |
Output is correct |
23 |
Correct |
6 ms |
1212 KB |
Output is correct |
24 |
Correct |
7 ms |
1212 KB |
Output is correct |
25 |
Correct |
7 ms |
1212 KB |
Output is correct |
26 |
Correct |
4 ms |
1212 KB |
Output is correct |
27 |
Correct |
5 ms |
1212 KB |
Output is correct |
28 |
Correct |
7 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 |
7 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
863 ms |
1212 KB |
Output is correct |
2 |
Correct |
865 ms |
1212 KB |
Output is correct |
3 |
Correct |
886 ms |
1212 KB |
Output is correct |
4 |
Correct |
861 ms |
1212 KB |
Output is correct |
5 |
Correct |
859 ms |
1212 KB |
Output is correct |
6 |
Correct |
877 ms |
1212 KB |
Output is correct |
7 |
Correct |
874 ms |
1212 KB |
Output is correct |
8 |
Correct |
862 ms |
1212 KB |
Output is correct |
9 |
Correct |
858 ms |
1212 KB |
Output is correct |
10 |
Correct |
871 ms |
1212 KB |
Output is correct |
11 |
Correct |
873 ms |
1212 KB |
Output is correct |
12 |
Correct |
880 ms |
1212 KB |
Output is correct |
13 |
Correct |
872 ms |
1212 KB |
Output is correct |
14 |
Execution timed out |
1000 ms |
1212 KB |
Program timed out |
15 |
Halted |
0 ms |
0 KB |
- |