結果

問題 No.187 中華風 (Hard)
ユーザー kazumakazuma
提出日時 2017-11-29 21:49:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 3,000 ms
コード長 2,451 bytes
コンパイル時間 3,380 ms
コンパイル使用メモリ 217,636 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 13:49:03
合計ジャッジ時間 6,715 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 52 ms
4,376 KB
testcase_03 AC 49 ms
4,376 KB
testcase_04 AC 174 ms
4,380 KB
testcase_05 AC 165 ms
4,380 KB
testcase_06 AC 170 ms
4,376 KB
testcase_07 AC 174 ms
4,376 KB
testcase_08 AC 339 ms
4,376 KB
testcase_09 AC 339 ms
4,376 KB
testcase_10 AC 339 ms
4,376 KB
testcase_11 AC 170 ms
4,376 KB
testcase_12 AC 176 ms
4,376 KB
testcase_13 AC 253 ms
4,376 KB
testcase_14 AC 207 ms
4,376 KB
testcase_15 AC 51 ms
4,380 KB
testcase_16 AC 56 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 146 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 173 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll mod = 1e9 + 7;

ll pow(ll x, int n) {
	ll res = 1;
	while (n) {
		if (n & 1) res *= x;
		x *= x;
		n >>= 1;
	}
	return res;
}

ll mod_pow(ll x, ll n, ll md) {
	ll res = 1;
	while (n) {
		if (n & 1) (res *= x) %= md;
		(x *= x) %= md;
		n >>= 1;
	}
	return res;
}

ll extgcd(ll a, ll b, ll& x, ll& y) {
	ll d = a;
	if (b != 0) {
		d = extgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
	else {
		x = 1;
		y = 0;
	}
	return d;
}

ll mod_inv(ll a, ll md) {
	ll x, y;
	extgcd(a, md, x, y);
	return (x % md + md) % md;
}

ll garner(vector<pll> mr, ll mod) {
	mr.emplace_back(mod, 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();
}

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

bool add_factor(ll x, ll m, map<ll, pair<int, ll>>& facts) {
	auto fs = factorize(m);
	for (auto p : fs) {
		if (!facts.count(p.first)) {
			facts[p.first] = make_pair(p.second, x % pow(p.first, p.second));
			continue;
		}
		auto q = facts[p.first];
		if ((x - q.second) % pow(p.first, min(p.second, q.first)) != 0) return false;
		if (p.second > q.first) {
			facts[p.first] = make_pair(p.second, x % pow(p.first, p.second));
		}
	}
	return true;
}

int main()
{
	int N;
	cin >> N;
	map<ll, pair<int, ll>> facts;
	bool ok = true;
	for (int i = 0; i < N; i++) {
		int X, Y;
		cin >> X >> Y;
		ok = ok && add_factor(X, Y, facts);
		if (!ok) {
			cout << -1 << endl;
			return 0;
		}
	}
	bool zero = true;
	vector<pll> mr;
	for (auto p : facts) {
		mr.emplace_back(pow(p.first, p.second.first), p.second.second);
		if (p.second.second != 0) zero = false;
	}
	ll res = garner(mr, mod);
	if (zero) {
		res = 1;
		for (auto p : facts) {
			(res *= pow(p.first, p.second.first)) %= mod;
		}
	}
	cout << res << endl;
	return 0;
}
0