結果

問題 No.187 中華風 (Hard)
ユーザー packer_jppacker_jp
提出日時 2020-04-05 03:20:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 3,000 ms
コード長 4,799 bytes
コンパイル時間 1,644 ms
コンパイル使用メモリ 171,036 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-16 06:25:16
合計ジャッジ時間 5,690 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 168 ms
4,380 KB
testcase_03 AC 166 ms
4,380 KB
testcase_04 AC 213 ms
4,380 KB
testcase_05 AC 211 ms
4,376 KB
testcase_06 AC 214 ms
4,380 KB
testcase_07 AC 212 ms
4,380 KB
testcase_08 AC 154 ms
4,376 KB
testcase_09 AC 154 ms
4,380 KB
testcase_10 AC 154 ms
4,380 KB
testcase_11 AC 214 ms
4,380 KB
testcase_12 AC 214 ms
4,376 KB
testcase_13 AC 70 ms
4,376 KB
testcase_14 AC 70 ms
4,380 KB
testcase_15 AC 149 ms
4,380 KB
testcase_16 AC 152 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 163 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 213 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
次のファイルから読み込み:  /usr/local/gcc7/include/c++/12.2.0/string:50,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/locale_classes.h:40,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/ios_base.h:41,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/ios:42,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/istream:38,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/sstream:38,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/complex:45,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/ccomplex:39,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:54,
         次から読み込み:  main.cpp:1:
静的メンバ関数 ‘static _Tp* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(const _Tp*, const _Tp*, _Tp*) [with _Tp = long long int; bool _IsMove = false]’ 内,
    inlined from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = long long int*; _OI = long long int*]’ at /usr/local/gcc7/include/c++/12.2.0/bits/stl_algobase.h:495:30,
    inlined from ‘_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = long long int*; _OI = long long int*]’ at /usr/local/gcc7/include/c++/12.2.0/bits/stl_algobase.h:522:42,
    inlined from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _OI = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >]’ at /usr/local/gcc7/include/c++/12.2.0/bits/stl_algobase.h:529:31,
    inlined from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >; _OI = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >]’ at /usr/local/gcc7/include/c++/12.2.0/bits/stl_algobase.h:620:7,
    

ソースコード

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 safe_mod(long long a, int m) { return (a % m + m) % m; }

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);
    }
    return safe_mod(x, m);
}

template<typename I, typename J> int garner(I begin_r, I end_r, J begin_m, int M) {
    int n = end_r - begin_r;
    vector<long long> x(n + 1);
    vector<int> m(n + 1);
    std::copy(begin_m, begin_m + n, begin(m));
    m[n] = M;
    vector<int> prod_m(n + 1, 1);
    for (int i = 0; i < n; i++) {
        long long r_i = *(begin_r + i);
        int mmi = safe_mod(r_i - x[i], m[i]) * mod_inv(prod_m[i], m[i]) % m[i];
        for (int j = i + 1; j <= n; j++) {
            (x[j] += mmi * prod_m[j] % m[j]) %= m[j];
            (prod_m[j] *= m[i]) %= m[j];
        }
    }
    return x[n];
}
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