結果

問題 No.2381 Gift Exchange Party
ユーザー otoshigootoshigo
提出日時 2023-07-14 22:05:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 1,366 bytes
コンパイル時間 787 ms
コンパイル使用メモリ 77,740 KB
実行使用メモリ 50,084 KB
最終ジャッジ日時 2023-10-14 12:17:45
合計ジャッジ時間 4,583 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
49,904 KB
testcase_01 AC 104 ms
49,844 KB
testcase_02 AC 104 ms
49,868 KB
testcase_03 AC 108 ms
49,836 KB
testcase_04 AC 110 ms
49,896 KB
testcase_05 AC 108 ms
50,016 KB
testcase_06 AC 103 ms
49,836 KB
testcase_07 AC 107 ms
50,084 KB
testcase_08 AC 106 ms
49,956 KB
testcase_09 AC 99 ms
49,764 KB
testcase_10 AC 100 ms
49,812 KB
testcase_11 AC 100 ms
49,760 KB
testcase_12 AC 108 ms
49,976 KB
testcase_13 AC 111 ms
49,872 KB
testcase_14 AC 102 ms
50,020 KB
testcase_15 AC 103 ms
49,952 KB
testcase_16 AC 103 ms
49,956 KB
testcase_17 AC 100 ms
49,584 KB
testcase_18 AC 107 ms
49,672 KB
testcase_19 AC 104 ms
49,936 KB
testcase_20 AC 129 ms
49,840 KB
testcase_21 AC 104 ms
49,760 KB
testcase_22 AC 131 ms
49,952 KB
testcase_23 AC 134 ms
50,032 KB
testcase_24 AC 103 ms
49,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

const int MAX=2e6+10;
const ll MOD=998244353;
vector<ll>fac(MAX),finv(MAX),inv(MAX);

void set_fac(){
    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;
    }
}

ll cmb(int n,int r){
    if (r<0||n<r)return 0;
    return fac[n]*(finv[r]*finv[n-r]%MOD)%MOD;
}

ll power(ll x,ll r){
    ll re=1,k=x;
    for(int i=0;i<60;i++){
        if(r>>i&1){
            re=(re*k)%MOD;
        }
        k=k*k%MOD;
    }
    return re;
}

ll modinv(ll x){
    return power(x, MOD-2);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    set_fac();
    ll N,P;
    cin>>N>>P;
    ll ans=fac[N]-1;
    ll K=1,L=1;
    for(int i=P;i<=N;i+=P){
        K*=fac[P-1];
        K%=MOD;
        L*=finv[P-1]*modinv(max(1LL,i-P))%MOD;
        L%=MOD;
        ans-=cmb(N,i)*fac[i-1]%MOD*K%MOD*L%MOD;
        ans%=MOD;
        if(ans<0)ans+=MOD;
    }
    cout<<ans<<"\n";
}
0