結果

問題 No.187 中華風 (Hard)
ユーザー SymmetricHorizonSymmetricHorizon
提出日時 2022-02-02 20:00:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,371 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 169,964 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 02:55:14
合計ジャッジ時間 5,428 ms
ジャッジサーバーID
(参考情報)
judge12 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (decltype(n) i = 0; i < (n); ++i)
#define rep3(i, s, n) for (decltype(n) i = (s); i < (n); ++i)
#define repR(i, n) for (decltype(n) i = (n)-1; i >= 0; --i)
#define rep3R(i, s, n) for (decltype(n) i = (n)-1; i >= (s); --i)
#define repit(it, c) for (auto it = (c).begin(); it != (c).end(); ++it)
#define all(x) ::std::begin(x), ::std::end(x)

using ll = long long;

template <typename T, typename U>
inline bool chmax(T &current, const U &test) {
  if (current < test) {
    current = test;
    return true;
  }
  return false;
}
template <typename T, typename U>
inline bool chmin(T &current, const U &test) {
  if (current > test) {
    current = test;
    return true;
  }
  return false;
}

const int64_t INFL = pow(10, 18);
const int INF = 2 * pow(10, 9);

const long long MOD = 1000000007;
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
ll ext_Euclid(ll n, ll m, ll &p, ll &q) {
  if (m == 0) {
    p = 1;
    q = 0;
    return n;
  }
  ll d = ext_Euclid(m, n % m, q, p);
  q -= n / m * p;
  return d;
}
ll inv_mod(ll n, ll m) {
  ll p, q;
  ext_Euclid(n, m, p, q);
  return (p + m) % m;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  constexpr char endl = '\n';
  ///////////////////////////

  int n;
  cin >> n;
  vector<ll> x(n), y(n);
  rep(i, n) { cin >> x[i] >> y[i]; }

  // Garner のアルゴリズムを使う為に y 同士を互いに素にする。
  rep(i, n - 1) {
    rep3(j, i + 1, n) {
      ll g = gcd(y[i], y[j]);
      if ((x[i] - x[j]) % g != 0) {
        cout << -1 << endl;
        return 0;
      }
      y[i] /= g, y[j] /= g;
      ll gi = gcd(y[i], g), gj = g / gi;
      g = gcd(gi, gj);
      gi *= g;
      gj /= g;
      assert(gcd(gi, gj) == 1);
      y[i] *= gi, y[j] *= gj;
      x[i] %= y[i], x[j] %= y[j];
    }
  }
  ll lcm = 1;
  rep(i, n) { (lcm *= y[i]) %= MOD; }

  // Garner のアルゴリズム
  y.emplace_back(MOD);
  vector<ll> coeffs(n + 1, 1);
  vector<ll> consts(n + 1, 0);
  rep(i, n) {
    // coeff * t + const = b
    ll t = ((x[i] - consts[i]) * inv_mod(coeffs[i], y[i]) + y[i]) % y[i];
    rep3(j, i + 1, n + 1) {
      (consts[j] += t * coeffs[i]) %= y[j];
      (coeffs[j] *= y[i]) %= y[j];
    }
  }

  if (consts[n] == 0) {
    consts[n] = lcm;
  }
  cout << consts[n] << endl;
}
0