結果
| 問題 | No.442 和と積 | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2022-08-02 09:09:11 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 63 ms / 1,000 ms | 
| コード長 | 2,131 bytes | 
| コンパイル時間 | 2,401 ms | 
| コンパイル使用メモリ | 78,352 KB | 
| 実行使用メモリ | 37,192 KB | 
| 最終ジャッジ日時 | 2024-07-23 01:59:19 | 
| 合計ジャッジ時間 | 4,517 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 18 | 
ソースコード
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();
    }
}
            
            
            
        