結果

問題 No.2381 Gift Exchange Party
ユーザー srjywrdnprkt
提出日時 2023-07-20 18:00:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 1,329 bytes
コンパイル時間 2,212 ms
コンパイル使用メモリ 196,876 KB
最終ジャッジ日時 2025-02-15 15:55:47
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll modc = 998244353, MAX=1000000;
vector<ll> f, finv;

ll inv(ll x){
    ll ans = 1, e = modc-2;
    x %= modc;

    while (e > 0){
        if ((e & 1LL)) ans = (ans * x) % modc;
        e = e >> 1LL;
        x = (x*x) % modc;
    }

    return ans;
}

void init(){
    f.resize(MAX+1); finv.resize(MAX+1);
    f[0] = 1;
    for (int i=1; i<=MAX; i++) f[i] = (f[i-1]*i) % modc;
    finv[MAX] = inv(f[MAX]);
    for (int i=MAX-1; i>=0; i--) finv[i] = (finv[i+1] * (i+1)) % modc;
}

ll C(ll n, ll k){ 
    if (n < k || k < 0) return 0;

    return (((f[n] * finv[k]) % modc) * finv[n-k]) % modc;
}

ll P(ll n, ll k){
    if (n < k || k < 0) return 0;

    return (f[n] * finv[n-k]) % modc;
}

ll H(ll n, ll k){
    if (n == 0 && k == 0) return 1;
    return C(n+k-1, k);
}

int main(){

    init();
    ll N, M, K, ans=0, kf, kfp=1, cp=1;
    cin >> N >> K;
    if (K > N){
        cout << (P(N, N) + modc - 1) % modc << endl;
        return 0;
    }
    kf = P(K-1, K-1);
    M = N/K;
    for (int i=0; i<=M; i++){
        ans += (cp * kfp) % modc * inv(P(i, i)) % modc;
        ans %= modc;
        cp *= C(N-K*i, K);
        cp %= modc;
        kfp *= kf;
        kfp %= modc;
    }

    cout << (P(N, N) + modc - ans) % modc << endl;

    return 0;
}
0