結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
__math
|
| 提出日時 | 2015-05-07 00:10:44 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 112 ms / 3,000 ms |
| コード長 | 3,174 bytes |
| コンパイル時間 | 889 ms |
| コンパイル使用メモリ | 104,968 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-05 19:21:38 |
| 合計ジャッジ時間 | 3,048 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 |
ソースコード
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <limits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <iterator>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())
#define ten(x) ((int)1e##x)
#define tenll(x) ((ll)1e##x)
// #pragma comment(linker,"/STACK:36777216")
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> void chmax(T& l, const T r) { l = max(l, r); }
template<class T> void chmin(T& l, const T r) { l = min(l, r); }
template<class T> T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; }
template<class T> T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; }
ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
// x % m[i] == r[i] を満たす,最小の0以上の整数x について, x % mod を求める
// ex)
// x % 5 == 4
// x % 7 == 1
// x % 11 == 2
// x % 13 = ?
// -> x % (5*7*11) = 134 より, 最小のxは 134
// よって, x % 13 = 4 を返す
ll garner(vector<Pii>& mr, int mod){
mr.emplace_back(mod, 0);
vector<ll> coffs(sz(mr), 1);
vector<ll> constants(sz(mr), 0);
FOR(i, sz(mr) - 1){
// coffs[i] * v + constants[i] == mr[i].second (mod mr[i].first) を解く
ll v = (mr[i].second - constants[i]) * mod_inv<ll>(coffs[i], mr[i].first) % mr[i].first;
if (v < 0) v += mr[i].first;
for (int j = i + 1; j < sz(mr); j++) {
(constants[j] += coffs[j] * v) %= mr[j].first;
(coffs[j] *= mr[i].first) %= mr[j].first;
}
}
return constants[sz(mr) - 1];
}
void test(){
vector<Pii> mr;
mr.emplace_back(5, 4);
mr.emplace_back(7, 1);
mr.emplace_back(11, 2);
auto ans = garner(mr, 13);
assert(ans == 4);
}
int solve(vector<Pii>& v){
const int n = sz(v);
FOR(i, n){
for (int j = i + 1; j < n; j++) {
int g = gcd(v[i].first, v[j].first);
if (g == 1) continue;
if (v[i].second % g != v[j].second % g) {
return -1;
}
v[i].first /= g;
v[i].second %= v[i].first;
}
}
return (int)garner(v, ten(9) + 7);
}
int main(){
int n; cin >> n;
vector<Pii> v;
FOR(i, n){
int x, y; cin >> x >> y;
v.emplace_back(y, x);
}
bool all_zero = true;
for (auto mr : v) if (mr.second != 0) all_zero = false;
if (all_zero) {
FOR(i, n) FOR(j, i) v[i].first /= gcd(v[i].first, v[j].first);
ll ans = 1;
for (auto mr : v) ans = ans * mr.first % (ten(9) + 7);
cout << ans << endl;
return 0;
}
int ans = solve(v);
cout << ans << endl;
}
__math