結果

問題 No.187 中華風 (Hard)
ユーザー packer_jppacker_jp
提出日時 2020-04-05 01:29:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,801 bytes
コンパイル時間 1,515 ms
コンパイル使用メモリ 168,332 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-16 06:20:39
合計ジャッジ時間 7,128 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define all(x) begin(x), end(x)
#define uniq(x) (x).erase(unique(all(x)), end(x))
#define bit(n) (1LL << (n))
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template<typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr long double PI = 3.1415926535897932384626433832795028L;
constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
    while (b) { swap(a %= b, b); }
    return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
template<typename T> void fin(T mes) {
    cout << mes << endl;
    exit(0);
}
template<typename T, typename U> bool chmax(T &a, const U &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> bool chmin(T &a, const U &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
    os << "(" << rhs.first << ", " << rhs.second << ")";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
struct setup {
    static constexpr int PREC = 20;
    setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} setup;

// Garner のアルゴリズムの前処理
long long PreGarner(vector<long long> &b, vector<long long> &m, long long MOD) {
    long long res = 1;
    for (int i = 0; i < (int) b.size(); ++i) {
        for (int j = 0; j < i; ++j) {
            long long g = gcd(m[i], m[j]);

            // これを満たさなければ解はない
            if ((b[i] - b[j]) % g != 0) { return -1; }

            // s = m[i], t = m[j] を仮想的に素因数分解して s = p^k ... q^l ..., t = q^m ... r^n ... となったときに
            m[i] /= g; // p については i の方が大きかったものについての j との差分、と q
            m[j] /= g; // p については j の方が大きかったものについての i との差分、と r

            /*
                残る g を i と j に振り分ける (i の方が指数大きかった素因子 p の分は最終的に gi に、j の方が指数大きかった素因子 p の分は最終的に gj に)
             */
            // ひとまず j 側にある p については gj のみに行くようにする
            long long gi = gcd(m[i], g), gj = g / gi;

            // 本来 i 側に行くべき p で gj 側にあるものを gi 側に寄せていく
            do {
                g = gcd(gi, gj);
                gi *= g, gj /= g;
            } while (g != 1);

            // i 側と j 側に戻していく
            m[i] *= gi, m[j] *= gj;

            // m[i] と m[j] が元より小さくなったのに合わせて余りも計算し直しておく
            b[i] %= m[i], b[j] %= m[j];
        }
    }
    for (int i = 0; i < (int) b.size(); ++i) { (res *= m[i]) %= MOD; }
    return res;
}

int mod_inv(long long a, int m) {
    long long b = m, x = 1, u = 0;
    while (b) {
        long long t = a / b;
        std::swap(a -= t * b, b);
        std::swap(x -= t * u, u);
    }
    x %= m;
    if (x < 0) { x += m; }
    return x;
}

template<typename I, typename J> long long garner(I begin_r, I end_r, J begin_m, long long M) {
    int n = end_r - begin_r;
    long long ret = 0;
    for (int i = 0; i < n; i++) {
        long long r_i = *(begin_r + i);
        int m_i = *(begin_m + i);
        r_i %= m_i;
        if (r_i < 0) { r_i += m_i; }
        long long prod = 1, prod_inv = 1;
        for (int j = 0; j < i; j++) {
            int m_j = *(begin_m + j);
            (prod *= m_j) %= M;
            (prod_inv *= mod_inv(m_j, m_i)) %= m_i;
        }
        (ret += (r_i - ret % m_i + m_i) % m_i * prod_inv % m_i * prod % M) %= M;
    }
    return ret;
}

int mod = 1e9 + 7;
signed main() {
    int N;
    cin >> N;
    vector<long long> 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; }
    }
    long long lcm = PreGarner(b, m, mod);

    if (!exist_non_zero) { cout << lcm << endl; }
    else if (lcm == -1) { cout << -1 << endl; }
    else { cout << garner(begin(b), end(b), begin(m), mod) << endl; }
}
0