結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
kazuma
|
| 提出日時 | 2018-03-24 07:37:26 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 369 ms / 3,000 ms |
| コード長 | 2,204 bytes |
| コンパイル時間 | 2,367 ms |
| コンパイル使用メモリ | 209,920 KB |
| 最終ジャッジ日時 | 2025-01-05 09:33:43 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
ll power(ll x, int n) {
ll res = 1;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
ll mod_inv(ll a, ll b) {
ll u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
return u;
}
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;
}
class CRT {
bool ng;
map<ll, pair<int, ll>> facts;
public:
CRT() : ng(false) {}
void add(ll x, ll m) {
if (ng) return;
for (auto& p : factorize(m)) {
if (!facts.count(p.first)) {
facts[p.first] = make_pair(p.second, x % power(p.first, p.second));
continue;
}
auto q = facts[p.first];
if ((x - q.second) % power(p.first, min(p.second, q.first)) != 0) {
ng = true;
return;
}
if (p.second > q.first) {
facts[p.first] = make_pair(p.second, x % power(p.first, p.second));
}
}
}
int solve(int md) const {
if (ng) return -1;
bool zero = true;
vector<pair<ll, ll>> mr;
for (auto p : facts) {
mr.emplace_back(power(p.first, p.second.first), p.second.second);
if (p.second.second != 0) zero = false;
}
if (zero) {
ll res = 1;
for (auto p : facts) {
(res *= power(p.first, p.second.first)) %= md;
}
return res;
}
mr.emplace_back(md, 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();
}
};
int main()
{
int N;
cin >> N;
CRT crt;
for (int i = 0; i < N; i++) {
int X, Y;
cin >> X >> Y;
crt.add(X, Y);
}
cout << crt.solve(mod) << endl;
return 0;
}
kazuma