結果

問題 No.816 Beautiful tuples
ユーザー tentententen
提出日時 2020-09-01 07:16:50
言語 Java19
(openjdk 21)
結果
WA  
実行時間 -
コード長 838 bytes
コンパイル時間 2,112 ms
コンパイル使用メモリ 72,264 KB
実行使用メモリ 58,344 KB
最終ジャッジ日時 2023-08-11 02:41:57
合計ジャッジ時間 5,492 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 122 ms
56,140 KB
testcase_02 AC 123 ms
55,832 KB
testcase_03 WA -
testcase_04 AC 123 ms
55,732 KB
testcase_05 AC 125 ms
55,580 KB
testcase_06 AC 124 ms
56,200 KB
testcase_07 AC 125 ms
55,732 KB
testcase_08 AC 124 ms
55,792 KB
testcase_09 AC 123 ms
56,012 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 127 ms
55,828 KB
testcase_13 AC 125 ms
55,908 KB
testcase_14 AC 124 ms
55,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	long a = sc.nextInt();
    	long b = sc.nextInt();
    	long sum = a + b;
    	if ((a + 1) % b == 0 && (b + 1) % a == 0) {
    	    System.out.println(1);
    	    return;
    	}
    	TreeSet<Long> remain = new TreeSet<>();
    	remain.add(sum);
    	for (long i = 2; i <= Math.sqrt(sum); i++) {
    	    if (sum % i == 0) {
    	        if ((a + i) % b == 0 && (b + i) % a == 0) {
    	            System.out.println(i);
    	            return;
    	        }
    	        remain.add(sum / i);
    	    }
    	}
    	for (long x : remain) {
    	    if ((a + x) % b == 0 && (b + x) % a == 0) {
    	        System.out.println(x);
    	        return;
    	    }
    	}
    	System.out.println("-1");
	}
}
0