結果

問題 No.187 中華風 (Hard)
ユーザー yomogyomog
提出日時 2021-11-09 17:18:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 3,000 ms
コード長 2,698 bytes
コンパイル時間 2,058 ms
コンパイル使用メモリ 205,140 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 23:08:47
合計ジャッジ時間 5,643 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 111 ms
4,376 KB
testcase_03 AC 109 ms
4,380 KB
testcase_04 AC 126 ms
4,380 KB
testcase_05 AC 128 ms
4,376 KB
testcase_06 AC 126 ms
4,376 KB
testcase_07 AC 126 ms
4,376 KB
testcase_08 AC 113 ms
4,380 KB
testcase_09 AC 112 ms
4,376 KB
testcase_10 AC 113 ms
4,380 KB
testcase_11 AC 124 ms
4,380 KB
testcase_12 AC 126 ms
4,376 KB
testcase_13 AC 52 ms
4,380 KB
testcase_14 AC 51 ms
4,380 KB
testcase_15 AC 99 ms
4,376 KB
testcase_16 AC 101 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 99 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 127 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 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> &b, vector<ll> &m) {
        assert(b.size() == m.size());
        ll r = 0, lcm_m = 1;
        for (int i = 0; i < (int)b.size(); i++) {
            ll p, q, g = ext_gcd(lcm_m, m[i], p, q);
            if ((b[i] - r) % g) return no_sol;
            ll tmp = (b[i] - r) / g * p % (m[i] / g);
            r += lcm_m * tmp;
            lcm_m *= m[i] / g;
        }
        return {mod(r, lcm_m), lcm_m};
    }
    static ll pre_garner(vector<ll> &b, vector<ll> &m, ll MOD) {
        ll res = 1;
        for (int i = 0; i < (int)b.size(); i++) {
            for (int j = 0; j < i; j++) {
                ll g = gcd(m[i], m[j]);
                if ((b[i] - b[j]) % g != 0) return -1;
                m[i] /= g, m[j] /= g;
                ll gi = gcd(m[i], g), gj = g / gi;
                do {
                    g = gcd(gi, gj);
                    gi *= g, gj /= g;
                } while (g != 1);
                m[i] *= gi, m[j] *= gj;
                b[i] %= m[i], b[j] %= m[j];
            }
        }
        for (int i = 0; i < (int)b.size(); i++) (res *= m[i]) %= MOD;
        return res;
    }
    static ll modinv(ll a, ll m) {
        ll x, y;
        ext_gcd(a, m, x, y);
        return mod(x, m);
    }
    static ll garner(vector<ll> &b, vector<ll> &m, ll MOD) {
        m.push_back(MOD);
        vector<ll> coeffs(m.size(), 1), constants(m.size());
        for (int k = 0; k < (int)b.size(); k++) {
            ll t = mod((b[k] - constants[k]) * modinv(coeffs[k], m[k]), m[k]);
            for (int i = k + 1; i < (int)m.size(); i++) {
                (constants[i] += t * coeffs[i]) %= m[i];
                (coeffs[i] *= m[k]) %= m[i];
            }
        }
        return constants.back();
    }
};
using crt = chinese_remainder_theorem;
int main() {
    int n;
    cin >> n;
    vector<ll> b(n), m(n);
    bool exist_non_zero = false;
    for (int i = 0; i < n; i++) {
        cin >> b[i] >> m[i];
        if (b[i]) exist_non_zero = true;
    }
    const int MOD = 1e9 + 7;
    ll lcm = crt::pre_garner(b, m, MOD);
    if (!exist_non_zero) {
        cout << lcm << '\n';
    } else if (lcm == -1) {
        cout << -1 << '\n';
    } else {
        cout << crt::garner(b, m, MOD) << '\n';
    }
    return 0;
}
0