結果

問題 No.186 中華風 (Easy)
ユーザー masamasa
提出日時 2015-04-20 00:20:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,390 bytes
コンパイル時間 633 ms
コンパイル使用メモリ 62,012 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 00:43:39
合計ジャッジ時間 1,705 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

typedef pair<long long, long long> PLL;

template <class T>
T gcd(T a, T b) {
	return b == 0 ? a : gcd(b, a % b);
}

template <class T>
T lcm(T a, T b) {
	return a / gcd(a, b) * b;
}

int main() {
	long long x1, y1, x2, y2, x3, y3;
	cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

	PLL big, small;
	if (y1 > y2) {
		big   = make_pair(x1, y1);
		small = make_pair(x2, y2);
	} else {
		big   = make_pair(x2, y2);
		small = make_pair(x1, y1);
	}

	long long y12 = lcm(y1, y2);
	long long x12 = -1;

	if (big.first == 0 && small.first == 0) {
		x12 = y12;
	} else {
		for (long long i = big.first; i <= y12; i += big.second) {
			if (i % small.second == small.first) {
				x12 = i;
				break;
			}
		}

		if (x12 == -1) {
			cout << -1 << endl;
			return 0;
		}
	}

	if (y12 > y3) {
		big   = make_pair(x12, y12);
		small = make_pair(x3,  y3);
	} else {
		big   = make_pair(x3,  y3);
		small = make_pair(x12, y12);
	}

	long long y123 = lcm(y12, y3);
	long long x123 = -1;

	if (big.first == 0 && small.first == 0) {
		x123 = y123;
	} else {
		for (long long i = big.first; i <= y123; i += big.second) {
			if (i % small.second == small.first) {
				x123 = i;
				break;
			}
		}

		if (x123 == -1) {
			cout << -1 << endl;
			return 0;
		}
	}

	cout << x123 << endl;
	return 0;
}
0