結果

問題 No.2326 Factorial to the Power of Factorial to the...
ユーザー MMMM
提出日時 2023-05-28 14:20:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,208 bytes
コンパイル時間 1,534 ms
コンパイル使用メモリ 166,824 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-27 09:32:19
合計ジャッジ時間 2,490 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define chmin(x,y) (x) = min((x),(y))
#define chmax(x,y) (x) = max((x),(y))
using namespace std;
using ll = long long;
const ll mod = 1000000007;
const vector<int> dx = {1,0,-1,0}, dy = {0,1,0,-1};
using Graph = vector<vector<int>>;

// https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

int main(){
  // input
  ll N,P; cin >> N >> P;
  // solve
  ll fac = 1, fer = 0;
  for (ll i = 1; i <= N; i++){
    // update fac/fer
    fer *= i;
    fer += (fac * i) / mod;
    fac *= i;
    
    // take remainder of fac/fer
    fac %= mod;
    fer = (fer / mod) + fer % mod;
  }
  
  ll pwr = modpow(fac,fac+fer,mod);
  
  ll leg = 0; // N!がPで割り切れる回数を調べる
  while(N > 0){
    leg += N / P;
    N /= P;
  }
  
  // output
  // cout << fac << " " << pwr << " " << leg << endl;
  cout << (leg * pwr) % mod << endl;
}
0