結果

問題 No.816 Beautiful tuples
ユーザー htensaihtensai
提出日時 2019-12-30 10:36:24
言語 Java19
(openjdk 21)
結果
AC  
実行時間 122 ms / 1,500 ms
コード長 1,011 bytes
コンパイル時間 2,673 ms
コンパイル使用メモリ 71,784 KB
実行使用メモリ 56,508 KB
最終ジャッジ日時 2023-08-07 16:52:29
合計ジャッジ時間 5,548 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,084 KB
testcase_01 AC 119 ms
55,460 KB
testcase_02 AC 118 ms
55,748 KB
testcase_03 AC 118 ms
56,320 KB
testcase_04 AC 118 ms
55,704 KB
testcase_05 AC 119 ms
55,460 KB
testcase_06 AC 119 ms
56,328 KB
testcase_07 AC 120 ms
56,272 KB
testcase_08 AC 119 ms
56,240 KB
testcase_09 AC 119 ms
56,508 KB
testcase_10 AC 119 ms
55,944 KB
testcase_11 AC 120 ms
55,992 KB
testcase_12 AC 120 ms
55,816 KB
testcase_13 AC 120 ms
56,204 KB
testcase_14 AC 122 ms
56,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int sum = a + b;
        int min = Integer.MAX_VALUE;
        for (int i = 1; i <= Math.sqrt(sum); i++) {
            if (sum % i != 0) {
                continue;
            }
            if (i == a || i == b) {
                continue;
            }
            if (canSet(a, b, i)) {
                min = i;
                break;
            }
            if (sum / i == a || sum / i == b) {
                continue;
            }
            if (canSet(a, b, sum / i)) {
                min = Math.min(min, sum / i);
            }
        }
        if (min == Integer.MAX_VALUE) {
            System.out.println(-1); 
        } else {
            System.out.println(min);
        }
    }
    
    static boolean canSet(int a, int b, int c) {
        return (a + c) % b == 0 && (b + c) % a == 0;
    }
}
0