結果

問題 No.368 LCM of K-products
ユーザー tentententen
提出日時 2021-12-03 13:54:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 2,379 bytes
コンパイル時間 2,501 ms
コンパイル使用メモリ 84,272 KB
実行使用メモリ 55,536 KB
最終ジャッジ日時 2023-09-19 05:49:10
合計ジャッジ時間 8,054 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
53,820 KB
testcase_01 AC 185 ms
53,828 KB
testcase_02 AC 62 ms
51,032 KB
testcase_03 AC 126 ms
53,148 KB
testcase_04 AC 121 ms
54,684 KB
testcase_05 AC 230 ms
53,416 KB
testcase_06 AC 46 ms
49,636 KB
testcase_07 AC 47 ms
49,804 KB
testcase_08 AC 46 ms
49,712 KB
testcase_09 AC 46 ms
49,852 KB
testcase_10 AC 45 ms
49,732 KB
testcase_11 AC 47 ms
49,684 KB
testcase_12 AC 45 ms
49,604 KB
testcase_13 AC 138 ms
54,488 KB
testcase_14 AC 132 ms
54,208 KB
testcase_15 AC 134 ms
54,044 KB
testcase_16 AC 149 ms
54,352 KB
testcase_17 AC 138 ms
54,764 KB
testcase_18 AC 135 ms
53,704 KB
testcase_19 AC 105 ms
53,564 KB
testcase_20 AC 123 ms
54,796 KB
testcase_21 AC 81 ms
53,004 KB
testcase_22 AC 179 ms
55,536 KB
testcase_23 AC 47 ms
49,616 KB
testcase_24 AC 45 ms
49,740 KB
testcase_25 AC 46 ms
49,716 KB
testcase_26 AC 46 ms
49,664 KB
testcase_27 AC 45 ms
49,640 KB
testcase_28 AC 45 ms
49,832 KB
testcase_29 AC 45 ms
50,128 KB
testcase_30 AC 44 ms
49,724 KB
testcase_31 AC 45 ms
49,796 KB
testcase_32 AC 44 ms
49,760 KB
testcase_33 AC 110 ms
54,260 KB
testcase_34 AC 63 ms
50,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static final int MOD = 1000000007;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        HashMap<Integer, ArrayList<Integer>> counts = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            for (int j = 2; j <= Math.sqrt(x); j++) {
                int count = 0;
                while (x % j == 0) {
                    count++;
                    x /= j;
                }
                if (count > 0) {
                    if (!counts.containsKey(j)) {
                        counts.put(j, new ArrayList<>());
                    }
                    counts.get(j).add(count);
                }
            }
            if (x > 1) {
                if (!counts.containsKey(x)) {
                    counts.put(x, new ArrayList<>());
                }
                counts.get(x).add(1);
            }
        }
        long ans = 1;
        for (int x : counts.keySet()) {
            ArrayList<Integer> list = counts.get(x);
            Collections.sort(list);
            int sum = 0;
            for (int i = 0; i < k && i < list.size(); i++) {
                sum += list.get(list.size() - i - 1);
            }
            ans *= pow(x, sum);
            ans %= MOD;
        }
        System.out.println(ans);
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return pow(x, p - 1) * x % MOD;
        }
    }
}
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 {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0