結果

問題 No.368 LCM of K-products
ユーザー tentententen
提出日時 2022-08-22 18:21:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 2,413 bytes
コンパイル時間 2,537 ms
コンパイル使用メモリ 80,796 KB
実行使用メモリ 44,156 KB
最終ジャッジ日時 2024-04-18 13:27:52
合計ジャッジ時間 7,294 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
40,544 KB
testcase_01 AC 189 ms
42,408 KB
testcase_02 AC 80 ms
38,120 KB
testcase_03 AC 158 ms
41,076 KB
testcase_04 AC 129 ms
40,352 KB
testcase_05 AC 228 ms
39,984 KB
testcase_06 AC 54 ms
37,180 KB
testcase_07 AC 54 ms
36,784 KB
testcase_08 AC 53 ms
37,048 KB
testcase_09 AC 52 ms
37,092 KB
testcase_10 AC 54 ms
36,984 KB
testcase_11 AC 53 ms
37,184 KB
testcase_12 AC 55 ms
36,856 KB
testcase_13 AC 150 ms
40,600 KB
testcase_14 AC 159 ms
40,568 KB
testcase_15 AC 165 ms
42,420 KB
testcase_16 AC 165 ms
41,356 KB
testcase_17 AC 157 ms
40,292 KB
testcase_18 AC 146 ms
41,096 KB
testcase_19 AC 111 ms
39,744 KB
testcase_20 AC 134 ms
40,316 KB
testcase_21 AC 97 ms
38,968 KB
testcase_22 AC 176 ms
44,156 KB
testcase_23 AC 53 ms
37,160 KB
testcase_24 AC 54 ms
37,180 KB
testcase_25 AC 52 ms
36,896 KB
testcase_26 AC 56 ms
36,748 KB
testcase_27 AC 59 ms
37,148 KB
testcase_28 AC 53 ms
37,120 KB
testcase_29 AC 52 ms
37,272 KB
testcase_30 AC 52 ms
37,032 KB
testcase_31 AC 52 ms
37,172 KB
testcase_32 AC 52 ms
36,756 KB
testcase_33 AC 125 ms
40,792 KB
testcase_34 AC 74 ms
37,920 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> collection = counts.get(x);
            Collections.sort(collection);
            int sum = 0;
            for (int i = 0; i < collection.size() && i < k; i++) {
                sum += collection.get(collection.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 {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0