結果

問題 No.187 中華風 (Hard)
ユーザー legosukelegosuke
提出日時 2019-06-06 14:28:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 3,000 ms
コード長 2,844 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 173,424 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-25 23:54:33
合計ジャッジ時間 5,519 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ---------------------------------------------------------------------------------------------------- */

int gcd(int a, int b) {
  while (b) {
    int c = a % b;
    a = b; b = c;
  }
  return a;
}

// garner のアルゴリズムの前処理
// m_1,m_2,... を互いに素にする
// lcm(m_1,m_2,...) を返す
int pre_garner(vector<int> &b, vector<int> &m, int mod = MOD) {
  int res = 1;
  for (int i = 0; i < (int)b.size(); i++) {
    for (int j = 0; j < i; j++) {
      int g = gcd(m[i],m[j]);
      if ((b[i]-b[j]) % g != 0) return -1;
      m[i] /= g;
      m[j] /= g;
      int gi = gcd(m[i],g), gj = g/gi;
      do {
        g = gcd(gi,gj);
        gi *= g, gj /= g;
      } while (g != 1);
      m[i] *= gi, m[j] *= gj;
      b[i] %= m[i], b[j] %= m[j];
    }
  }
  for (int i = 0; i < (int)b.size(); i++) (res *= m[i]) %= mod;
  return res;
}

int inv_mod(int a, int mod = MOD) {
  int b = mod, x = 1, y = 0;
  while (b) {
    int t = a/b;
    a -= t*b; swap(a,b);
    x -= t*y; swap(x,y);
  }
  x %= mod;
  if (x < 0) x += mod;
  return x;
}

// garner のアルゴリズム | (m_i,m_j) は互いに素
int garner(vector<int> b, vector<int> m, int mod = MOD) {
  m.push_back(mod);
  vector<int> coeffs((int)m.size(),1);
  vector<int> constants((int)m.size(),0);
  for (int k = 0; k < (int)b.size(); k++) {
    int t = ((b[k]-constants[k])*inv_mod(coeffs[k],m[k])%m[k]+m[k])%m[k];
    for (int i = k+1; i < (int)m.size(); i++) {
      (constants[i] += t*coeffs[i]) %= m[i];
      (coeffs[i] *= m[k]) %= m[i];
    }
  }
  return constants.back();
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int N;
  cin >> N;
  vector<int> b(N),m(N);
  bool all_zero = true;
  REP(i,N) {
    cin >> b[i] >> m[i];
    if (b[i]) all_zero = false;
  }
  int l = pre_garner(b,m);
  if (all_zero) cout << l << endl;
  else if (l < 0) cout << -1 << endl;
  else cout << garner(b,m) << endl;

  return 0;
}
0