結果

問題 No.1691 Badugi
ユーザー 👑 ygussanyygussany
提出日時 2021-09-12 15:50:46
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,177 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 31,140 KB
実行使用メモリ 9,740 KB
最終ジャッジ日時 2023-09-18 20:27:28
合計ジャッジ時間 1,095 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 9 ms
9,740 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 9 ms
9,664 KB
testcase_09 AC 9 ms
9,664 KB
testcase_10 AC 9 ms
9,668 KB
testcase_11 AC 2 ms
6,204 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 7 ms
8,500 KB
testcase_14 AC 9 ms
9,532 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 9 ms
9,732 KB
testcase_18 AC 3 ms
6,524 KB
testcase_19 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353;
long long fact[500001], fact_inv[500001];

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

long long combination(int n, int k)
{
	if (k < 0 || n < k) return 0;
	return fact[n] * fact_inv[k] % Mod * fact_inv[n-k] % Mod;
}


int main()
{
	int i, N, M, K;
	scanf("%d %d %d", &N, &M, &K);
	if (N < M) {
		N ^= M;
		M ^= N;
		N ^= M;
	}
	for (i = 1, fact[0] = 1; i <= N; i++) fact[i] = fact[i-1] * i % Mod;
	for (i = N - 1, fact_inv[N] = div_mod(1, fact[N], Mod); i >= 0; i--) fact_inv[i] = fact_inv[i+1] * (i + 1) % Mod;

	long long ans[5] = {combination(N, K - 2) * combination(M, K - 2) % Mod * fact[K-2] % Mod};
	if (K - 2 >= 4) ans[1] += ans[0] * combination(K - 2, 4) % Mod * 12 % Mod;
	if (K - 2 >= 3) {
		ans[1] += ans[0] * combination(K - 2, 3) % Mod * 12 % Mod;
		if (M >= K - 1) ans[2] += ans[0] * combination(K - 2, 3) % Mod * (M - K + 2) * 6 % Mod;
		if (N >= K - 1) ans[2] += ans[0] * combination(K - 2, 3) % Mod * (N - K + 2) * 6 % Mod;
	}
	if (K - 2 >= 2) {
		ans[2] += ans[0] * combination(K - 2, 2) % Mod;
		if (M >= K - 1) {
			ans[2] += ans[0] * combination(K - 2, 2) % Mod * (M - K + 2) * 2 % Mod;
			ans[3] += ans[0] * combination(K - 2, 2) % Mod * (M - K + 2) * 3 % Mod;
		}
		if (N >= K - 1) {
			ans[2] += ans[0] * combination(K - 2, 2) % Mod * (N - K + 2) * 2 % Mod;
			ans[3] += ans[0] * combination(K - 2, 2) % Mod * (N - K + 2) * 3 % Mod;
		}
		if (M >= K) ans[4] += ans[0] * combination(K - 2, 2) % Mod * combination(M - K + 2, 2) * 2 % Mod;
		if (M >= K - 1 && N >= K - 1) ans[4] += ans[0] * combination(K - 2, 2) % Mod * (M - K + 2) % Mod * (N - K + 2) * 2 % Mod;
		if (N >= K) ans[4] += ans[0] * combination(K - 2, 2) % Mod * combination(N - K + 2, 2) * 2 % Mod;
	}
	if (M >= K) ans[3] += ans[0] * (K - 2) % Mod * combination(M - K + 2, 2) % Mod;
	if (N >= K) ans[3] += ans[0] * (K - 2) % Mod * combination(N - K + 2, 2) % Mod;
	printf("%lld\n", (ans[1] + div_mod(ans[2] % Mod, 2, Mod) + div_mod(ans[3] % Mod, 3, Mod) + div_mod(ans[4] % Mod, 4, Mod)) % Mod);
	fflush(stdout);
	return 0;
}
0