Submission #199842

# Submission time Handle Problem Language Result Execution time Memory
199842 2020-02-03T16:25:47 Z human1234 Min-cost GCD (GA9_mcg) C++14
0 / 100
162 ms 8440 KB
#include <stdio.h>
#include <vector>

using namespace std;

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

const ll LIMIT = 1e19;

inline ll conv(i128 x) {
    if (x > (i128)LIMIT) return LIMIT;
    return (ll)x;
}

int main() {
    int t;
    vpll v;

    scanf("%d", &t);
    v.resize(300);
    while (t--) {
        ll a, b, p, q;

        scanf("%lld%lld%lld%lld", &a, &b, &p, &q);
        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 = {conv((i128)p), conv((i128)k * (i128)q)};
            } else {
                ll f, s;

                f = min(conv((i128)past.first + (i128)p), conv((i128)past.second + (i128)k * (i128)q));
                s = min(conv((i128)past.first + (i128)k * (i128)q), conv((i128)past.second + (i128)p + (i128)k * (i128)q));

                next = {f, s};
            }

            c++;
            v[c] = next;

            a = b;
            b = r;
        }

        printf("%lld\n", min(v[c].first, v[c].second));
    }
}

Compilation message

mcg.cpp:11:18: warning: overflow in implicit constant conversion [-Woverflow]
 const ll LIMIT = 1e19;
                  ^~~~
mcg.cpp: In function 'int main()':
mcg.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &t);
     ~~~~~^~~~~~~~~~
mcg.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld%lld%lld%lld", &a, &b, &p, &q);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Incorrect 5 ms 256 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 61 ms 2300 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 7 ms 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 6 ms 376 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
11 Correct 6 ms 248 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
13 Correct 6 ms 376 KB Output is correct
14 Incorrect 6 ms 376 KB Output isn't correct
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 162 ms 8312 KB Output is correct
2 Correct 146 ms 8312 KB Output is correct
3 Correct 138 ms 8312 KB Output is correct
4 Correct 141 ms 8384 KB Output is correct
5 Correct 142 ms 8440 KB Output is correct
6 Correct 144 ms 8440 KB Output is correct
7 Execution timed out 53 ms 3320 KB Time limit exceeded (wall clock)
8 Halted 0 ms 0 KB -