結果

問題 No.187 中華風 (Hard)
ユーザー kazumakazuma
提出日時 2018-03-24 07:37:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 326 ms / 3,000 ms
コード長 2,204 bytes
コンパイル時間 2,462 ms
コンパイル使用メモリ 216,396 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-07 08:22:40
合計ジャッジ時間 6,282 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 48 ms
4,380 KB
testcase_03 AC 45 ms
4,376 KB
testcase_04 AC 162 ms
4,380 KB
testcase_05 AC 155 ms
4,380 KB
testcase_06 AC 156 ms
4,380 KB
testcase_07 AC 162 ms
4,380 KB
testcase_08 AC 326 ms
4,376 KB
testcase_09 AC 318 ms
4,376 KB
testcase_10 AC 316 ms
4,376 KB
testcase_11 AC 164 ms
4,380 KB
testcase_12 AC 163 ms
4,380 KB
testcase_13 AC 240 ms
4,380 KB
testcase_14 AC 198 ms
4,500 KB
testcase_15 AC 38 ms
4,376 KB
testcase_16 AC 42 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 145 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 178 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const ll mod = 1e9 + 7;

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

ll mod_inv(ll a, ll b) {
	ll u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	return u;
}

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

class CRT {
	bool ng;
	map<ll, pair<int, ll>> facts;
public:
	CRT() : ng(false) {}
	void add(ll x, ll m) {
		if (ng) return;
		for (auto& p : factorize(m)) {
			if (!facts.count(p.first)) {
				facts[p.first] = make_pair(p.second, x % power(p.first, p.second));
				continue;
			}
			auto q = facts[p.first];
			if ((x - q.second) % power(p.first, min(p.second, q.first)) != 0) {
				ng = true;
				return;
			}
			if (p.second > q.first) {
				facts[p.first] = make_pair(p.second, x % power(p.first, p.second));
			}
		}
	}
	int solve(int md) const {
		if (ng) return -1;
		bool zero = true;
		vector<pair<ll, ll>> mr;
		for (auto p : facts) {
			mr.emplace_back(power(p.first, p.second.first), p.second.second);
			if (p.second.second != 0) zero = false;
		}
		if (zero) {
			ll res = 1;
			for (auto p : facts) {
				(res *= power(p.first, p.second.first)) %= md;
			}
			return res;
		}
		mr.emplace_back(md, 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();
	}
};

int main()
{
	int N;
	cin >> N;
	CRT crt;
	for (int i = 0; i < N; i++) {
		int X, Y;
		cin >> X >> Y;
		crt.add(X, Y);
	}
	cout << crt.solve(mod) << endl;
	return 0;
}
0