Submission #13225

# Submission time Handle Problem Language Result Execution time Memory
13225 2015-02-04T07:27:52 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 174 ms 1212 KB Output is correct
2 Correct 169 ms 1212 KB Output is correct
3 Correct 161 ms 1212 KB Output is correct
4 Correct 162 ms 1212 KB Output is correct
5 Correct 168 ms 1212 KB Output is correct
6 Correct 164 ms 1212 KB Output is correct
7 Correct 170 ms 1212 KB Output is correct
8 Correct 171 ms 1212 KB Output is correct
9 Correct 169 ms 1212 KB Output is correct
10 Correct 167 ms 1212 KB Output is correct
11 Correct 171 ms 1212 KB Output is correct
12 Correct 160 ms 1212 KB Output is correct
13 Correct 172 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 1 ms 1212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1212 KB Output is correct
2 Correct 3 ms 1212 KB Output is correct
3 Correct 4 ms 1212 KB Output is correct
4 Correct 3 ms 1212 KB Output is correct
5 Correct 5 ms 1212 KB Output is correct
6 Correct 4 ms 1212 KB Output is correct
7 Correct 0 ms 1212 KB Output is correct
8 Correct 5 ms 1212 KB Output is correct
9 Correct 4 ms 1212 KB Output is correct
10 Correct 5 ms 1212 KB Output is correct
11 Correct 4 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 1 ms 1212 KB Output is correct
15 Correct 2 ms 1212 KB Output is correct
16 Correct 2 ms 1212 KB Output is correct
17 Correct 2 ms 1212 KB Output is correct
18 Correct 1 ms 1212 KB Output is correct
19 Correct 3 ms 1212 KB Output is correct
20 Correct 6 ms 1212 KB Output is correct
21 Correct 3 ms 1212 KB Output is correct
22 Correct 6 ms 1212 KB Output is correct
23 Correct 3 ms 1212 KB Output is correct
24 Correct 3 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 5 ms 1212 KB Output is correct
28 Correct 6 ms 1212 KB Output is correct
29 Correct 6 ms 1212 KB Output is correct
30 Correct 6 ms 1212 KB Output is correct
31 Correct 6 ms 1212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 809 ms 1212 KB Output is correct
2 Correct 786 ms 1212 KB Output is correct
3 Correct 789 ms 1212 KB Output is correct
4 Correct 794 ms 1212 KB Output is correct
5 Correct 793 ms 1212 KB Output is correct
6 Correct 788 ms 1212 KB Output is correct
7 Correct 801 ms 1212 KB Output is correct
8 Correct 791 ms 1212 KB Output is correct
9 Correct 823 ms 1212 KB Output is correct
10 Correct 822 ms 1212 KB Output is correct
11 Correct 798 ms 1212 KB Output is correct
12 Correct 803 ms 1212 KB Output is correct
13 Correct 807 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 Execution timed out 1000 ms 1212 KB Program timed out
18 Correct 999 ms 1212 KB Output is correct
19 Correct 241 ms 1212 KB Output is correct
20 Correct 243 ms 1212 KB Output is correct
21 Correct 253 ms 1212 KB Output is correct
22 Correct 245 ms 1212 KB Output is correct
23 Correct 319 ms 1212 KB Output is correct
24 Correct 329 ms 1212 KB Output is correct
25 Correct 325 ms 1212 KB Output is correct
26 Correct 309 ms 1212 KB Output is correct
27 Correct 337 ms 1212 KB Output is correct
28 Execution timed out 1000 ms 1212 KB Program timed out
29 Correct 978 ms 1212 KB Output is correct
30 Execution timed out 1000 ms 1212 KB Program timed out
31 Correct 911 ms 1212 KB Output is correct
32 Correct 966 ms 1212 KB Output is correct
33 Execution timed out 1000 ms 1212 KB Program timed out
34 Correct 987 ms 1212 KB Output is correct
35 Execution timed out 1000 ms 1212 KB Program timed out