제출 #199846

#제출 시각아이디문제언어결과실행 시간메모리
199846human1234Min-cost GCD (GA9_mcg)C++14
0 / 100
340 ms2144 KiB
#include <iostream>
#include <vector>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;

int main() {
    ios::sync_with_stdio(false);
    
    int t;

    cin >> t;
    while (t--) {
        ll a, b, p, q;
        vpll v;

        cin >> a >> b >> p >> q;
        v.resize(100);
        if (a > b) {
            v[0] = {0, -1};
        } else {
            ll t = a;

            a = b;
            b = t;
            v[0] = {p, 0};
        }

        int c = 0;

        while (a && b) {
            ll k = a / b, r = a % b;
            pll past = v[c], next;
            
            if (past.second == -1) {
                next = {p, k * q};
            } else {
                ll f, s;

                f = min(past.first + p, past.second + k * q);
                s = min(past.first + k * q, past.second + p + k * q);
                next = {f, s};
            }

            c++;
            v[c] = next;

            a = b;
            b = r;
        }

        cout << min(v[c].first, v[c].second) << "\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...