結果

問題 No.442 和と積
ユーザー tentententen
提出日時 2022-08-02 09:09:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 53 ms / 1,000 ms
コード長 2,131 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 75,856 KB
実行使用メモリ 50,652 KB
最終ジャッジ日時 2023-09-30 07:45:11
合計ジャッジ時間 4,427 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,388 KB
testcase_01 AC 42 ms
49,432 KB
testcase_02 AC 43 ms
49,448 KB
testcase_03 AC 53 ms
50,652 KB
testcase_04 AC 43 ms
49,464 KB
testcase_05 AC 44 ms
49,472 KB
testcase_06 AC 43 ms
49,388 KB
testcase_07 AC 52 ms
50,332 KB
testcase_08 AC 43 ms
49,352 KB
testcase_09 AC 42 ms
49,336 KB
testcase_10 AC 45 ms
49,392 KB
testcase_11 AC 43 ms
49,464 KB
testcase_12 AC 43 ms
49,464 KB
testcase_13 AC 43 ms
49,336 KB
testcase_14 AC 44 ms
49,496 KB
testcase_15 AC 44 ms
49,404 KB
testcase_16 AC 44 ms
49,840 KB
testcase_17 AC 43 ms
49,596 KB
testcase_18 AC 43 ms
49,388 KB
testcase_19 AC 45 ms
49,512 KB
testcase_20 AC 44 ms
49,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = a + b;
        long gcdA = getGCD(a, c);
        long gcdB = getGCD(b, c);
        HashSet<Long> each = new HashSet<>();
        for (long i = 2; i <= Math.sqrt(gcdA); i++) {
            while (gcdA % i == 0) {
                each.add(i);
                gcdA /= i;
            }
        }
        if (gcdA > 1) {
            each.add(gcdA);
        }
        for (long i = 2; i <= Math.sqrt(gcdB); i++) {
            while (gcdB % i == 0) {
                each.add(i);
                gcdB /= i;
            }
        }
        if (gcdB > 1) {
            each.add(gcdB);
        }
        long ans = 1;
        for (long x : each) {
            int times = Math.min(getCount(c, x), getCount(a, x) + getCount(b, x));
            for (int i = 0; i < times; i++) {
                ans *= x;
            }
        }
        System.out.println(ans);
    }
    
    static int getCount(long base, long x) {
        int count = 0;
        while (base % x == 0) {
            count++;
            base /= x;
        }
        return count;
    }
    
    static long getGCD(long x, long y) {
        if (y == 0) {
            return x;
        } else {
            return getGCD(y, x % y);
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0