結果

問題 No.187 中華風 (Hard)
ユーザー tokusakuraitokusakurai
提出日時 2020-10-19 20:03:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 3,000 ms
コード長 3,426 bytes
コンパイル時間 2,172 ms
コンパイル使用メモリ 205,948 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 13:24:25
合計ジャッジ時間 5,283 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

//ユークリッドの互除法を用いた計算
//計算量 O(log(max(A, B)))

template<typename T> T gcd(const T &a, const T &b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

template<typename T> T lcm(const T &a, const T &b) {return a*(b/gcd(a,b));}

template<typename T> T extgcd(const T &a, const T &b, T &x, T &y){
    if(b == 0) {x = 1, y = 0; return a;}
    T g = extgcd(b, a%b, y, x);
    y -= (a/b)*x;
    return g;
}

int mod(const ll &a, const int &m){
    int ret = a%m;
    return ret+(ret < 0? m : 0);
}

int modinv(const int &a, const int &m){ //aとmは互いに素
    int x, y;
    extgcd(a, m, x, y);
    return mod(x, m);
}

template<typename T> T floor_sum(const T &n, const T &m, T a, T b){ //Σ(floor((a*i+b)/m)) (0<=i<n)
    T ret = (a/m)*(n*(n-1)/2) + (b/m)*n;
    a %= m, b %= m;
    T y = (a*n+b)/m;
    if(y == 0) return ret;
    ret += floor_sum(y, a, m, a*n-(m*y-b));
    return ret;
}

template<typename T> pair<T, T> Chinese_reminder_theorem(const T &a1, const T &m1, const T &a2, const T &m2){
    T x, y, g = extgcd(m1, m2, x, y);
    if((a2-a1)%g != 0) return make_pair(0, -1);
    T m = m1*(m2/g);
    T tmp = mod(x*((a2-a1)/g), m2/g);
    T a = (m1*tmp+a1) % m;
    return make_pair(a, m);
}

bool prepare_Garner(vector<int> &a, vector<int> &m){
    int n = sz(a);
    rep(i, n){
        rep(j, i){
            int g = gcd(m[i], m[j]);
            if((a[i]-a[j])%g != 0) return false;
            m[i] /= g, m[j] /= g;
            int 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;
        }
    }
    return true;
}

int Garner(vector<int> a, vector<int> m, const int &M){ //mの各要素はそれぞれ互いに素
    m.pb(M);
    vector<ll> coeffs(sz(m), 1);
    vector<ll> constants(sz(m), 0);
    rep(k, sz(a)){
        ll x = a[k]-constants[k], y = modinv(coeffs[k], m[k]);
        ll t = mod(x*y, m[k]);
        rep2(i, k+1, sz(m)-1){
            constants[i] += t*coeffs[i], constants[i] %= m[i];
            coeffs[i] *= m[k], coeffs[i] %= m[i];
        }
    }
    return constants.back();
}

//https://yukicoder.me/problems/no/187

int main(){
    int N;
    cin >> N;
    vector<int> a(N), m(N);
    rep(i, N) cin >> a[i] >> m[i];
    if(!prepare_Garner(a, m)) {cout << -1 << endl; return 0;}
    ll l = 1;
    rep(i, N) l *= m[i], l %= MOD;
    bool flag = true;
    rep(i, N) if(a[i] != 0) flag = false;
    if(flag) {cout << l << endl; return 0;}
    cout << Garner(a, m, MOD) << endl;
}
0