結果

問題 No.2381 Gift Exchange Party
ユーザー cn_449cn_449
提出日時 2023-07-14 21:42:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 768 bytes
コンパイル時間 4,040 ms
コンパイル使用メモリ 262,072 KB
実行使用メモリ 5,416 KB
最終ジャッジ日時 2023-10-14 11:47:07
合計ジャッジ時間 6,263 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
5,324 KB
testcase_01 AC 43 ms
5,332 KB
testcase_02 AC 44 ms
5,404 KB
testcase_03 AC 43 ms
5,384 KB
testcase_04 AC 46 ms
5,228 KB
testcase_05 AC 45 ms
5,388 KB
testcase_06 AC 46 ms
5,412 KB
testcase_07 AC 48 ms
5,392 KB
testcase_08 AC 49 ms
5,388 KB
testcase_09 AC 48 ms
5,208 KB
testcase_10 AC 45 ms
5,248 KB
testcase_11 AC 45 ms
5,208 KB
testcase_12 AC 43 ms
5,156 KB
testcase_13 AC 44 ms
5,200 KB
testcase_14 AC 46 ms
5,400 KB
testcase_15 AC 46 ms
5,208 KB
testcase_16 AC 45 ms
5,320 KB
testcase_17 AC 48 ms
5,388 KB
testcase_18 AC 45 ms
5,208 KB
testcase_19 AC 47 ms
5,160 KB
testcase_20 AC 68 ms
5,248 KB
testcase_21 AC 47 ms
5,228 KB
testcase_22 AC 72 ms
5,228 KB
testcase_23 AC 70 ms
5,400 KB
testcase_24 AC 45 ms
5,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;

vector<mint> f,finv;
void calc(int n){
	f.resize(n);
	finv.resize(n);
	f[0] = 1;
	finv[0] = 1;
	for(int i = 1; i < n; i++){
		f[i] = f[i - 1] * i;
		finv[i] = finv[i - 1] / i;
	}
}

mint comb(int n, int r){
	return f[n] * finv[r] * finv[n - r];
}

int main(){
	calc(300000);
	ll n, p;
	cin >> n >> p;
	mint ans = 0;
	mint pre = 1;
	for(int i = n; i >= 0; i--){
		if(n % p == i % p){
			mint v = comb(n, i);
			int r = n - i;
			if(r) for(int j = 0; j < p; j++) pre *= r - j;
			mint tmp = pre;
			tmp /= mint(p).pow(r / p);
			tmp *= finv[r / p];
			ans += v * tmp;
		}
	}
	ans = f[n] - ans;
	cout << ans.val() << '\n';
}
0