結果

問題 No.816 Beautiful tuples
ユーザー htensaihtensai
提出日時 2019-12-30 10:36:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 120 ms / 1,500 ms
コード長 1,011 bytes
コンパイル時間 3,129 ms
コンパイル使用メモリ 74,676 KB
実行使用メモリ 41,456 KB
最終ジャッジ日時 2024-04-25 10:31:21
合計ジャッジ時間 4,478 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
41,188 KB
testcase_01 AC 112 ms
41,456 KB
testcase_02 AC 120 ms
41,188 KB
testcase_03 AC 106 ms
40,112 KB
testcase_04 AC 117 ms
41,124 KB
testcase_05 AC 119 ms
40,104 KB
testcase_06 AC 112 ms
40,916 KB
testcase_07 AC 115 ms
41,068 KB
testcase_08 AC 101 ms
39,840 KB
testcase_09 AC 104 ms
39,912 KB
testcase_10 AC 115 ms
39,924 KB
testcase_11 AC 116 ms
41,232 KB
testcase_12 AC 112 ms
40,736 KB
testcase_13 AC 119 ms
40,224 KB
testcase_14 AC 114 ms
40,616 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