結果

問題 No.187 中華風 (Hard)
ユーザー SymmetricHorizonSymmetricHorizon
提出日時 2022-02-02 20:12:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 196 ms / 3,000 ms
コード長 2,464 bytes
コンパイル時間 1,724 ms
コンパイル使用メモリ 170,120 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-02 02:56:58
合計ジャッジ時間 5,459 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 164 ms
4,380 KB
testcase_03 AC 160 ms
4,380 KB
testcase_04 AC 195 ms
4,380 KB
testcase_05 AC 196 ms
4,380 KB
testcase_06 AC 196 ms
4,384 KB
testcase_07 AC 196 ms
4,380 KB
testcase_08 AC 129 ms
4,380 KB
testcase_09 AC 129 ms
4,384 KB
testcase_10 AC 129 ms
4,380 KB
testcase_11 AC 195 ms
4,380 KB
testcase_12 AC 195 ms
4,388 KB
testcase_13 AC 58 ms
4,384 KB
testcase_14 AC 58 ms
4,384 KB
testcase_15 AC 161 ms
4,380 KB
testcase_16 AC 162 ms
4,380 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 150 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 195 ms
4,384 KB
testcase_23 AC 2 ms
4,384 KB
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);
  bool all_zero = true;
  rep(i, n) { cin >> x[i] >> y[i]; 
    if(x[i]){all_zero = false;}
  }

  // 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;
      while(g != 1) {
        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 = x
    ll t = ((x[i] - consts[i] + y[i]) % y[i] * inv_mod(coeffs[i], y[i]) + y[i]) % y[i];
    rep3(j, i + 1, n + 1) {
      (consts[j] += t * coeffs[j]) %= y[j];
      (coeffs[j] *= y[i]) %= y[j];
    }
  }

  ll ans = all_zero? lcm: consts[n];
  cout << ans << endl;
}
0