結果

問題 No.186 中華風 (Easy)
ユーザー yomogyomog
提出日時 2021-11-09 14:13:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,255 bytes
コンパイル時間 2,226 ms
コンパイル使用メモリ 200,836 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 18:06:29
合計ジャッジ時間 3,813 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
struct chinese_remainder_theorem {
    static constexpr pair<ll, ll> no_sol = {0, -1};
    static ll mod(ll x, ll y) {
        x %= y;
        return x < 0 ? x + y : x;
    }
    static ll ext_gcd(ll a, ll b, ll &x, ll &y) {
        if (b == 0) return x = 1, y = 0, a;
        ll g = ext_gcd(b, a % b, y, x);
        y -= a / b * x;
        return g;
    }
    static pair<ll, ll> solve(vector<ll> &bs, vector<ll> &ms) {
        assert(bs.size() == ms.size());
        ll r = 0, m = 1;
        for (int i = 0; i < (int)bs.size(); i++) {
            ll p, q, d = ext_gcd(m, ms[i], p, q);
            if ((bs[i] - r) % d) return no_sol;
            ll tmp = (bs[i] - r) / d * p % (ms[i] / d);
            r += m * tmp;
            m *= ms[i] / d;
        }
        return {mod(r, m), m};
    }
};
using crt = chinese_remainder_theorem;
int main() {
    vector<ll> x(3), y(3);
    bool exist_non_zero = false;
    for (int i = 0; i < 3; i++) {
        cin >> x[i] >> y[i];
        if (x[i]) exist_non_zero = true;
    }
    auto [r, m] = crt::solve(x, y);
    if (m == -1) {
        cout << -1 << '\n';
        return 0;
    }
    cout << (exist_non_zero ? r : m) << '\n';
    return 0;
}
0