結果

問題 No.816 Beautiful tuples
ユーザー ks2m
提出日時 2019-04-19 21:47:35
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 847 bytes
コンパイル時間 2,773 ms
コンパイル使用メモリ 77,844 KB
実行使用メモリ 54,492 KB
最終ジャッジ日時 2024-09-22 18:21:19
合計ジャッジ時間 4,816 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 11 WA * 4
権限があれば一括ダウンロードができます

ソースコード

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