結果

問題 No.187 中華風 (Hard)
ユーザー HIR180HIR180
提出日時 2019-10-26 23:16:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 3,000 ms
コード長 2,121 bytes
コンパイル時間 1,573 ms
コンパイル使用メモリ 171,828 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 22:06:18
合計ジャッジ時間 5,474 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 162 ms
4,348 KB
testcase_03 AC 159 ms
4,348 KB
testcase_04 AC 205 ms
4,348 KB
testcase_05 AC 205 ms
4,348 KB
testcase_06 AC 205 ms
4,348 KB
testcase_07 AC 209 ms
4,348 KB
testcase_08 AC 141 ms
4,352 KB
testcase_09 AC 142 ms
4,352 KB
testcase_10 AC 141 ms
4,352 KB
testcase_11 AC 206 ms
4,352 KB
testcase_12 AC 206 ms
4,348 KB
testcase_13 AC 65 ms
4,352 KB
testcase_14 AC 65 ms
4,352 KB
testcase_15 AC 149 ms
4,348 KB
testcase_16 AC 152 ms
4,352 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 157 ms
4,352 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 205 ms
4,348 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define SIZE 100005
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define pb push_back

template<class T> T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; }
template<class T> T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; }
ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }

ll garner(vector<ll>b, vector<ll>m, ll mod){
    b.emplace_back(0);
    m.emplace_back(mod);

    vector<ll> coffs(b.size(), 1);
    vector<ll> constants(b.size(), 0);
    for(int i=0;i<b.size()-1;i++){
        ll v = (b[i] - constants[i]) * mod_inv<ll>(coffs[i], m[i]) % m[i];
        if (v < 0) v += m[i];

        for (int j = i + 1; j < b.size(); j++) {
            (constants[j] += coffs[j] * v) %= m[j];
            (coffs[j] *= m[i]) %= m[j];
        }
    }

    return constants[b.size() - 1];
}
ll gcd(ll a, ll b){
    if(a<b)swap(a,b);
    if(b==0) return a;
    else return gcd(b,a%b);
}
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;
}

///////
vector<ll>B,M;
int n;
int main(){
	cin >> n;
	rep(i,n){ ll a,b; cin >> a >> b; B.pb(a); M.pb(b); }
	ll ret = preGarner(B,M,1000000007LL);
	rep(i,n) if(B[i] > 0) goto nxt;
	cout << ret << endl; return 0;
	nxt:;
	if(ret == -1) cout << "-1" << endl;
	else{
		cout << garner(B,M,1000000007LL) << endl;
	}
}
0