結果

問題 No.187 中華風 (Hard)
ユーザー risujirohrisujiroh
提出日時 2019-02-03 02:29:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 370 ms / 3,000 ms
コード長 2,567 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 168,324 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-20 11:44:24
合計ジャッジ時間 7,309 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 267 ms
4,380 KB
testcase_03 AC 266 ms
4,380 KB
testcase_04 AC 370 ms
4,376 KB
testcase_05 AC 369 ms
4,376 KB
testcase_06 AC 365 ms
4,376 KB
testcase_07 AC 362 ms
4,376 KB
testcase_08 AC 232 ms
4,376 KB
testcase_09 AC 231 ms
4,380 KB
testcase_10 AC 232 ms
4,376 KB
testcase_11 AC 366 ms
4,376 KB
testcase_12 AC 364 ms
4,380 KB
testcase_13 AC 73 ms
4,376 KB
testcase_14 AC 73 ms
4,376 KB
testcase_15 AC 143 ms
4,380 KB
testcase_16 AC 145 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 277 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 367 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using lint = long long int;
using ulint = unsigned long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T, class U> void assign(V<T>& v, int n, const U& a) { v.assign(n, a); }
template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); }


lint tmod(lint a, lint p) {
  assert(p > 0);
  return (a %= p) < 0 ? a + p : a;
}

lint mod_inv(lint a, lint p) {
  a = tmod(a, p);
  lint b = p, x = 1, u = 0;
  while (b) {
    lint q = a / b;
    swap(a -= q * b, b);
    swap(x -= q * u, u);
  }
  return a == 1 ? tmod(x, p) : -1;
}

bool pre(V<lint>& a, V<lint>& p) {
  int n = a.size();
  assert(p.size() == n);
  for (int i = 0; i < n; ++i) {
    a[i] = tmod(a[i], p[i]);
  }
  for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) {
    lint d = __gcd(p[i], p[j]);
    if (a[i] % d != a[j] % d) return false;
    p[i] /= d;
    p[j] /= d;
    while (true) {
      lint e = __gcd(d, p[j]);
      if (e == 1) break;
      p[j] *= e;
      d /= e;
    }
    p[i] *= d;
    a[i] %= p[i];
    a[j] %= p[j];
  }
  return true;
}

lint CRT(const V<lint>& a, const V<lint>& p) {
  int n = a.size();
  lint x = 0;
  V<lint> y(n);
  lint prod = 1;
  for (int i = 0; i < n; ++i) {
    y[i] = tmod(a[i] - x, p[i]);
    for (int j = 0; j < i; ++j) {
      (y[i] *= mod_inv(p[j], p[i])) %= p[i];
    }
    x += prod * y[i];
    prod *= p[i];
  }
  return x;
}

lint CRT(const V<lint>& a, const V<lint>& p, lint mod) {
  int n = a.size();
  V<lint> y(n);
  for (int i = 0; i < n; ++i) {
    y[i] = a[i];
    lint prod = 1;
    for (int j = 0; j < i; ++j) {
      y[i] -= prod * y[j] % p[i];
      (prod *= p[j]) %= p[i];
    }
    y[i] = tmod(y[i], p[i]);
    for (int j = 0; j < i; ++j) {
      (y[i] *= mod_inv(p[j], p[i])) %= p[i];
    }
  }
  lint res = 0, prod = 1;
  for (int i = 0; i < n; ++i) {
    res += prod * y[i] % mod;
    (prod *= p[i]) %= mod;
  }
  return res % mod;
}

constexpr lint mod = 1e9 + 7;

int main() {
  cin.tie(nullptr); ios_base::sync_with_stdio(false);
  int n; cin >> n;
  V<lint> a(n), p(n); for (int i = 0; i < n; ++i) cin >> a[i] >> p[i];
  if (!pre(a, p)) return cout << -1 << '\n', 0;
  if (all_of(begin(a), end(a), [](lint e) { return e == 0; })) {
    cout << accumulate(begin(p), end(p), 1LL, [&](lint acc, lint e) { return acc * e % mod; }) << '\n';
    return 0;
  }
  lint res = CRT(a, p, mod);
  cout << res << '\n';
}
0