結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
assy1028
|
| 提出日時 | 2021-08-06 16:02:46 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 121 ms / 3,000 ms |
| コード長 | 4,699 bytes |
| コンパイル時間 | 7,463 ms |
| コンパイル使用メモリ | 147,556 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-17 00:30:21 |
| 合計ジャッジ時間 | 3,741 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 |
ソースコード
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;
#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
template<typename T, typename U>
size_t operator()(const pair<T, U> &x) const {
size_t seed = hash<T>()(x.first);
return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
};
const ll INF = 1e9 + 9;
const ll MOD = 1e9 + 7;
int n;
ll x[1010], y[1010];
class Garner {
private:
vector<ll> as;
vector<ll> ms;
bool all_zero = true;
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a%b);
}
// return: gcd(a, b)
// ax + by = gcd(a, b) を満たす (x, y) が格納される
ll ext_gcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = ext_gcd(b, a%b, y, x);
y -= a/b * x;
return d;
}
// ax = 1 (mod m) となる x を返す
// gcd(a, m) != 1 の場合 -1 を返す
ll mod_inverse(ll a, ll m) {
ll x, y;
ll g = ext_gcd(a, m, x, y);
if (g > 1) return -1;
if (x < 0) return m+x;
return x;
}
bool make_coprime() {
const int sz = as.size();
for (int i = 0; i < sz; i++) {
for (int j = i+1; j < sz; j++) {
ll g = gcd(ms[i], ms[j]);
if ((as[i] - as[j]) % g != 0) return false;
ms[i] /= g;
ms[j] /= g;
ll gi = gcd(ms[i], g);
ll gj = g / gi;
do {
g = gcd(gi, gj);
gi *= g;
gj /= g;
} while (g != 1);
ms[i] *= gi;
ms[j] *= gj;
as[i] %= ms[i];
as[j] %= ms[j];
}
}
return true;
}
ll lcm(const bool is_coprime, const ll MOD) {
if (!is_coprime) make_coprime();
ll res = 1;
if (MOD == 0) {
for (const ll m : ms) {
res *= m;
}
} else {
for (const ll m : ms) {
res *= m;
res %= MOD;
}
}
return res;
}
public:
void add_constraint(const ll a, const ll m) {
if (a != 0) all_zero = false;
as.push_back((a%m+m)%m);
ms.push_back(m);
}
ll calc(const bool is_coprime, const ll MOD = 0) {
if (all_zero) {
return lcm(is_coprime, MOD);
}
if (!is_coprime) {
// m を互いに素にする
if (!make_coprime())
return -1;
}
if (MOD == 0) {
const int sz = as.size();
ll m_prod = 1;
ll x = as[0] % ms[0];
for (int i = 1; i < sz; i++) {
m_prod *= ms[i-1];
ll t = (as[i] - x) * mod_inverse(m_prod, ms[i]) % ms[i];
if (t < 0) t += ms[i];
x += t * m_prod;
}
return x;
} else {
ms.push_back(MOD);
const int sz = ms.size();
vector<ll> coeffs(sz, 1);
vector<ll> constants(sz, 0);
for (int i = 0; i < (int)as.size(); i++) {
ll t = (as[i] - constants[i]) * mod_inverse(coeffs[i], ms[i]) % ms[i];
if (t < 0) t += ms[i];
for (int j = i+1; j < sz; j++) {
constants[j] += t * coeffs[j];
constants[j] %= ms[j];
coeffs[j] *= ms[i];
coeffs[j] %= ms[j];
}
}
return constants.back();
}
}
};
ll solve() {
Garner g;
for (int i = 0; i < n; i++) {
g.add_constraint(x[i], y[i]);
}
return g.calc(false, MOD);
}
void input() {
cin >> n;
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
input();
cout << solve() << endl;
}
assy1028