# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
12164 |
2014-12-21T12:53:36 Z |
xhae |
Min-cost GCD (GA9_mcg) |
C++14 |
|
677 ms |
1212 KB |
#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(q < p)
{
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
1212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
1212 KB |
Output is correct |
2 |
Incorrect |
116 ms |
1212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
1212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
677 ms |
1212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |