結果

問題 No.3068 Speedrun (Hard)
ユーザー Yudai0606Nazo
提出日時 2025-07-31 23:15:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,783 bytes
コンパイル時間 4,167 ms
コンパイル使用メモリ 253,664 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-07-31 23:15:33
合計ジャッジ時間 6,027 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <cassert>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
//using mint = modint1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repu(i, s, t) for (int i = (int)(s); i < (int)(t); i++)
#define repd(i, s, t) for (int i = (int)(s)-1; i >= (int)(t); i--)
#define all(v) v.begin(), v.end()
void _u() { cerr << endl; } template <class H, class... T> void _u(H&& h, T&&... t) { cerr << h << ", "; _u(move(t)...); }
#define U(...) { cerr << #__VA_ARGS__ << ": "; _u(__VA_ARGS__); }
template<typename T> bool chmax(T &a, const T b) { if(a >= b) return false; a = b; return true; }
template<typename T> bool chmin(T &a, const T b) { if(a <= b) return false; a = b; return true; }
template<typename T> istream& operator>>(istream &in, vector<T> &a) { for(T &x: a) in >> x; return in; }
template<typename T> ostream& operator<<(ostream &out, const vector<T> &a) { for(const T &x: a) out << x << ' '; return out; }
const int di[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dj[] = {0, 1, 0, -1, -1, 1, 1, -1, 0};


template<typename T=int>
T extgcd(T a, T b, T &x, T &y) {
    T g = a;
    if(b) {
        g = extgcd(b, a%b, y, x);
        y -= (a/b) * x;
    } else {
        x = 1, y = 0;
    }
    return g;
}
template<typename T = int>
bool format(T a, T b, T &x, T &y, T xmin, T xmax, T ymin, T ymax) {
    T tmp;
    if(x < xmin) {
        tmp = (xmin - x + b - 1) / b;
        x += tmp * b, y -= tmp * a;
    }
    if(y < ymin) {
        tmp = (ymin - y + a - 1) / a;
        x -= tmp * b, y += tmp * a;
    }
    if(x > xmax) {
        tmp = (x - xmax + b - 1) / b;
        x -= tmp * b, y += tmp * a;
    }
    if(y > ymax) {
        tmp = (y - ymax + b - 1) / b;
        x += tmp * b, y -= tmp * a;
    }
    return x >= xmin && x <= xmax && y >= ymin && y <= ymax;
}
int main() {
    int a, b, c, d, n, p, q, r, s, t;
    cin >> a >> b >> c >> d >> n >> p >> q >> r >> s >> t;
    bool sw1 = false, sw2 = false;
    if(p > q) swap(a, b), swap(p, q), sw1 = true;
    if(r > s) swap(c, d), swap(r, s), sw2 = true;
    if(p == q && q == r && r == s) {
        int na = min(n, a); n -= na;
        int nb = min(n, b); n -= nb;
        int nc = min(n, c); n -= nc;
        int nd = min(n, d); n -= nd;
        cout << na << " " << nb << " " << nc << " " << nd << endl;
        return 0;
    }
    if(p == q && r == s) {
        int i2 = (t - n * p) / (r - p);
        int i1 = n - i2;
        int na = min(i1, a); int nb = i1 - na;
        int nc = min(i2, c); int nd = i2 - nc;
        cout << na << " " << nb << " " << nc << " " << nd << endl;
        return 0;
    }
    rep(i1, n+1) {
        int i2 = n - i1;
        ll x1 = min(a, i1) * p + (i1 - min(a, i1)) * q;
        ll y1 = q - p, k1max = min(b - (i1-min(a, i1)), min(a, i1));
        if(k1max < 0) continue;
        ll x2 = min(c, i2) * r + (i2 - min(c, i2)) * s;
        ll y2 = s - r, k2max = min(d - (i2-min(c, i2)), min(c, i2));
        if(k2max < 0) continue;
        // (x1 + y1 * k1) + (x2 + y2 * k2) = t
        // y1 * k1 + y2 * k2 = t - x1 - x2
        if(t-x1-x2 < 0) continue;
        ll k1 = 0, k2 = 0;
        ll g = extgcd<ll>(y1, y2, k1, k2);
        if((t-x1-x2) % g != 0) continue;
        ll tmp = (t-x1-x2) / g;
        k1 *= tmp, k2 *= tmp;
        bool res = format<ll>(y1, y2, k1, k2, 0, k1max, 0, k2max);
        if(!res) continue;
        int na = min(a, i1) - k1, nb = i1 - min(a, i1) + k1;
        int nc = min(c, i2) - k2, nd = i2 - min(c, i2) + k2;
        if(sw1) swap(na, nb);
        if(sw2) swap(nc, nd);
        cout << na << " " << nb << " " << nc << " " << nd << endl;
        return 0;
    }
    return 0;
}
0