結果

問題 No.186 中華風 (Easy)
ユーザー startcpp
提出日時 2016-11-08 14:59:21
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 894 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 54,344 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-25 04:51:45
合計ジャッジ時間 1,042 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 8 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

//Easyの解法(条件式を1つずつ追加していく)を高速化. -> Hard, Mod取るのが面倒くさいなあ…
//((a % mod) % c == (b % mod) % c ⇔ a % c == b % cではないので)

#include <iostream>
#define int long long
using namespace std;

int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * b / gcd(a, b); }

signed main() {
	int x[3], y[3];
	int i, j;
	
	for (i = 0; i < 3; i++) cin >> x[i] >> y[i];
	
	int ans = 0;
	int t = 1;
	for (i = 0; i < 3; i++) {
		int diff = t % y[i];
		
		if (diff == 0) {
			if (ans % y[i] != x[i]) {
				cout << -1 << endl;
				return 0;
			}
		}
		else {
			int rem = (x[i] - ans + y[i]) % y[i];
			if (rem % diff != 0) {
				cout << -1 << endl;
				return 0;
			}
			ans += t * rem / diff;
		}
		t = lcm(t, y[i]);
	}
	if (ans == 0) { cout << t << endl; }
	else { cout << ans << endl; }
	return 0;
}
0