結果

問題 No.187 中華風 (Hard)
ユーザー kenkooookenkoooo
提出日時 2015-04-20 09:51:34
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,251 bytes
コンパイル時間 2,079 ms
コンパイル使用メモリ 73,672 KB
実行使用メモリ 60,648 KB
最終ジャッジ日時 2023-09-18 00:18:56
合計ジャッジ時間 8,171 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 200 ms
58,300 KB
testcase_01 AC 201 ms
58,064 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 125 ms
55,460 KB
testcase_18 AC 200 ms
58,176 KB
testcase_19 AC 125 ms
55,952 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 123 ms
55,792 KB
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Scanner;

public class Main {

	private static final int MOD = 1000000007;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] X = new long[N];
		long[] Y = new long[N];
		for (int i = 0; i < N; i++) {
			X[i] = sc.nextInt();
			Y[i] = sc.nextInt();
		}

		for (int i = 1; i < N; i++) {
			long[] cmt = cmt(X[0], Y[0], X[i], Y[i]);
			if (cmt[0] < 0) {
				System.out.println(-1);
				return;
			}

			X[0] = cmt[0];
			Y[0] = cmt[1];
		}

		if (X[0] == 0) {
			X[0] = Y[0];
		}

		System.out.println(X[0] % MOD);
	}

	static long[] extgcd(long a, long b, long[] is) {
		if (a == 0) {
			is[0] = 0;
			is[1] = 1;
			is[2] = b;
		} else {
			extgcd(b % a, a, is);
			long x = is[1] - b / a * is[0];
			is[1] = is[0];
			is[0] = x;
		}
		return is;
	}

	static long[] cmt(long a1, long mo1, long a2, long mo2) {
		long[] exgcd = extgcd(mo1, mo2, new long[3]);
		long g = exgcd[2];
		long x = exgcd[0];

		a1 %= mo1;
		a2 %= mo2;
		if (a1 % exgcd[2] != a2 % exgcd[2]) {
			return new long[] { -1, 0 };
		}

		long lcm = mo1 * (mo2 / g);
		long v = a1 + ((a2 - a1) % lcm) * x % lcm * (mo1 / g);
		return new long[] { ((v % lcm) + lcm) % lcm, lcm };
	}

}
0