結果

問題 No.187 中華風 (Hard)
ユーザー しおだしおだ
提出日時 2020-09-19 08:38:02
言語 C++17(clang)
(14.0.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,779 bytes
コンパイル時間 1,831 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 09:28:30
合計ジャッジ時間 4,735 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 112 ms
4,380 KB
testcase_03 AC 110 ms
4,376 KB
testcase_04 AC 140 ms
4,380 KB
testcase_05 AC 140 ms
4,380 KB
testcase_06 AC 140 ms
4,380 KB
testcase_07 AC 140 ms
4,376 KB
testcase_08 AC 99 ms
4,380 KB
testcase_09 AC 99 ms
4,380 KB
testcase_10 AC 97 ms
4,380 KB
testcase_11 AC 140 ms
4,380 KB
testcase_12 AC 140 ms
4,380 KB
testcase_13 AC 31 ms
4,380 KB
testcase_14 AC 31 ms
4,376 KB
testcase_15 AC 107 ms
4,376 KB
testcase_16 AC 109 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 107 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 140 ms
4,380 KB
testcase_23 WA -
testcase_24 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
const int MOD = 1000000007;

long long gcd(long long a, long long b) {
	if (a < 0) a = -a;
	if (b < 0) b = -b;
	while (a != 0) {
		long long tmp = b % a;
		b = a;
		a = tmp;
	}
	return b;
}

long long mod(long long a) {
	a %= MOD;
	return a >= 0 ? a : a+MOD;
}

long long inverse_mod(long long a, long long mod) {
	long long b = mod, x = 1, u = 0;
	while (b != 0) {
		long long r = a % b, q = a / b;
		a = b; b = r;
		int nu = x - q*u;
		x = u; u = nu;
	}
	x %= mod;
	if (x < 0) x += mod;
	return x;
}

template<typename T> long long garner_mod(const vector<T> &r, const vector<T> &m, long long mod) {
	int n = r.size();
	assert(n == m.size());
	if (n == 0) return 0;
	vector<long long> c(n);
	long long x = 0, prod_m = 1;
	for (int i = 0; i < n; i++) {
		const long long mi = m[i];
		long long s = 0, p = 1;
		for (int j = 0; j < i; j++) {
			s = (s + p * c[j] % mi) % mi;
			p = p * m[j] % mi;
		}
		const long long tmp = (r[i] % mi - s) % mi;
		c[i] = inverse_mod(p, mi) * (tmp >= 0 ? tmp : tmp + mi) % mi;
		x = (x + c[i] * prod_m % mod) % mod;
		prod_m = prod_m * m[i] % mod;
	}
	if (x == 0) x = prod_m;
	return x;
}

int main() {
	int n;
	cin >> n;
	vector<long long> x(n), y(n);
	for (int i = 0; i < n; i++) {
		cin >> x[i] >> y[i];
	}
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			long long g = gcd(y[i], y[j]);
			if ((x[i] - x[j]) % g != 0) {
				cout << -1 << endl;
				return 0;
			}
			y[i] /= g, y[j] /= g;
			long long gi = gcd(y[i], g), gj = g / gi;
			do {
				g = gcd(gi, gj);
				gi *= g, gj /= g;
			} while (g != 1);
			y[i] *= gi, y[j] *= gj;
			x[i] %= y[i], x[j] %= y[j];
		}
	}
	long long ans = garner_mod(x, y, MOD);
	cout << ans << endl;
	return 0;
}
0