結果

問題 No.2381 Gift Exchange Party
ユーザー yuyu_5510yuyu_5510
提出日時 2023-07-14 22:27:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,980 bytes
コンパイル時間 1,504 ms
コンパイル使用メモリ 167,768 KB
実行使用メモリ 6,392 KB
最終ジャッジ日時 2023-10-14 12:42:30
合計ジャッジ時間 2,925 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 WA -
testcase_03 AC 4 ms
4,352 KB
testcase_04 AC 10 ms
4,604 KB
testcase_05 AC 18 ms
5,856 KB
testcase_06 AC 8 ms
4,400 KB
testcase_07 AC 9 ms
4,352 KB
testcase_08 AC 16 ms
5,724 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 7 ms
4,348 KB
testcase_11 AC 11 ms
4,808 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 18 ms
6,116 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 9 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
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;
      while(m >= p){
        del *= fac[m]*finv[p]*finv[m-p];
        del *= fac[p-1];
        ans -= del;
        m -= p;
      }

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