結果

問題 No.187 中華風 (Hard)
ユーザー drken1215drken1215
提出日時 2018-06-28 10:52:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,386 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 78,920 KB
実行使用メモリ 5,028 KB
最終ジャッジ日時 2023-09-13 14:39:37
合計ジャッジ時間 21,611 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 940 ms
4,716 KB
testcase_01 AC 924 ms
4,780 KB
testcase_02 AC 945 ms
4,916 KB
testcase_03 AC 940 ms
4,872 KB
testcase_04 AC 945 ms
4,860 KB
testcase_05 AC 936 ms
4,784 KB
testcase_06 AC 935 ms
4,852 KB
testcase_07 AC 940 ms
4,728 KB
testcase_08 AC 914 ms
4,760 KB
testcase_09 AC 913 ms
5,028 KB
testcase_10 AC 919 ms
4,864 KB
testcase_11 AC 950 ms
4,808 KB
testcase_12 AC 945 ms
4,728 KB
testcase_13 AC 910 ms
4,872 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 11 ms
4,716 KB
testcase_18 AC 773 ms
4,872 KB
testcase_19 AC 11 ms
4,824 KB
testcase_20 AC 832 ms
4,848 KB
testcase_21 AC 12 ms
4,684 KB
testcase_22 AC 941 ms
4,756 KB
testcase_23 WA -
testcase_24 AC 12 ms
4,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <bitset>
using namespace std;



#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }

const long long MOD = 1000000007;

// エラトステネスのふるい
const int MAX = 1100000;
static bool IsPrime[MAX];
vector<int> Eratostenes(int n = MAX) {
    vector<int> res;
    IsPrime[0] = false; IsPrime[1] = false;
    for (int i = 2; i < n; ++i) IsPrime[i] = true;
    for (int i = 2; i < n; ++i) {
        if (IsPrime[i]) {
            res.push_back(i);
            for (int j = i*2; j < n; j += i) IsPrime[j] = false;
        }
    }
    return res;
}

// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}

// 拡張 Euclid の互除法
long long extGCD(long long a, long long b, long long &p, long long &q) {
    if (b == 0) { p = 1; q = 0; return a; }
    long long d = extGCD(b, a%b, q, p);
    q -= a/b * p;
    return d;
}

// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
    long long x, y;
    extGCD(a, m, x, y);
    return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}

// Garner のアルゴリズム, x%MOD, LCM%MOD を求める (M が互いに素でない場合も対応)
pair<long long, long long> Garner(vector<long long> b, vector<long long> m, long long MOD) {
    /*
        let M be piece-wise prime
     */
    // Eratostenes
    vector<int> primes = Eratostenes();
    
    // for each p, remain the index which can divide p most, and divide p all except the index
    long long mres = 1;
    bool ng = false;
    for (auto p : primes) {
        vector<long long> divp((int)b.size(), 1);
        long long maxp = 1;
        int maxi = -1;
        for (int i = 0; i < (int)b.size(); ++i) {
            long long tm = m[i];
            while (tm % p == 0) {
                divp[i] *= p;
                tm /= p;
            }
            if (divp[i] > maxp) {
                maxp = divp[i];
                maxi = i;
            }
        }
        if (maxp == 1) continue;
        mres = (mres * maxp) % MOD;
        for (int i = 0; i < (int)b.size(); ++i) {
            if (i == maxi) continue;
            if ((b[i] - b[maxi]) % divp[i] != 0) ng = true;
            m[i] /= divp[i];
            b[i] %= m[i];
        }
    }
    if (ng) return make_pair(-1, mres);
    
    /*
        Garner
        for each step, we 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]
     */
    m.push_back(MOD); // banpei
    vector<long long> coeffs((int)m.size(), 1);
    vector<long long> constants((int)m.size(), 0);
    for (int k = 0; k < (int)b.size(); ++k) {
        long long 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 make_pair(constants.back(), mres);
}

int 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 (m[i]) exist_non_zero = true;
    }
    
    pair<long long, long long> res = Garner(b, m, MOD);
    if (res.first == -1) cout << -1 << endl;
    else if (res.first == 0) cout << res.second << endl;
    else cout << res.first << endl;
}
0