結果

問題 No.186 中華風 (Easy)
ユーザー 東前頭十一枚目東前頭十一枚目
提出日時 2019-09-26 13:50:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,040 bytes
コンパイル時間 1,595 ms
コンパイル使用メモリ 168,820 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 00:56:46
合計ジャッジ時間 2,668 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

template<typename T>
struct ChineseRemainderTheorem {
	inline T modulo(T x, T mod) {
		if(0 <= x and x < mod) return x;
		x %= mod;
		if(x < 0) x += mod;
		return x;
	}
	// gcd(a, b)
	// ap + bq = gcd(a, b)を満たす(p, q)
	T exGCD(T a, T b, T &p, T &q) {
		if(b == 0) {
			// gcd(a, b)p + 0q = gcd(a, b)
			p = 1, q = 0;
			return a;
		}
		T ret = exGCD(b, a % b, q, p);
		q -= a / b * p;
		return ret;
	}
	// x ≡ b(mod m)を満たすx(mod M (= lcm m))
	T get(vector<T> &b, vector<T> &mod) {
		// x ≡ 0(mod 1)は全ての整数
		T x = 0, MOD = 1;
		for(int i = 0; i < (int)b.size(); ++i) {
			T p, q;
			T d = exGCD(MOD, mod[i], p, q);
			// 必要十分性の確認
			if((x - b[i]) % d != 0) throw "NOT FOUND";
			// nextMOD = MOD * (mod[i] / d)なので部分的にmodが取れてオーバーフロー回避
			x = x - modulo((x - b[i]) / d * p, (mod[i] / d)) * MOD;
			// lcm(M, m) = M * m / gcd(M, m)
			MOD = MOD * (mod[i] / d);
			x = modulo(x, MOD);
		}
		return modulo(x, MOD);
	}
	T Garner(vector<T> &b, vector<T> &mod, T MOD = numeric_limits<T>::max()) {
		// normalisation(b, mod);
		vector<T> p(b.size());
		for(int i = 0; i < (int)b.size(); ++i) {
			p[i] = b[i] % mod[i];
			for(int j = 0; j < i; ++j) {
				T inv, _tmp;
				exGCD(mod[j], mod[i], inv, _tmp);
				inv = modulo(inv, mod[i]);
				p[i] = (p[i] - p[j]) * inv;
				p[i] = modulo(p[i], mod[i]);
			}
		}
		// 復元
		T ret = 0;
		for(int i = 0; i < (int)b.size(); ++i) {
			T tmp = modulo(p[i], MOD);
			for(int j = 0; j < i; ++j) {
				tmp *= mod[j];
				tmp %= MOD;
			}
			ret += tmp;
			ret %= MOD;
		}
		return ret;
	}
};

int main() {
	const int n = 3;
	vector<int64_t> x(n), y(n);
	for(int i = 0; i < n; ++i) {
		cin >> x[i] >> y[i];
	}
	ChineseRemainderTheorem<int64_t> crt;
	try {
		int64_t ans = crt.get(x, y);
		if(ans == 0) {
			ans = 1;
			for(int i = 0; i < n; ++i) {
				ans = ans * y[i] / __gcd(ans, y[i]);
			}
		}
		cout << ans << '\n';
	}
	catch(...) {
		cout << -1 << '\n';
	}
	return 0;
}
0