結果

問題 No.187 中華風 (Hard)
ユーザー goodbatongoodbaton
提出日時 2019-10-30 11:26:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 212 ms / 3,000 ms
コード長 3,117 bytes
コンパイル時間 962 ms
コンパイル使用メモリ 101,956 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 23:41:54
合計ジャッジ時間 4,830 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 169 ms
4,352 KB
testcase_03 AC 165 ms
4,352 KB
testcase_04 AC 211 ms
4,352 KB
testcase_05 AC 210 ms
4,352 KB
testcase_06 AC 211 ms
4,352 KB
testcase_07 AC 211 ms
4,352 KB
testcase_08 AC 145 ms
4,352 KB
testcase_09 AC 144 ms
4,348 KB
testcase_10 AC 144 ms
4,352 KB
testcase_11 AC 212 ms
4,352 KB
testcase_12 AC 211 ms
4,352 KB
testcase_13 AC 64 ms
4,352 KB
testcase_14 AC 64 ms
4,348 KB
testcase_15 AC 155 ms
4,352 KB
testcase_16 AC 158 ms
4,348 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 161 ms
4,352 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 210 ms
4,352 KB
testcase_23 AC 1 ms
4,352 KB
testcase_24 AC 1 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

// {a, {b, c}} : a = gcd(p, q) = bp + cq
pair<ll,pair<ll,ll>> ext_gcd(ll p, ll q) {
  if (q == 0) return {p, {1, 0}};
  auto r = ext_gcd(q, p%q);
  return {r.first, {r.second.second, r.second.first - p/q*r.second.second}};
}


// GCC __gcd(long long A, long long B)

ll gcd(ll a, ll b){
  if(a == 0) return b;
  return gcd(b%a, a);
}

ll lcm(ll a, ll b){
  return a / gcd(a, b) * b;
}


/* Garner */
// modsは互いに素

ll Garner(vector<ll> b, vector<ll> mods, ll MOD) {
  mods.push_back(MOD);
  vector<ll> coeffs((int)mods.size(), 1);
  vector<ll> constants((int)mods.size(), 0);
  for (int i = 0; i < (int)b.size(); i++) {
    ll m = mods[i];
    ll s = (ext_gcd(coeffs[i], m).second.first % m + m) % m;
    ll t = ((b[i] - constants[i]) * s % m + m) % m;
    for (int j = i+1; j < (int)mods.size(); j++) {
      constants[j] = (constants[j] + t * coeffs[j]) % mods[j];
      coeffs[j] = (coeffs[j] * m) % mods[j];
    }
  }
  return constants.back();
}

//破壊的
ll preGarner(vector<ll> &b, vector<ll> &mods, ll MOD) {
  ll res = 1;
  for (int i = 0; i < (int)b.size(); i++) {
    for (int j = 0; j < i; j++) {
      ll g = gcd(mods[i], mods[j]);
      if ((b[i] - b[j]) % g != 0) return -1;
      mods[i] /= g;
      mods[j] /= g;
      ll gi = gcd(mods[i], g);
      ll gj = g / gi;
      do {
        g = gcd(gi, gj);
        gi *= g; gj /= g;
      } while (g != 1);
      mods[i] *= gi; mods[j] *= gj;
      b[i] %= mods[i]; b[j] %= mods[j];
    }
  }
  for (int i = 0; i < (int)b.size(); ++i)
    res = res * mods[i] % MOD;
  return res;
}

// ll lcm = preGarner(x, y, mod);
// if (lcm == -1) //NG; else if (forall x is 0) //lcm or 0 else ans = Garner(x, y);

int main(){
  int N;
  vector<ll> mods, vals;
  bool zeroFlag = true;

  cin >> N;

  for (int i=0; i<N; i++) {
    ll x, y;
    cin >> x >> y;
    vals.push_back(x);
    mods.push_back(y);
    zeroFlag &= x == 0;
  }

  ll lcm = preGarner(vals, mods, mod);

  debug(vals);
  debug(mods);

  if (lcm == -1) {
    puts("-1");
  } else if (zeroFlag) {
    printf("%lld\n", lcm);
  } else {
    printf("%lld\n", Garner(vals, mods, mod));
  }

  return 0;
}
0