結果

問題 No.2326 Factorial to the Power of Factorial to the...
ユーザー umezoumezo
提出日時 2023-05-28 14:37:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,068 bytes
コンパイル時間 1,941 ms
コンパイル使用メモリ 198,460 KB
実行使用メモリ 13,044 KB
最終ジャッジ日時 2023-08-27 10:11:46
合計ジャッジ時間 3,110 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
12,824 KB
testcase_01 AC 10 ms
12,824 KB
testcase_02 AC 10 ms
12,788 KB
testcase_03 AC 10 ms
12,772 KB
testcase_04 AC 10 ms
12,804 KB
testcase_05 AC 11 ms
12,784 KB
testcase_06 AC 10 ms
12,764 KB
testcase_07 AC 10 ms
12,804 KB
testcase_08 AC 10 ms
12,756 KB
testcase_09 AC 11 ms
12,752 KB
testcase_10 AC 10 ms
12,756 KB
testcase_11 AC 10 ms
12,748 KB
testcase_12 AC 10 ms
12,816 KB
testcase_13 AC 10 ms
12,844 KB
testcase_14 AC 10 ms
12,788 KB
testcase_15 AC 11 ms
12,768 KB
testcase_16 AC 11 ms
12,804 KB
testcase_17 AC 10 ms
13,044 KB
testcase_18 AC 10 ms
12,820 KB
testcase_19 AC 10 ms
12,808 KB
testcase_20 AC 10 ms
12,776 KB
testcase_21 AC 10 ms
12,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

const int MAX=200200;
const int MOD=1e9+7;
const int MOD1=1e9+6;
ll fac[MAX],finv[MAX],inv[MAX];
ll fac1[MAX],finv1[MAX],inv1[MAX];

void init(){
  fac[0]=fac[1]=1;
  finv[0]=finv[1]=1;
  inv[1]=1;
  for(int i=2;i<MAX;i++){
    fac[i]=fac[i-1]*i%MOD;
    inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
    finv[i]=finv[i-1]*inv[i]%MOD;
  }
}

void init1(){
  fac1[0]=fac1[1]=1;
  finv1[0]=finv1[1]=1;
  inv1[1]=1;
  for(int i=2;i<MAX;i++){
    fac1[i]=fac1[i-1]*i%MOD1;
    inv1[i]=MOD-inv1[MOD1%i]*(MOD/i)%MOD1;
    finv1[i]=finv1[i-1]*inv1[i]%MOD1;
  }
}

ll modpow(ll x,ll n,ll m){
  ll ans=1;
  while(n){
    if(n&1) ans=ans*x%m;
    x=x*x%m;
    n/=2;
  }
  return ans;
}
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  init();
  init1();
  
  ll n,p;
  cin>>n>>p;
  
  ll q=p;
  ll cnt=0;
  while(q<=n){
    cnt+=n/q;
    q*=p;
  }
  
  cout<<cnt*modpow(fac[n],fac1[n],MOD)%MOD<<endl;

  return 0;
}
0