This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// main.cpp
// 2. Min-cost GCD (mcg)
//
// Created by KJBS2 on 2014. 12. 24..
// Copyright (c) 2014년 KJBS2. All rights reserved.
//
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
#define MAX_LL ((ll)1*0x7fffffff*0x7fffffff)
ll A, B, P, Q;
ll X, Y, Z, W;
void PROCESS() {
scanf("%lld%lld%lld%lld", &A, &B, &P, &Q);
X = 0; Y = MAX_LL;
for(; A!=0 && B!=0;) {
ll C = A % B, D = A / B;
Z = X + P;
W = X + D * Q;
if(Y != MAX_LL) {
Z = min(Z, Y + P + P);
Z = min(Z, Y + D * Q);
W = min(W, Y + P + D * Q);
}
X = Z;
Y = W;
A = B; B = C;
}
printf("%lld\n", min(X, Y) );
}
int main() {
int T; scanf("%d", &T);
for(int i=0; i<T; i++)
PROCESS();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |