Submission #13224

# Submission time Handle Problem Language Result Execution time Memory
13224 2015-02-04T07:10:36 Z yourID Min-cost GCD (GA9_mcg) C++14
57 / 100
1000 ms 1212 KB
#pragma warning(disable:4996)
#include<stdio.h>
#include<algorithm>
#include <map>
using namespace std;
long long a, b, P, Q, C1, C2, TC1, TC2;
typedef long long ll;
const ll INF = (ll)1e18 + 5;
 
typedef pair<ll,ll> pll;
map<pll, ll> ans;
 
ll mul (ll a, ll b) {
    if((double)INF / a < b) return INF;
    return a * b;
}
long long  gcd_cost_quite_fast (long long a, long long  b) {
    if(a == 0 || b == 0)return 0;
    if(ans.count(pll(a,b)) == 1) return ans[pll(a,b)];
     
    long long ret = 0;
     
    if(a >= b) {
        //correct
        ret = gcd_cost_quite_fast(b, a%b) + P;
        ret = min(ret, gcd_cost_quite_fast(a%b, b) + mul(Q, a/b));
    }else {
        ret = gcd_cost_quite_fast(a, b%a) + min(P+P, mul(Q, (b/a)));
    }
     
    return ans[pll(a,b)] = ret;
}
 
int main()
{
    int T;
    scanf("%d", &T);
    while (T--){
        scanf("%lld%lld", &a, &b);
        scanf("%lld%lld", &P, &Q);
        ans.clear();
        printf("%lld\n",gcd_cost_quite_fast(a, b));
    }
}
# Verdict Execution time Memory 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
# Verdict Execution time Memory Grader output
1 Correct 176 ms 1212 KB Output is correct
2 Correct 168 ms 1212 KB Output is correct
3 Correct 162 ms 1212 KB Output is correct
4 Correct 171 ms 1212 KB Output is correct
5 Correct 167 ms 1212 KB Output is correct
6 Correct 172 ms 1212 KB Output is correct
7 Correct 174 ms 1212 KB Output is correct
8 Correct 167 ms 1212 KB Output is correct
9 Correct 185 ms 1212 KB Output is correct
10 Correct 175 ms 1212 KB Output is correct
11 Correct 177 ms 1212 KB Output is correct
12 Correct 171 ms 1212 KB Output is correct
13 Correct 163 ms 1212 KB Output is correct
14 Correct 1 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
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1212 KB Output is correct
2 Correct 5 ms 1212 KB Output is correct
3 Correct 3 ms 1212 KB Output is correct
4 Correct 4 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 5 ms 1212 KB Output is correct
10 Correct 3 ms 1212 KB Output is correct
11 Correct 5 ms 1212 KB Output is correct
12 Correct 4 ms 1212 KB Output is correct
13 Correct 5 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 1 ms 1212 KB Output is correct
19 Correct 6 ms 1212 KB Output is correct
20 Correct 5 ms 1212 KB Output is correct
21 Correct 3 ms 1212 KB Output is correct
22 Correct 4 ms 1212 KB Output is correct
23 Correct 6 ms 1212 KB Output is correct
24 Correct 6 ms 1212 KB Output is correct
25 Correct 6 ms 1212 KB Output is correct
26 Correct 4 ms 1212 KB Output is correct
27 Correct 6 ms 1212 KB Output is correct
28 Correct 4 ms 1212 KB Output is correct
29 Correct 5 ms 1212 KB Output is correct
30 Correct 5 ms 1212 KB Output is correct
31 Correct 4 ms 1212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 783 ms 1212 KB Output is correct
2 Correct 790 ms 1212 KB Output is correct
3 Correct 805 ms 1212 KB Output is correct
4 Correct 799 ms 1212 KB Output is correct
5 Correct 806 ms 1212 KB Output is correct
6 Correct 803 ms 1212 KB Output is correct
7 Correct 806 ms 1212 KB Output is correct
8 Correct 808 ms 1212 KB Output is correct
9 Correct 801 ms 1212 KB Output is correct
10 Correct 817 ms 1212 KB Output is correct
11 Correct 810 ms 1212 KB Output is correct
12 Correct 806 ms 1212 KB Output is correct
13 Correct 788 ms 1212 KB Output is correct
14 Execution timed out 1000 ms 1212 KB Program timed out
15 Execution timed out 1000 ms 1212 KB Program timed out
16 Execution timed out 1000 ms 1212 KB Program timed out
17 Correct 991 ms 1212 KB Output is correct
18 Execution timed out 1000 ms 1212 KB Program timed out
19 Correct 246 ms 1212 KB Output is correct
20 Correct 232 ms 1212 KB Output is correct
21 Correct 260 ms 1212 KB Output is correct
22 Correct 244 ms 1212 KB Output is correct
23 Correct 319 ms 1212 KB Output is correct
24 Correct 324 ms 1212 KB Output is correct
25 Correct 320 ms 1212 KB Output is correct
26 Correct 319 ms 1212 KB Output is correct
27 Correct 345 ms 1212 KB Output is correct
28 Execution timed out 1000 ms 1212 KB Program timed out
29 Correct 984 ms 1212 KB Output is correct
30 Execution timed out 1000 ms 1212 KB Program timed out
31 Correct 946 ms 1212 KB Output is correct
32 Correct 974 ms 1212 KB Output is correct
33 Execution timed out 1000 ms 1212 KB Program timed out
34 Correct 970 ms 1212 KB Output is correct
35 Execution timed out 1000 ms 1212 KB Program timed out