結果

問題 No.187 中華風 (Hard)
ユーザー lumc_lumc_
提出日時 2018-10-05 19:42:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 3,000 ms
コード長 2,847 bytes
コンパイル時間 1,225 ms
コンパイル使用メモリ 115,944 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-02 16:47:27
合計ジャッジ時間 3,952 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
4,380 KB
testcase_01 AC 32 ms
4,376 KB
testcase_02 AC 84 ms
4,380 KB
testcase_03 AC 86 ms
4,376 KB
testcase_04 AC 87 ms
4,376 KB
testcase_05 AC 88 ms
4,376 KB
testcase_06 AC 88 ms
4,380 KB
testcase_07 AC 89 ms
4,380 KB
testcase_08 AC 89 ms
4,380 KB
testcase_09 AC 90 ms
4,376 KB
testcase_10 AC 88 ms
4,380 KB
testcase_11 AC 87 ms
4,380 KB
testcase_12 AC 88 ms
4,380 KB
testcase_13 AC 76 ms
4,380 KB
testcase_14 AC 77 ms
4,380 KB
testcase_15 AC 75 ms
4,380 KB
testcase_16 AC 75 ms
4,376 KB
testcase_17 AC 20 ms
4,376 KB
testcase_18 AC 30 ms
4,380 KB
testcase_19 AC 19 ms
4,376 KB
testcase_20 AC 76 ms
4,376 KB
testcase_21 AC 20 ms
4,376 KB
testcase_22 AC 87 ms
4,376 KB
testcase_23 AC 20 ms
4,380 KB
testcase_24 AC 20 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
// }}}
using namespace std;
using ll = long long;

/// --- math {{{ ///
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d;
  return b == 0 ? (x = 1, y = 0, a)
    : (d = extgcd(b, a % b, y, x), y -= a / b * x, d);
}
ll modinv(ll a, ll mod = 1e9 + 7) {
  ll x = 0, y = 0;
  extgcd(a, mod, x, y);
  return (x + mod) % mod;
}
ll modpow(ll a, ll b, ll mod = 1e9 + 7) {
  ll r = 1;
  a %= mod;
  while(b) {
    if(b & 1) r = r * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}
/// }}}--- ///

// require math library
/// --- Garner Library {{{ ///
ll garner(const vector< ll > &x, vector< ll > mods, ll mod) {
  mods.emplace_back(mod);
  vector< ll > coeffs(x.size() + 1, 1); // coeffs[i]v_i
  vector< ll > constants(x.size() + 1, 0);
  for(size_t i = 0; i < x.size(); i++) {
    // x[i] - constants[i] == coeffs[i] * v_i (mod mods[i])
    ll v = (x[i] - constants[i]) * modinv(coeffs[i], mods[i]) % mods[i];
    if(v < 0) v += mods[i];
    for(size_t j = i + 1; j < x.size() + 1; j++) {
      // coeffs[j] is (mod mods[j])
      constants[j] = (constants[j] + coeffs[j] * v) % mods[j];
      coeffs[j] = (coeffs[j] * mods[i]) % mods[j];
    }
  }
  return constants.back();
}
/// }}}--- ///


int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n; cin >> n;
  vector<ll> vs(n), ms(n);
  for(int i = 0; i < n; i++) {
    cin >> vs[i] >> ms[i];
  }

  vector<int> primes;
  for(int i = 2; (ll) i*i <= 1e9; i++) {
    int flag = 1;
    for(int x : primes) if(i % x == 0) {
      flag = 0; break;
    }
    if(flag) primes.emplace_back(i);
  }

  for(int m : ms) {
    for(int p : primes) while(m % p == 0) m /= p;
    if(m != 1) primes.emplace_back(m);
  }

  for(int p : primes) {
    vector< pair<int, int> > ps;
    for(int i = 0; i < n; i++) {
      if(ms[i] % p == 0) {
        int lg = 0;
        while(ms[i] % p == 0) ms[i] /= p, lg++;
        ps.emplace_back(lg, i);
      }
    }
    if(ps.size() < 1) continue;
    sort(begin(ps), end(ps));
    int base = vs[ps.back().second];
    for(auto d : ps) {
      if(vs[d.second] % p != base % p) {
        cout << -1 << endl;
        return 0;
      }
      if(d.second == ps.back().second) {
        for(int i = 0; i < d.first; i++) ms[d.second] *= p;
      }
    }
  }

  int allzero = 1;
  ll ans = 1;
  for(int i = 0; i < n; i++) {
    vs[i] = vs[i] % ms[i];
    if(vs[i] != 0) allzero = 0;
    ans = ans * ms[i] % int(1e9 + 7);
  }
  if(allzero) {
    cout << ans << endl;
    return 0;
  }

  cout << garner(vs, ms, 1e9 + 7) << endl;
}
0