# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
12125 |
2014-12-21T07:36:45 Z |
gs14004 |
Min-cost GCD (GA9_mcg) |
C++ |
|
1000 ms |
1324 KB |
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long lint;
typedef pair<lint,lint> pi;
map<pi,lint> mp;
lint a,b,p,q;
bool calc_valid(lint x, lint y){
double t = log10(x)+log10(y);
return t < 18;
}
lint f(lint x, lint y){
if(x == 0 || y == 0) return 0;
if(mp.find(pi(x,y)) != mp.end()) return mp[pi(x,y)];
if(x >= y){
if(!calc_valid(q,x/y)) return mp[pi(x,y)] = f(y,x%y) + p;
else return mp[pi(x,y)] = min(f(y,x%y) + p,f(x%y,y) + q*(x/y));
}
else{
if(!calc_valid(q,y/x)) return 5e18;
else return f(x,y%x) + q * (y/x);
}
}
void solve(){
mp.clear();
scanf("%lld %lld %lld %lld",&a,&b,&p,&q);
if(a < b) printf("%lld\n",min(p + f(b,a),f(a,b)));
else printf("%lld\n",f(a,b));
}
int main(){
int t;
scanf("%d",&t);
while (t--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1324 KB |
Output is correct |
2 |
Correct |
0 ms |
1324 KB |
Output is correct |
3 |
Correct |
0 ms |
1324 KB |
Output is correct |
4 |
Correct |
0 ms |
1324 KB |
Output is correct |
5 |
Correct |
0 ms |
1324 KB |
Output is correct |
6 |
Correct |
0 ms |
1324 KB |
Output is correct |
7 |
Correct |
0 ms |
1324 KB |
Output is correct |
8 |
Correct |
0 ms |
1324 KB |
Output is correct |
9 |
Correct |
0 ms |
1324 KB |
Output is correct |
10 |
Correct |
0 ms |
1324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
264 ms |
1324 KB |
Output is correct |
2 |
Correct |
268 ms |
1324 KB |
Output is correct |
3 |
Correct |
270 ms |
1324 KB |
Output is correct |
4 |
Correct |
279 ms |
1324 KB |
Output is correct |
5 |
Correct |
262 ms |
1324 KB |
Output is correct |
6 |
Correct |
278 ms |
1324 KB |
Output is correct |
7 |
Correct |
274 ms |
1324 KB |
Output is correct |
8 |
Correct |
271 ms |
1324 KB |
Output is correct |
9 |
Correct |
271 ms |
1324 KB |
Output is correct |
10 |
Correct |
268 ms |
1324 KB |
Output is correct |
11 |
Correct |
283 ms |
1324 KB |
Output is correct |
12 |
Correct |
276 ms |
1324 KB |
Output is correct |
13 |
Correct |
285 ms |
1324 KB |
Output is correct |
14 |
Correct |
4 ms |
1324 KB |
Output is correct |
15 |
Correct |
2 ms |
1324 KB |
Output is correct |
16 |
Correct |
2 ms |
1324 KB |
Output is correct |
17 |
Correct |
3 ms |
1324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1324 KB |
Output is correct |
2 |
Correct |
8 ms |
1324 KB |
Output is correct |
3 |
Correct |
8 ms |
1324 KB |
Output is correct |
4 |
Correct |
7 ms |
1324 KB |
Output is correct |
5 |
Correct |
8 ms |
1324 KB |
Output is correct |
6 |
Correct |
8 ms |
1324 KB |
Output is correct |
7 |
Correct |
5 ms |
1324 KB |
Output is correct |
8 |
Correct |
8 ms |
1324 KB |
Output is correct |
9 |
Correct |
8 ms |
1324 KB |
Output is correct |
10 |
Correct |
8 ms |
1324 KB |
Output is correct |
11 |
Correct |
7 ms |
1324 KB |
Output is correct |
12 |
Correct |
5 ms |
1324 KB |
Output is correct |
13 |
Correct |
5 ms |
1324 KB |
Output is correct |
14 |
Correct |
3 ms |
1324 KB |
Output is correct |
15 |
Correct |
3 ms |
1324 KB |
Output is correct |
16 |
Correct |
2 ms |
1324 KB |
Output is correct |
17 |
Correct |
0 ms |
1324 KB |
Output is correct |
18 |
Correct |
5 ms |
1324 KB |
Output is correct |
19 |
Correct |
8 ms |
1324 KB |
Output is correct |
20 |
Correct |
10 ms |
1324 KB |
Output is correct |
21 |
Correct |
8 ms |
1324 KB |
Output is correct |
22 |
Correct |
10 ms |
1324 KB |
Output is correct |
23 |
Correct |
3 ms |
1324 KB |
Output is correct |
24 |
Correct |
9 ms |
1324 KB |
Output is correct |
25 |
Correct |
7 ms |
1324 KB |
Output is correct |
26 |
Correct |
9 ms |
1324 KB |
Output is correct |
27 |
Correct |
7 ms |
1324 KB |
Output is correct |
28 |
Correct |
9 ms |
1324 KB |
Output is correct |
29 |
Correct |
6 ms |
1324 KB |
Output is correct |
30 |
Correct |
9 ms |
1324 KB |
Output is correct |
31 |
Correct |
7 ms |
1324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1000 ms |
1324 KB |
Program timed out |
2 |
Halted |
0 ms |
0 KB |
- |