結果

問題 No.187 中華風 (Hard)
ユーザー kazumakazuma
提出日時 2018-11-24 00:54:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 357 ms / 3,000 ms
コード長 2,321 bytes
コンパイル時間 2,322 ms
コンパイル使用メモリ 221,640 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 04:44:06
合計ジャッジ時間 6,950 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
4,380 KB
testcase_01 AC 38 ms
4,376 KB
testcase_02 AC 54 ms
4,380 KB
testcase_03 AC 52 ms
4,376 KB
testcase_04 AC 182 ms
4,380 KB
testcase_05 AC 175 ms
4,376 KB
testcase_06 AC 177 ms
4,380 KB
testcase_07 AC 181 ms
4,376 KB
testcase_08 AC 357 ms
4,376 KB
testcase_09 AC 357 ms
4,376 KB
testcase_10 AC 356 ms
4,376 KB
testcase_11 AC 178 ms
4,376 KB
testcase_12 AC 184 ms
4,380 KB
testcase_13 AC 266 ms
4,380 KB
testcase_14 AC 219 ms
4,376 KB
testcase_15 AC 40 ms
4,380 KB
testcase_16 AC 43 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 33 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 154 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 180 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const ll mod = 1e9 + 7;

template <typename T>
T power(T x, long long n) {
	T res = 1;
	while (n) {
		if (n & 1) res *= x;
		x *= x;
		n >>= 1;
	}
	return res;
}

template <typename T>
T mod_inv(T a, T md) {
	T b = md, u = 1, v = 0;
	while (b != 0) {
		T t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= md;
	if (u < 0) u += md;
	return u;
}

template <typename T>
vector<pair<T, int>> factorize(T n) {
	vector<pair<T, int>> res;
	for (T 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;
}

template <typename T>
T linear_congruence(const vector<pair<T, T>>& cs, T md) {
	bool ng = false;
	unordered_map<T, pair<int, T>> facts;
	for (auto cc : cs) {
		T x, m;
		tie(x, m) = cc;
		for (auto& pp : factorize(m)) {
			T p; int k;
			tie(p, k) = pp;
			if (!facts.count(p)) {
				facts[p] = make_pair(k, x % power(p, k));
				continue;
			}
			auto q = facts[p];
			if ((x - q.second) % power(p, min(k, q.first)) != 0) {
				ng = true;
				break;;
			}
			if (k > q.first) {
				facts[p] = make_pair(k, x % power(p, k));
			}
		}
	}
	if (ng) return -1;
	bool zero = true;
	vector<pair<T, T>> mr;
	for (auto p : facts) {
		mr.emplace_back(power(p.first, p.second.first), p.second.second);
		zero &= (p.second.second == 0);
	}
	if (zero) {
		T 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<T> coffs(n, 1);
	vector<T> constants(n, 0);
	for (int i = 0; i < n - 1; i++) {
		T 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()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int N;
	cin >> N;
	vector<pair<ll, ll>> cs;
	for (int i = 0; i < N; i++) {
		int X, Y;
		cin >> X >> Y;
		cs.emplace_back(X, Y);
	}
	cout << linear_congruence(cs, mod) << endl;
	return 0;
}
0