結果

問題 No.2381 Gift Exchange Party
ユーザー yuyu_5510yuyu_5510
提出日時 2023-07-14 23:41:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 2,039 bytes
コンパイル時間 1,391 ms
コンパイル使用メモリ 167,832 KB
実行使用メモリ 6,324 KB
最終ジャッジ日時 2023-10-14 14:11:59
合計ジャッジ時間 2,685 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 3 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 4 ms
4,660 KB
testcase_05 AC 5 ms
5,928 KB
testcase_06 AC 3 ms
4,472 KB
testcase_07 AC 3 ms
4,412 KB
testcase_08 AC 4 ms
5,400 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 3 ms
4,880 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 3 ms
4,352 KB
testcase_14 AC 4 ms
4,888 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 4 ms
5,528 KB
testcase_18 AC 3 ms
4,356 KB
testcase_19 AC 4 ms
4,628 KB
testcase_20 AC 29 ms
6,324 KB
testcase_21 AC 5 ms
6,260 KB
testcase_22 AC 31 ms
6,200 KB
testcase_23 AC 30 ms
6,156 KB
testcase_24 AC 3 ms
4,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
const long long mod = 998244353LL;
class mint {
    long long x;
public:
    mint(long long x=0) : x((x%mod+mod)%mod) {}
    mint operator-() const { 
      return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }

    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
};

int main(){
    ll n, p;
    cin >> n >> p;

    vector<mint> fac(n+10);
    vector<mint> finv(n+10);
    fac[0] = mint(1);
    fac[1] = mint(1);
    for(ll i = 2;i <= n;i++){
      fac[i] = fac[i-1]*i;
    }
    finv[n] = mint(1);
    finv[n] /= fac[n];
    for(ll i = n;i > 0;i--){
      finv[i-1] = finv[i]*i;
    }

    if(n >= p){
      mint ans = fac[n];
      mint del = 1;
      ll m = n;
      ans -= 1;
      ll cnt = 1;
      while(m >= p){
        del *= fac[m]*finv[p]*finv[m-p];
        del *= fac[p-1];
        del /= cnt;
        ans -= del;
        m -= p;
        cnt++;
      }

      cout << ans << endl;
    }
    else{
      cout << fac[n]-1 << endl;
    }
}
0