結果
| 問題 |
No.2381 Gift Exchange Party
|
| コンテスト | |
| ユーザー |
yuyu_5510
|
| 提出日時 | 2023-07-14 23:41:23 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 35 ms / 2,000 ms |
| コード長 | 2,039 bytes |
| コンパイル時間 | 1,757 ms |
| コンパイル使用メモリ | 170,008 KB |
| 実行使用メモリ | 6,528 KB |
| 最終ジャッジ日時 | 2024-09-16 08:49:57 |
| 合計ジャッジ時間 | 2,783 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 |
ソースコード
#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;
}
}
yuyu_5510