結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
kazuma
|
| 提出日時 | 2017-11-29 21:49:32 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 368 ms / 3,000 ms |
| コード長 | 2,451 bytes |
| コンパイル時間 | 2,495 ms |
| コンパイル使用メモリ | 211,468 KB |
| 最終ジャッジ日時 | 2025-01-05 04:33:32 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll mod = 1e9 + 7;
ll pow(ll x, int n) {
ll res = 1;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
ll mod_pow(ll x, ll n, ll md) {
ll res = 1;
while (n) {
if (n & 1) (res *= x) %= md;
(x *= x) %= md;
n >>= 1;
}
return res;
}
ll extgcd(ll a, ll b, ll& x, ll& y) {
ll d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
}
else {
x = 1;
y = 0;
}
return d;
}
ll mod_inv(ll a, ll md) {
ll x, y;
extgcd(a, md, x, y);
return (x % md + md) % md;
}
ll garner(vector<pll> mr, ll mod) {
mr.emplace_back(mod, 0);
int n = mr.size();
vector<ll> coffs(n, 1);
vector<ll> constants(n, 0);
for (int i = 0; i < n - 1; i++) {
ll v = (mr[i].second - constants[i]) * mod_inv(coffs[i], mr[i].first) % mr[i].first;
if (v < 0) v += mr[i].first;
for (int j = i + 1; j < n; j++) {
(constants[j] += coffs[j] * v) %= mr[j].first;
(coffs[j] *= mr[i].first) %= mr[j].first;
}
}
return constants.back();
}
vector<pair<ll, int>> factorize(ll n) {
vector<pair<ll, int>> res;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
res.emplace_back(i, 0);
while (n % i == 0) {
res.back().second++;
n /= i;
}
}
}
if (n != 1) {
if (!res.empty() && res.back().first == n) res.back().second++;
else res.emplace_back(n, 1);
}
return res;
}
bool add_factor(ll x, ll m, map<ll, pair<int, ll>>& facts) {
auto fs = factorize(m);
for (auto p : fs) {
if (!facts.count(p.first)) {
facts[p.first] = make_pair(p.second, x % pow(p.first, p.second));
continue;
}
auto q = facts[p.first];
if ((x - q.second) % pow(p.first, min(p.second, q.first)) != 0) return false;
if (p.second > q.first) {
facts[p.first] = make_pair(p.second, x % pow(p.first, p.second));
}
}
return true;
}
int main()
{
int N;
cin >> N;
map<ll, pair<int, ll>> facts;
bool ok = true;
for (int i = 0; i < N; i++) {
int X, Y;
cin >> X >> Y;
ok = ok && add_factor(X, Y, facts);
if (!ok) {
cout << -1 << endl;
return 0;
}
}
bool zero = true;
vector<pll> mr;
for (auto p : facts) {
mr.emplace_back(pow(p.first, p.second.first), p.second.second);
if (p.second.second != 0) zero = false;
}
ll res = garner(mr, mod);
if (zero) {
res = 1;
for (auto p : facts) {
(res *= pow(p.first, p.second.first)) %= mod;
}
}
cout << res << endl;
return 0;
}
kazuma