Submission #12335

# Submission time Handle Problem Language Result Execution time Memory
12335 2014-12-26T12:32:24 Z baneling100 Min-cost GCD (GA9_mcg) C++
57 / 100
1000 ms 1208 KB
#include <stdio.h>
#include <algorithm>
#include <math.h>
#define INF 0x7fffffffffffffff

using namespace std;

int T, Cnt;
long long A, B, P, Q, D[1001][2];

void gcd(long long X, long long Y) {

    if(!Y) {
        printf("%lld\n",min(D[Cnt][0],D[Cnt][1]));
        return;
    }
    Cnt++;
    if(log(X)-log(Y)+log(Q)>log(INF) || D[Cnt-1][1]+X/Y*Q<0)
        D[Cnt][0]=D[Cnt-1][0]+P;
    else
        D[Cnt][0]=min(D[Cnt-1][0]+P,D[Cnt-1][1]+X/Y*Q);
    if(log(X)-log(Y)+log(Q)>log(INF) || D[Cnt-1][0]+X/Y*Q<0)
        D[Cnt][1]=INF;
    else
        D[Cnt][1]=D[Cnt-1][0]+X/Y*Q;
    gcd(Y,X%Y);
}

int main(void) {

    int i;

    scanf("%d",&T);
    for(i=1 ; i<=T ; i++) {
        scanf("%lld %lld %lld %lld",&A,&B,&P,&Q);
        Cnt=1;
        if(A>B) {
            D[Cnt][0]=0;
            D[Cnt][1]=INF;
        }
        else if(A<B) {
            D[Cnt][0]=P;
            D[Cnt][1]=0;
        }
        else
            D[Cnt][0]=D[Cnt][1]=0;
        if(A>B)
            gcd(A,B);
        else
            gcd(B,A);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1208 KB Output is correct
2 Correct 0 ms 1208 KB Output is correct
3 Correct 0 ms 1208 KB Output is correct
4 Correct 0 ms 1208 KB Output is correct
5 Correct 0 ms 1208 KB Output is correct
6 Correct 0 ms 1208 KB Output is correct
7 Correct 0 ms 1208 KB Output is correct
8 Correct 0 ms 1208 KB Output is correct
9 Correct 0 ms 1208 KB Output is correct
10 Correct 0 ms 1208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 1208 KB Output is correct
2 Correct 246 ms 1208 KB Output is correct
3 Correct 257 ms 1208 KB Output is correct
4 Correct 244 ms 1208 KB Output is correct
5 Correct 252 ms 1208 KB Output is correct
6 Correct 250 ms 1208 KB Output is correct
7 Correct 239 ms 1208 KB Output is correct
8 Correct 252 ms 1208 KB Output is correct
9 Correct 247 ms 1208 KB Output is correct
10 Correct 258 ms 1208 KB Output is correct
11 Correct 249 ms 1208 KB Output is correct
12 Correct 237 ms 1208 KB Output is correct
13 Correct 246 ms 1208 KB Output is correct
14 Correct 2 ms 1208 KB Output is correct
15 Correct 2 ms 1208 KB Output is correct
16 Correct 2 ms 1208 KB Output is correct
17 Correct 2 ms 1208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1208 KB Output is correct
2 Correct 8 ms 1208 KB Output is correct
3 Correct 3 ms 1208 KB Output is correct
4 Correct 7 ms 1208 KB Output is correct
5 Correct 7 ms 1208 KB Output is correct
6 Correct 8 ms 1208 KB Output is correct
7 Correct 7 ms 1208 KB Output is correct
8 Correct 7 ms 1208 KB Output is correct
9 Correct 7 ms 1208 KB Output is correct
10 Correct 6 ms 1208 KB Output is correct
11 Correct 7 ms 1208 KB Output is correct
12 Correct 7 ms 1208 KB Output is correct
13 Correct 6 ms 1208 KB Output is correct
14 Correct 0 ms 1208 KB Output is correct
15 Correct 1 ms 1208 KB Output is correct
16 Correct 2 ms 1208 KB Output is correct
17 Correct 0 ms 1208 KB Output is correct
18 Correct 3 ms 1208 KB Output is correct
19 Correct 8 ms 1208 KB Output is correct
20 Correct 8 ms 1208 KB Output is correct
21 Correct 8 ms 1208 KB Output is correct
22 Correct 8 ms 1208 KB Output is correct
23 Correct 5 ms 1208 KB Output is correct
24 Correct 8 ms 1208 KB Output is correct
25 Correct 6 ms 1208 KB Output is correct
26 Correct 4 ms 1208 KB Output is correct
27 Correct 8 ms 1208 KB Output is correct
28 Correct 5 ms 1208 KB Output is correct
29 Correct 8 ms 1208 KB Output is correct
30 Correct 8 ms 1208 KB Output is correct
31 Correct 7 ms 1208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1000 ms 1208 KB Program timed out
2 Halted 0 ms 0 KB -