結果

問題 No.187 中華風 (Hard)
ユーザー suisensuisen
提出日時 2022-12-09 20:56:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 288 ms / 3,000 ms
コード長 4,750 bytes
コンパイル時間 2,414 ms
コンパイル使用メモリ 203,796 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 19:18:41
合計ジャッジ時間 7,000 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 213 ms
5,376 KB
testcase_03 AC 212 ms
5,376 KB
testcase_04 AC 285 ms
5,376 KB
testcase_05 AC 288 ms
5,376 KB
testcase_06 AC 287 ms
5,376 KB
testcase_07 AC 287 ms
5,376 KB
testcase_08 AC 199 ms
5,376 KB
testcase_09 AC 197 ms
5,376 KB
testcase_10 AC 199 ms
5,376 KB
testcase_11 AC 285 ms
5,376 KB
testcase_12 AC 286 ms
5,376 KB
testcase_13 AC 58 ms
5,376 KB
testcase_14 AC 59 ms
5,376 KB
testcase_15 AC 209 ms
5,376 KB
testcase_16 AC 215 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 220 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 287 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "test/math/garner/yuki187.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/187"

#line 2 "src/Template.hpp"

#define CUT
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define rrep(i, l, r) for (int i = (r); i --> (l);)
#define all(c) begin(c), end(c)

#ifdef LOCAL
#define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)
template <class H, class... Ts> void debug_impl(string s, H&& h, Ts&&... ts) {
  cerr << '(' << s << "): (" << forward<H>(h);
  ((cerr << ", " << forward<Ts>(ts)), ..., (cerr << ")\n"));
}
#else
#define debug(...) void(0)
#endif

template <class T> bool chmax(T& a, const T& b) { return b > a ? (a = b, true) : false; }
template <class T> bool chmin(T& a, const T& b) { return b < a ? (a = b, true) : false; }

template <class T> istream& operator>>(istream& in, vector<T>& v) {
  for (auto& e : v) in >> e;
  return in;
}
template <class ...Args> void read(Args&... args) {
  (cin >> ... >> args);
}
template <class T> ostream& operator<<(ostream& out, const vector<T>& v) {
  int n = v.size();
  rep(i, 0, n) {
    out << v[i];
    if (i + 1 != n) out << ' ';
  }
  return out;
}
template <class H, class ...Ts> void print(H&& h, Ts &&... ts) {
  cout << h, ((cout << ' ' << forward<Ts>(ts)), ..., (cout << '\n'));
}

struct io_setup_ {
  io_setup_() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cout << fixed << setprecision(10);
  }
} io_setup{};
#undef CUT

#define NOTE compile command: \texttt{g++ -std=gnu++17 -Wall -Wextra -g -fsanitize=address -fsanitize=undefined \$\{file\} -o \$\{fileDirname\}/\$\{fileBasenameNoExtension\}}
#undef NOTE
#define NOTE \texttt{-DLOCAL} を加えると \texttt{debug(...)} による出力が有効となる
#undef NOTE
#line 3 "src/math/Garner.hpp"
#define CUT
#line 3 "src/math/ExtGCD.hpp"

#define CUT
constexpr long safe_mod(long long x, long long m) {
  return (x %= m) < 0 ? x + m : x;
}
// Returns `(x,g)` s.t. `g=\gcd(a,b)`, `xa\equiv g \pmod{b}`, and `0\leq x< \frac{b}{g}`
constexpr pair<long long, long long> inv_gcd(long long a, long long b) {
  assert(b > 0);
  a = safe_mod(a, b);
  if (a == 0) return { 0, b };
  long long s = b, t = a, x = 0, y = 1;
  while (t) {
    long long u = s / t;
    s -= t * u, swap(s, t);
    x -= y * u, swap(x, y);
  }
  if (x < 0) x += b / s;
  return { x, s };
}
// Returns `x` s.t. `xa\equiv 1 \pmod{m}`.
// Requirement: `\gcd(a, m) = 1`.
constexpr long long inv_mod(long long a, long long m) {
  auto [x, g] = inv_gcd(a, m);
  assert(g == 1);
  return x;
}
// Returns `(x_0,y_0,g)` s.t. `g=\gcd(a,b)`, `ax_0 + by_0 = g`, and `0\leq x_0<\frac{b}{g}`.
// 一般解は `(x,y)=(x_0+k\cdot\dfrac{b}{g}, y_0-k\cdot\dfrac{a}{g})\;(k\in\mathbb{Z})` なので、`(x_0,y_0)` は `x` が非負の下で最小の解
constexpr tuple<long long, long long, long long> ext_gcd(long long a, long long b) {
  auto [x, g] = inv_gcd(a, b);
  return { x, (g - x * a) / b, g };
}
#undef CUT
#line 5 "src/math/Garner.hpp"
// Returns `x \bmod m` s.t. `\forall (x_i,m_i) \in \mathtt{eq}.\;x=x_i \pmod{m_i}`.
// Time Complexity: `\Theta(|\mathtt{eq}| ^ 2 \log \max m_i)`
// Requairement: `m_i` are coprime
int garner_coprime(vector<pair<int, int>> eq, int m) {
  const int n = eq.size();
  vector<long long> a(n);
  auto f = [&](int i, long long mod) {
    long long res = 0, prd = 1;
    rep(j, 0, i) {
      (res += a[j] * prd) %= mod;
      (prd *= eq[j].second) %= mod;
    }
    return res;
  };
  rep(i, 0, n) {
    auto [xi, mi] = eq[i];
    a[i] = safe_mod(xi - f(i, mi), mi);
    rep(j, 0, i) (a[i] *= inv_mod(eq[j].second, mi)) %= mi;
  }
  return f(n, m);
}
// Returns `(x \bmod m, (\mathrm{lcm}_i\; m_i) \bmod m)` s.t. `\forall (x_i,m_i) \in \mathtt{eq}.\;x=x_i \pmod{m_i}`.
// Time Complexity: `\Theta(|\mathtt{eq}| ^ 2 \log \max m_i)`
pair<int, int> garner(vector<pair<int, int>> eq, int m) {
  const int n = eq.size();
  rep(i, 0, n) {
    auto &[xi, mi] = eq[i];
    rep(j, 0, i) {
      auto &[xj, mj] = eq[j];
      long long g = gcd(mi, mj);
      if ((xi - xj) % g) return { -1, -1 };
      mi /= g, mj /= g;
      long long gi = gcd(mi, g);
      long long gj = g / gi;
      do g = gcd(gi, gj), gi *= g, gj /= g; while (g != 1);
      mi *= gi, mj *= gj, xi %= mi, xj %= mj;
    }
  }
  long long l = 1;
  for (auto &[_, mi] : eq) l = (l * mi) % m;
  return { garner_coprime(eq, m), l };
}
#undef CUT
#line 4 "test/math/garner/yuki187.test.cpp"

int main() {
  int n;
  read(n);
  bool all_zero = true;
  vector<pair<int, int>> eq(n);
  for (auto &[x, m] : eq) {
    read(x, m);
    all_zero &= x == 0;
  }
  const int m = 1000000007;
  auto [ans, lcm] = garner(eq, m);
  if (ans == -1) {
    print(-1);
  } else {
    print(all_zero ? lcm : ans);
  }
}
0