結果

問題 No.187 中華風 (Hard)
ユーザー __math__math
提出日時 2015-05-07 00:10:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 115 ms / 3,000 ms
コード長 3,174 bytes
コンパイル時間 1,051 ms
コンパイル使用メモリ 102,272 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-19 07:19:34
合計ジャッジ時間 3,771 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 94 ms
4,380 KB
testcase_03 AC 93 ms
4,380 KB
testcase_04 AC 114 ms
4,376 KB
testcase_05 AC 114 ms
4,376 KB
testcase_06 AC 115 ms
4,380 KB
testcase_07 AC 114 ms
4,380 KB
testcase_08 AC 67 ms
4,380 KB
testcase_09 AC 68 ms
4,376 KB
testcase_10 AC 67 ms
4,376 KB
testcase_11 AC 114 ms
4,376 KB
testcase_12 AC 114 ms
4,380 KB
testcase_13 AC 16 ms
4,380 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 73 ms
4,376 KB
testcase_16 AC 76 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 87 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 114 ms
4,384 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0