結果

問題 No.368 LCM of K-products
ユーザー tentententen
提出日時 2023-06-13 16:48:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 2,954 bytes
コンパイル時間 5,647 ms
コンパイル使用メモリ 88,732 KB
実行使用メモリ 54,552 KB
最終ジャッジ日時 2023-09-04 02:10:07
合計ジャッジ時間 7,872 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
54,552 KB
testcase_01 AC 176 ms
54,112 KB
testcase_02 AC 71 ms
51,924 KB
testcase_03 AC 119 ms
53,348 KB
testcase_04 AC 110 ms
54,400 KB
testcase_05 AC 243 ms
51,324 KB
testcase_06 AC 43 ms
49,736 KB
testcase_07 AC 44 ms
49,648 KB
testcase_08 AC 44 ms
49,664 KB
testcase_09 AC 42 ms
47,912 KB
testcase_10 AC 42 ms
50,100 KB
testcase_11 AC 43 ms
49,912 KB
testcase_12 AC 44 ms
49,748 KB
testcase_13 AC 135 ms
54,488 KB
testcase_14 AC 146 ms
54,236 KB
testcase_15 AC 150 ms
54,216 KB
testcase_16 AC 144 ms
54,492 KB
testcase_17 AC 131 ms
54,516 KB
testcase_18 AC 132 ms
53,544 KB
testcase_19 AC 97 ms
53,648 KB
testcase_20 AC 118 ms
53,952 KB
testcase_21 AC 86 ms
53,440 KB
testcase_22 AC 131 ms
54,132 KB
testcase_23 AC 43 ms
49,752 KB
testcase_24 AC 43 ms
49,804 KB
testcase_25 AC 43 ms
50,004 KB
testcase_26 AC 43 ms
50,116 KB
testcase_27 AC 43 ms
49,768 KB
testcase_28 AC 42 ms
49,964 KB
testcase_29 AC 42 ms
49,920 KB
testcase_30 AC 42 ms
49,708 KB
testcase_31 AC 43 ms
50,104 KB
testcase_32 AC 43 ms
49,728 KB
testcase_33 AC 110 ms
54,200 KB
testcase_34 AC 61 ms
51,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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<>();
        while (n-- > 0) {
            int x = sc.nextInt();
            for (int i = 2; i <= Math.sqrt(x); i++) {
                if (x % i == 0) {
                    int count = 0;
                    while (x % i == 0) {
                        count++;
                        x /= i;
                    }
                    if (!counts.containsKey(i)) {
                        counts.put(i, new ArrayList<>());
                    }
                    counts.get(i).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() - 1 - i);
            }
            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 Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0