結果

問題 No.442 和と積
ユーザー htensaihtensai
提出日時 2019-12-10 11:33:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 142 ms / 1,000 ms
コード長 1,817 bytes
コンパイル時間 3,545 ms
コンパイル使用メモリ 76,064 KB
実行使用メモリ 56,212 KB
最終ジャッジ日時 2023-09-06 05:11:57
合計ジャッジ時間 7,482 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,760 KB
testcase_01 AC 128 ms
55,808 KB
testcase_02 AC 126 ms
56,072 KB
testcase_03 AC 142 ms
56,160 KB
testcase_04 AC 128 ms
55,772 KB
testcase_05 AC 127 ms
55,900 KB
testcase_06 AC 128 ms
55,796 KB
testcase_07 AC 138 ms
55,832 KB
testcase_08 AC 129 ms
55,496 KB
testcase_09 AC 127 ms
55,864 KB
testcase_10 AC 128 ms
55,964 KB
testcase_11 AC 131 ms
55,936 KB
testcase_12 AC 129 ms
55,716 KB
testcase_13 AC 128 ms
55,784 KB
testcase_14 AC 129 ms
56,060 KB
testcase_15 AC 128 ms
55,972 KB
testcase_16 AC 127 ms
53,708 KB
testcase_17 AC 126 ms
55,832 KB
testcase_18 AC 127 ms
56,212 KB
testcase_19 AC 125 ms
55,508 KB
testcase_20 AC 128 ms
55,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long aa = sc.nextLong();
        long bb = sc.nextLong();
        long c = aa + bb;
        long a = gcd(aa + bb, aa);
        long b = gcd(aa + bb, bb);
        HashSet<Long> set = new HashSet<>();
        for (long i = 2; i <= Math.sqrt(a); i++) {
            while (a % i == 0) {
                set.add(i);
                a /= i;
            }
        }
        if (a != 1) {
            set.add(a);
        }
        for (long i = 2; i <= Math.sqrt(b); i++) {
            while (b % i == 0) {
                set.add(i);
                b /= i;
            }
        }
        if (b != 1) {
            set.add(b);
        }
        HashMap<Long, Integer> mapX = new HashMap<>();
        for (long x : set) {
            int idx = 0;
            while (c % x == 0) {
                idx++;
                c /= x;
            }
            mapX.put(x, idx);
        }
        HashMap<Long, Integer> mapY = new HashMap<>();
        for (long x : set) {
            int idx = 0;
            while (aa % x == 0) {
                idx++;
                aa /= x;
            }
            while (bb % x == 0) {
                idx++;
                bb /= x;
            }
            mapY.put(x, idx);
        }
        long ans = 1;
        for (Map.Entry<Long, Integer> entry : mapX.entrySet()) {
            if (mapY.containsKey(entry.getKey())) {
                ans *= (long)(Math.pow(entry.getKey(), Math.min(entry.getValue(), mapY.get(entry.getKey()))));
            }
        }
        System.out.println(ans);
   }
   
   static long gcd(long x, long y) {
       if (x % y == 0) {
           return y;
       } else {
           return gcd(y, x % y);
       }
   }
}
0