답안 #12131

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
12131 2014-12-21T07:52:27 Z gs14004 Min-cost GCD (GA9_mcg) C++
100 / 100
978 ms 1196 KB
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;

typedef long long lint;
lint dp[100];

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, int step){
    if(x == 0 || y == 0) return 0;
    if(x >= y){
        if(dp[step]) return dp[step];
        if(!calc_valid(q,x/y)) return dp[step] = f(y,x%y,step+1) + p;
        else return dp[step] = min(f(y,x%y,step+1) + p,f(x%y,y,step+1) + q*(x/y));
    }
    else{
        if(!calc_valid(q,y/x)) return 5e18;
        else return f(x,y%x,step+1) + q * (y/x);
    }
}

void solve(){
    memset(dp,0,sizeof(dp));
    scanf("%lld %lld %lld %lld",&a,&b,&p,&q);
    if(a < b) printf("%lld\n",min(p + f(b,a,0),f(a,b,0)));
    else printf("%lld\n",f(a,b,0));
}

int main(){
    int t;
    scanf("%d",&t);
    while (t--) {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1196 KB Output is correct
2 Correct 0 ms 1196 KB Output is correct
3 Correct 0 ms 1196 KB Output is correct
4 Correct 0 ms 1196 KB Output is correct
5 Correct 0 ms 1196 KB Output is correct
6 Correct 0 ms 1196 KB Output is correct
7 Correct 0 ms 1196 KB Output is correct
8 Correct 0 ms 1196 KB Output is correct
9 Correct 0 ms 1196 KB Output is correct
10 Correct 0 ms 1196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 205 ms 1196 KB Output is correct
2 Correct 206 ms 1196 KB Output is correct
3 Correct 219 ms 1196 KB Output is correct
4 Correct 201 ms 1196 KB Output is correct
5 Correct 203 ms 1196 KB Output is correct
6 Correct 200 ms 1196 KB Output is correct
7 Correct 211 ms 1196 KB Output is correct
8 Correct 213 ms 1196 KB Output is correct
9 Correct 208 ms 1196 KB Output is correct
10 Correct 206 ms 1196 KB Output is correct
11 Correct 206 ms 1196 KB Output is correct
12 Correct 214 ms 1196 KB Output is correct
13 Correct 212 ms 1196 KB Output is correct
14 Correct 1 ms 1196 KB Output is correct
15 Correct 2 ms 1196 KB Output is correct
16 Correct 2 ms 1196 KB Output is correct
17 Correct 2 ms 1196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1196 KB Output is correct
2 Correct 4 ms 1196 KB Output is correct
3 Correct 4 ms 1196 KB Output is correct
4 Correct 5 ms 1196 KB Output is correct
5 Correct 4 ms 1196 KB Output is correct
6 Correct 5 ms 1196 KB Output is correct
7 Correct 3 ms 1196 KB Output is correct
8 Correct 5 ms 1196 KB Output is correct
9 Correct 3 ms 1196 KB Output is correct
10 Correct 5 ms 1196 KB Output is correct
11 Correct 2 ms 1196 KB Output is correct
12 Correct 3 ms 1196 KB Output is correct
13 Correct 5 ms 1196 KB Output is correct
14 Correct 2 ms 1196 KB Output is correct
15 Correct 2 ms 1196 KB Output is correct
16 Correct 2 ms 1196 KB Output is correct
17 Correct 3 ms 1196 KB Output is correct
18 Correct 2 ms 1196 KB Output is correct
19 Correct 5 ms 1196 KB Output is correct
20 Correct 3 ms 1196 KB Output is correct
21 Correct 2 ms 1196 KB Output is correct
22 Correct 4 ms 1196 KB Output is correct
23 Correct 0 ms 1196 KB Output is correct
24 Correct 5 ms 1196 KB Output is correct
25 Correct 6 ms 1196 KB Output is correct
26 Correct 6 ms 1196 KB Output is correct
27 Correct 6 ms 1196 KB Output is correct
28 Correct 6 ms 1196 KB Output is correct
29 Correct 7 ms 1196 KB Output is correct
30 Correct 3 ms 1196 KB Output is correct
31 Correct 4 ms 1196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 858 ms 1196 KB Output is correct
2 Correct 859 ms 1196 KB Output is correct
3 Correct 847 ms 1196 KB Output is correct
4 Correct 855 ms 1196 KB Output is correct
5 Correct 836 ms 1196 KB Output is correct
6 Correct 850 ms 1196 KB Output is correct
7 Correct 853 ms 1196 KB Output is correct
8 Correct 857 ms 1196 KB Output is correct
9 Correct 852 ms 1196 KB Output is correct
10 Correct 831 ms 1196 KB Output is correct
11 Correct 860 ms 1196 KB Output is correct
12 Correct 848 ms 1196 KB Output is correct
13 Correct 852 ms 1196 KB Output is correct
14 Correct 963 ms 1196 KB Output is correct
15 Correct 959 ms 1196 KB Output is correct
16 Correct 955 ms 1196 KB Output is correct
17 Correct 961 ms 1196 KB Output is correct
18 Correct 943 ms 1196 KB Output is correct
19 Correct 292 ms 1196 KB Output is correct
20 Correct 249 ms 1196 KB Output is correct
21 Correct 274 ms 1196 KB Output is correct
22 Correct 269 ms 1196 KB Output is correct
23 Correct 352 ms 1196 KB Output is correct
24 Correct 366 ms 1196 KB Output is correct
25 Correct 358 ms 1196 KB Output is correct
26 Correct 370 ms 1196 KB Output is correct
27 Correct 376 ms 1196 KB Output is correct
28 Correct 965 ms 1196 KB Output is correct
29 Correct 946 ms 1196 KB Output is correct
30 Correct 960 ms 1196 KB Output is correct
31 Correct 885 ms 1196 KB Output is correct
32 Correct 928 ms 1196 KB Output is correct
33 Correct 943 ms 1196 KB Output is correct
34 Correct 967 ms 1196 KB Output is correct
35 Correct 978 ms 1196 KB Output is correct