結果

問題 No.816 Beautiful tuples
ユーザー ks2m
提出日時 2019-04-19 21:52:53
言語 Java
(openjdk 23)
結果
AC  
実行時間 131 ms / 1,500 ms
コード長 849 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 78,096 KB
実行使用メモリ 41,612 KB
最終ジャッジ日時 2024-09-22 18:41:06
合計ジャッジ時間 5,282 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

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