結果

問題 No.2381 Gift Exchange Party
ユーザー Today03Today03
提出日時 2023-07-14 22:44:03
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 951 bytes
コンパイル時間 2,113 ms
コンパイル使用メモリ 206,488 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-16 07:47:52
合計ジャッジ時間 3,153 ms
ジャッジサーバーID
(参考情報)
judge6 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/modint>
using mint=atcoder::modint998244353;

template <typename mint>
struct combination {
  vector<mint> fact,factinv;

  combination(int n) {
    fact.resize(n+1);
    factinv.resize(n+1);
    fact[0]=1;
    for (int i=1;i<=n;i++) {
      fact[i]=fact[i-1]*i;
    }
    for (int i=0;i<=n;i++) {
      factinv[i]=fact[i].inv();
    }
  }

  mint nCr(long long n,long long r) {
    if (n<0||r<0||n-r<0) {
      return 0;
    }
    return fact[n]*factinv[r]*factinv[n-r];
  }
  
  mint nPr(long long n,long long r) {
    if (n<0||r<0||n-r<0) {
      return 0;
    }
    return fact[n]*factinv[n-r];
  }
};

int main() {
  int n;
  long long p;
  cin>>n>>p;
  combination<mint> C(n);

  mint ans=C.fact[n]-1;
  mint pre=1,tmp=1;
  for (int i=1;p*i<=n;i++) {
    pre*=C.nCr(n-p*(i-1),p);
    tmp*=i;
    mint fact=(C.fact[p-1]).pow(i);
    ans-=pre*fact/tmp;
  }
  cout<<ans.val()<<endl;
}
0