結果

問題 No.368 LCM of K-products
ユーザー GrenacheGrenache
提出日時 2016-05-28 13:05:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 581 ms / 2,000 ms
コード長 4,010 bytes
コンパイル時間 3,816 ms
コンパイル使用メモリ 80,588 KB
実行使用メモリ 94,972 KB
最終ジャッジ日時 2023-10-23 21:17:55
合計ジャッジ時間 14,225 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 453 ms
78,472 KB
testcase_01 AC 354 ms
72,332 KB
testcase_02 AC 472 ms
85,664 KB
testcase_03 AC 417 ms
77,008 KB
testcase_04 AC 558 ms
77,748 KB
testcase_05 AC 581 ms
94,972 KB
testcase_06 AC 86 ms
54,512 KB
testcase_07 AC 89 ms
54,540 KB
testcase_08 AC 77 ms
54,520 KB
testcase_09 AC 75 ms
54,512 KB
testcase_10 AC 76 ms
54,504 KB
testcase_11 AC 76 ms
54,508 KB
testcase_12 AC 91 ms
54,512 KB
testcase_13 AC 487 ms
73,088 KB
testcase_14 AC 446 ms
79,100 KB
testcase_15 AC 495 ms
83,612 KB
testcase_16 AC 450 ms
76,948 KB
testcase_17 AC 432 ms
76,892 KB
testcase_18 AC 476 ms
82,600 KB
testcase_19 AC 305 ms
68,200 KB
testcase_20 AC 396 ms
75,512 KB
testcase_21 AC 232 ms
65,496 KB
testcase_22 AC 562 ms
83,292 KB
testcase_23 AC 76 ms
54,504 KB
testcase_24 AC 75 ms
53,964 KB
testcase_25 AC 76 ms
54,504 KB
testcase_26 AC 77 ms
54,508 KB
testcase_27 AC 76 ms
54,500 KB
testcase_28 AC 77 ms
54,516 KB
testcase_29 AC 75 ms
54,504 KB
testcase_30 AC 75 ms
54,524 KB
testcase_31 AC 76 ms
54,516 KB
testcase_32 AC 75 ms
54,512 KB
testcase_33 AC 313 ms
70,908 KB
testcase_34 AC 107 ms
55,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder368 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

		Prime p = new Prime(100_000);
		List<Integer> primes = p.getPrimeList(100_000);

        int n = sc.nextInt();
        int k = sc.nextInt();

        Map<Integer, List<Integer>> hm = new HashMap<>();
        for (int i = 0; i < n; i++) {
        	int a = sc.nextInt();

        	int tmp = a;
        	for (int j : primes) {
        		if (j * j > a) {
        			break;
        		}

        		int cnt = 0;
        		while (tmp % j == 0) {
        			cnt++;
        			tmp /= j;
        		}

        		if (hm.containsKey(j)) {
        			hm.get(j).add(cnt);
        		} else {
        			List<Integer> ltmp = new ArrayList<>();
        			ltmp.add(cnt);
        			hm.put(j, ltmp);
        		}
        	}

        	if (tmp != 1) {
        		if (hm.containsKey(tmp)) {
        			hm.get(tmp).add(1);
        		} else {
        			List<Integer> ltmp = new ArrayList<>();
        			ltmp.add(1);
        			hm.put(tmp, ltmp);
        		}
        	}
        }

        final long MOD = 1_000_000_007;

        long ret = 1;
        for (Entry<Integer, List<Integer>> e : hm.entrySet()) {
        	List<Integer> bp = e.getValue();
        	Collections.sort(bp);
        	int pp = 0;
        	for (int i = bp.size() - 1; bp.size() - i <= k && i >= 0; i--) {
        		pp += bp.get(i);
        	}

        	ret = ret * pow(e.getKey(), pp, MOD) % MOD;
        }

        pr.println(ret);

        pr.close();
        sc.close();
    }

	private static long pow(int a, int n, long MOD) {
		int loop = n;
		long ret = 1;
		long x = a;
		while (loop > 0) {
			if (loop % 2 == 1) {
				ret = ret * x % MOD;
			}
			x = x * x % MOD;
			loop /= 2;
		}

		return ret;
	}

	@SuppressWarnings("unused")
	private static class Prime {
		private int n;
		private List<Integer> primes;
		private BitSet p;

		Prime(int n) {
			this.n = n;

			p = new BitSet(n / 2);
			int m = (int)Math.sqrt(n);

			for (int i = 1; i <= m; i++) {
				if (p.get(i - 1)) {
					continue;
				}

				for (int j = 2 * i * (i + 1); 2 * j + 1 <= n; j += 2 * i + 1) {
					p.set(j - 1);
				}
			}
		}

		boolean isPrime(int n) {
			if (n == 2) {
				return true;
			}

			if (n == 1 || (n & 0x1) == 0) {
				return false;
			}

			return !p.get(n / 2 - 1);
		}

		List<Integer> getPrimeList(int n) {
			if (primes != null) {
				return primes;
			}

			primes = new ArrayList<>();
			primes.add(2);
			for (int i = 1; 2 * i + 1 <= n; i++) {
				if (!p.get(i - 1)) {
					primes.add(2 * i + 1);
				}
			}

			return primes;
		}

		private static boolean isPrime(long n) {
			if (n == 2) {
				return true;
			}

			if (n == 1 || (n & 0x1) == 0) {
				return false;
			}

			for (long i = 3; i * i <= n; i += 2) {
				if (n % i == 0) {
					return false;
				}
			}

			return true;
		}
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0