結果
| 問題 | No.442 和と積 | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2022-08-01 19:49:48 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 2,139 bytes | 
| コンパイル時間 | 2,943 ms | 
| コンパイル使用メモリ | 79,344 KB | 
| 実行使用メモリ | 60,148 KB | 
| 最終ジャッジ日時 | 2024-07-22 10:39:01 | 
| 合計ジャッジ時間 | 6,418 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 14 WA * 3 TLE * 1 | 
ソースコード
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;
        HashMap<Long, Integer> base = new HashMap<>();
        for (long i = 2; i <= Math.sqrt(c); i++) {
            while (c % i == 0) {
                base.put(i, base.getOrDefault(i, 0) + 1);
                c /= i;
            }
        }
        if (c > 1) {
            base.put(c, base.getOrDefault(c, 0) + 1);
        }
        HashMap<Long, Integer> next = new HashMap<>();
        for (long i = 2; i <= Math.sqrt(a); i++) {
            while (a % i == 0) {
                next.put(i, next.getOrDefault(i, 0) + 1);
                a /= i;
            }
        }
        if (a > 1) {
            next.put(a, next.getOrDefault(c, 0) + 1);
        }
        for (long i = 2; i <= Math.sqrt(b); i++) {
            while (b % i == 0) {
                next.put(i, next.getOrDefault(i, 0) + 1);
                b /= i;
            }
        }
        if (b > 1) {
            next.put(b, next.getOrDefault(c, 0) + 1);
        }
        long ans = 1;
        for (long x : base.keySet()) {
            int times = Math.min(base.get(x), next.getOrDefault(x, 0));
            for (int i = 0; i < times; i++) {
                ans *= x;
            }
        }
        System.out.println(ans);
    }
}
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();
    }
}
            
            
            
        