結果

問題 No.187 中華風 (Hard)
ユーザー veqccveqcc
提出日時 2019-09-01 15:42:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 3,000 ms
コード長 2,612 bytes
コンパイル時間 742 ms
コンパイル使用メモリ 76,824 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 19:57:02
合計ジャッジ時間 4,608 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <vector>
#include <iostream>
typedef long long ll;
using namespace std;

const ll MOD = 1000000007LL;
typedef pair <ll, ll> P;

ll mod(ll a, ll m) {
    return (a % m + m) % m;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

// 拡張Euclidの互除法
// ap + bq = gcd(a, b) となるp,qを求め、return d = gcd(a,b)
ll extGcd(ll a, ll b, ll &p, ll &q) {
    ll d = a;
    if (b == 0) {
        p = 1;
        q = 0;
    } else {
        d = extGcd(b, a % b, q, p);
        q -= (a / b) * p;
    }
    return d;
}

// 逆元計算 (aとmは互いに素)
ll modinv(ll a, ll m) {
    ll x, y;
    extGcd(a, m, x, y);
    return mod(x, m);
}

// Garner O(N^2) mは全て互いに素である必要がある
//  多倍長整数必要なしにx mod MODを求めるアルゴリズム
//  for each step, solve "coeffs[k] * t[k] + constants[k] = b[k] (mod m[k])"
//   coeffs[k] = m[0]m[1]...m[k-1]
//   constants[k] = t[0] + t[1]m[0] + ... + t[k-1]m[0]m[1]...m[k-2]
ll Garner(vector <ll> b, vector <ll> m, ll MOD) {
    m.push_back(MOD); // 番兵
    vector <ll> coeffs(m.size(), 1);
    vector <ll> constants(m.size(), 0);
    for (int k = 0; k < b.size(); k++) {
        ll t = mod((b[k] - constants[k]) * modinv(coeffs[k], m[k]), m[k]);
        for (int i = k + 1; i < m.size(); i++) {
            (constants[i] += t * coeffs[i]) %= m[i];
            (coeffs[i] *= m[k]) %= m[i];
        }
    }
    return constants.back();
}

// Garner の前処理。法を全て互いに素にする。
ll preGarner(vector <ll> &b, vector <ll> &m, ll MOD) {
    for (int i = 0; i < 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];
        }
    }

    ll res = 1;
    for (int i = 0; i < b.size(); i++) (res *= m[i]) %= MOD;
    return res;
}


// verified
//  https://yukicoder.me/problems/448
void yuki187() {
    int n; cin >> n;
    vector <ll> b(n), m(n);
    bool all_zero = true;
    for (int i = 0; i < n; i++) {
        cin >> b[i] >> m[i];
        if (b[i]) all_zero = false;
    }
    ll lcm = preGarner(b, m, MOD);
    if (all_zero) cout << lcm << '\n';
    else if (lcm == -1) cout << -1 << '\n';
    else cout << Garner(b, m, MOD) << '\n';
}

int main() {
    yuki187();
    return 0;
}
0