結果

問題 No.816 Beautiful tuples
ユーザー ks2mks2m
提出日時 2019-04-19 21:52:53
言語 Java17
(openjdk 17.0.1)
結果
AC  
実行時間 95 ms / 1,500 ms
コード長 849 bytes
コンパイル時間 1,711 ms
使用メモリ 38,080 KB
最終ジャッジ日時 2022-11-22 09:01:33
合計ジャッジ時間 4,226 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 88 ms
37,684 KB
testcase_01 AC 89 ms
37,932 KB
testcase_02 AC 92 ms
37,832 KB
testcase_03 AC 91 ms
37,644 KB
testcase_04 AC 95 ms
37,692 KB
testcase_05 AC 86 ms
37,660 KB
testcase_06 AC 90 ms
37,752 KB
testcase_07 AC 87 ms
37,700 KB
testcase_08 AC 90 ms
37,904 KB
testcase_09 AC 91 ms
37,668 KB
testcase_10 AC 89 ms
37,948 KB
testcase_11 AC 88 ms
37,784 KB
testcase_12 AC 91 ms
37,684 KB
testcase_13 AC 88 ms
38,080 KB
testcase_14 AC 92 ms
37,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		sc.close();

		int max = (int) Math.sqrt(a + b);
		TreeSet<Integer> yakusuu = new TreeSet<Integer>();
		for (int i = 1; i <= max; i++) {
			if ((a + b) % i == 0) {
				yakusuu.add(i);
			}
			if (a == i || b == i) {
				continue;
			}
			if ((a + b) % i == 0 && (a + i) % b == 0 && (i + b) % a == 0) {
				System.out.println(i);
				return;
			}
		}
		while (!yakusuu.isEmpty()) {
			int c = yakusuu.pollLast();
			int i = (a + b) / c;
			if (a == i || b == i) {
				continue;
			}
			if ((a + b) % i == 0 && (a + i) % b == 0 && (i + b) % a == 0) {
				System.out.println(i);
				return;
			}
		}
		System.out.println(-1);
	}
}
0