結果
| 問題 | No.2326 Factorial to the Power of Factorial to the... |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-03-05 11:52:54 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 1,073 bytes |
| 記録 | |
| コンパイル時間 | 3,162 ms |
| コンパイル使用メモリ | 333,352 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2026-03-05 11:53:09 |
| 合計ジャッジ時間 | 3,966 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int64 MOD = 1000000007LL;
// 繰り返し二乗法
int64 mod_pow(int64 a, int64 e, int64 mod) {
int64 r = 1 % mod;
a %= mod;
while (e > 0) {
if (e & 1) r = (r * a) % mod;
a = (a * a) % mod;
e >>= 1;
}
return r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int64 N, P;
cin >> N >> P;
// 1) v_P(N!) をルジャンドルの公式で求める
int64 vp = 0;
int64 x = N;
while (x > 0) {
x /= P;
vp += x;
}
// 2) N! mod MOD と N! mod (MOD-1) を求める
int64 fact_mod = 1;
int64 fact_mod_phi = 1; // phi(MOD) = MOD-1
int64 PHI = MOD - 1;
for (int64 i = 1; i <= N; i++) {
fact_mod = (fact_mod * i) % MOD;
fact_mod_phi = (fact_mod_phi * i) % PHI;
}
// 3) (N!)^(N!) mod MOD
int64 big = mod_pow(fact_mod, fact_mod_phi, MOD);
// 4) 答え
int64 ans = (vp % MOD) * big % MOD;
cout << ans << '\n';
return 0;
}