結果
| 問題 | No.2326 Factorial to the Power of Factorial to the... |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-02-02 23:55:42 |
| 言語 | D (dmd 2.111.0) |
| 結果 |
AC
|
| 実行時間 | 38 ms / 2,000 ms |
| コード長 | 1,497 bytes |
| 記録 | |
| コンパイル時間 | 1,754 ms |
| コンパイル使用メモリ | 172,072 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2026-02-02 23:55:47 |
| 合計ジャッジ時間 | 3,975 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
import std;
void main () {
const long MOD = 10 ^^ 9 + 7;
int N, P;
readln.read(N, P);
// N!はPで何回割り切れる? -> P^xを因数に持つものがx回カウントされるように数える簡単な方法がある。(ルシャンドルの定理)
int divCount = 0;
long div = P;
while (div <= N) {
divCount += N / div;
div *= P;
}
// 指数が増える = その分だけ素因数が倍になる
// なので、これをN!^N!倍したらよい。
// N! mod 1e9+7を肩に乗せると壊れるので、肩は((N!)^N)^(N-1)...とほぐすとNlog(1e9+7)時間
long fac = 1;
foreach (i; 1 .. N + 1) {
fac *= i;
fac %= MOD;
}
foreach (i; 1 .. N + 1) {
fac = mod_pow(fac, i, MOD);
}
writeln(divCount * fac % MOD);
}
void read (T...) (string S, ref T args) {
import std.conv : to;
import std.array : split;
auto buf = S.split;
foreach (i, ref arg; args) {
arg = buf[i].to!(typeof(arg));
}
}
long mod_pow (long a, long x, const long MOD)
in {
assert(0 <= x, "x must satisfy 0 <= x");
assert(1 <= MOD, "MOD must satisfy 1 <= MOD");
assert(MOD <= int.max, "MOD must satisfy MOD*MOD <= long.max");
}
do {
// normalize
a %= MOD; a += MOD; a %= MOD;
long res = 1L;
long base = a;
while (0 < x) {
if (0 < (x&1)) (res *= base) %= MOD;
(base *= base) %= MOD;
x >>= 1;
}
return res % MOD;
}