結果

問題 No.187 中華風 (Hard)
ユーザー tactac
提出日時 2020-02-04 23:11:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,527 bytes
コンパイル時間 1,652 ms
コンパイル使用メモリ 185,148 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 06:40:02
合計ジャッジ時間 7,493 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 218 ms
4,348 KB
testcase_01 AC 214 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
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 WA -
testcase_16 WA -
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 171 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
const int inf = 1e9 + 7;
const ll INF = 1e18;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> T pow(T a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
const int mod = 1000000007;
ll modpow(ll a, int b) { return b ? modpow(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& v) { rep(i, sz(v)) { if (i) os << " "; os << v[i]; } return os; }
template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.F << " " << p.S; return os; }
template<class T> inline void add(T &a, int b) { a += b; if (a >= mod) a - mod; }

void solve();

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int T;
    // cin >> T;
    T = 1;

    while (T--) {
      solve();
    }
}

// https://atcoder.jp/contests/ddcc2019-qual/submissions/9899069

ll ext_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll x2, y2;
  ll d = ext_gcd(b, a % b, x2, y2);
  x = y2;
  y = x2 - (a / b) * y2;
  return d;
}

ll mod_inv(ll a, ll m) {
  ll x, y;
  ll d = ext_gcd(a, m, x, y); // ax + my = 1, ax ≡ 1
  if (d != 1) {
    return -1;
  }
  x %= m;
  if (x < 0) x += m;
  return x;
}

ll garner(vector<ll> r, vector<ll> m) {
  int n = sz(r);
  ll m_prod = 1;
  ll x = r[0] % m[0];
  rep3(i, 1, n) {
    m_prod *= m[i - 1];
    ll t = (r[i] - x) * mod_inv(m_prod, m[i]);
    t %= m[i];
    if (t < 0) t += m[i];
    x += t * m_prod;
  }
  return x;
}

map<ll, int> prime_factorize(ll n) {
    ll tmp = n;
    map<ll, int> mp;
    for (ll i = 2; i * i <= n; ++i) {
        ll cnt = 0;
        while (tmp % i == 0) {
            tmp /= i;
            cnt++;
        }
        if (cnt) mp[i] = cnt;
    }
    if (tmp != 1) mp[tmp] = 1;
    return mp;
}

void solve() {
  int n;
  cin >> n;
  vector<pair<ll, ll> > g(n);
  rep(i, n) cin >> g[i].F >> g[i].S;

  map<ll, int> mp, mp2;
  vector<map<ll, int> > v(n);
  rep(i, n) v[i] = prime_factorize(g[i].S);
  rep(i, n) {
    for (auto e : v[i]) {
      if (chmax(mp[e.F], e.S)) {
        mp2[e.F] = i;
      }
    }
  }
  // for (auto e : mp) cout << e.F << " " << e.S << " " << mp2[e.F] << endl;

  vector<ll> r(n), m(n);
  rep(i, n) r[i] = g[i].F, m[i] = g[i].S;
  for (auto e : mp) {
    rep(i, n) {
      if (!v[i].count(e.F) || i == mp2[e.F]) continue;

      ll d = pow(e.F, v[i][e.F]);

      m[i] /= d;
      r[i] %= m[i];
    }
    // cout << endl << e.F << endl;
    // rep(i, n) cout << g[i] << endl;
  }
  ll x = garner(r, m);

  rep(i, n) { // sample3 のような, y が素数のとき 1 0 になる
    if (x % g[i].S != g[i].F) {
      cout << -1 << endl;
      return;
    }
  }
  cout << x << endl;
}
0