QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#591530#8635. 圆xiay100 ✓46ms199400kbC++141.3kb2024-09-26 16:19:062024-09-26 16:19:06

Judging History

你现在查看的是最新测评结果

  • [2024-09-26 16:19:06]
  • 评测
  • 测评结果:100
  • 用时:46ms
  • 内存:199400kb
  • [2024-09-26 16:19:06]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 5000 + 5, P = 998244353;
int dp[N][N];
int n;
long long fac[N];

long long qpow(long long a, int b) {
	long long ans = 1;
	while (b) {
		if (b & 1) {
			ans = ans * a % P;
		}
		a = a * a % P;
		b >>= 1;
	}
	return ans;
}

void init() {
	fac[0] = 1;
	for (int i = 1; i <= n; i++) {
		fac[i] = fac[i - 1] * i % P;
	}
}

long long C(int n, int m) {
	return fac[n] * qpow(fac[n - m], P - 2) % P * qpow(fac[m], P - 2) % P;
}

signed main() {
	#ifndef ONLINE_JUDGE
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
	#endif

	scanf("%lld", &n);
	if (n == 3) {
		puts("1");
		return 0;
	}
	init();
	dp[0][0] = 1;
	dp[1][1] = 1;
	for (int i = 2; i < n; i++) {
		for (int j = 1; j <= n; j++) {
			dp[i][j] = dp[i - 1][j - 1];
			if (i == 2) {
				(dp[i][j] += dp[i - 2][j - 1]) %= P;
			}
			else {
				(dp[i][j] += (dp[i - 2][j - 1] + dp[i - 3][j - 1]) % P) %= P;
			}
		}
	}
	int ans = 2;
	for (int i = 1; i < n; i++) {
		ans = (ans + 1ll * (C(n - 1, i) - (dp[n - 1][i] + dp[n - 2][i] + dp[n - 3][i]) % P + P) % P * fac[i] % P * fac[n - i - 1] % P * qpow(fac[n - 1], P - 2) % P) % P;
	}
	printf("%lld\n", ans);

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 0ms
memory: 3784kb

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 10
Accepted
time: 0ms
memory: 3808kb

input:

4

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 10
Accepted
time: 1ms
memory: 5852kb

input:

6

output:

299473309

result:

ok 1 number(s): "299473309"

Test #4:

score: 10
Accepted
time: 0ms
memory: 3948kb

input:

10

output:

487238321

result:

ok 1 number(s): "487238321"

Test #5:

score: 10
Accepted
time: 1ms
memory: 8064kb

input:

100

output:

41620761

result:

ok 1 number(s): "41620761"

Test #6:

score: 10
Accepted
time: 1ms
memory: 10344kb

input:

200

output:

208771764

result:

ok 1 number(s): "208771764"

Test #7:

score: 10
Accepted
time: 0ms
memory: 24456kb

input:

500

output:

888621375

result:

ok 1 number(s): "888621375"

Test #8:

score: 10
Accepted
time: 45ms
memory: 192416kb

input:

4798

output:

319137015

result:

ok 1 number(s): "319137015"

Test #9:

score: 10
Accepted
time: 46ms
memory: 199344kb

input:

4999

output:

818467659

result:

ok 1 number(s): "818467659"

Test #10:

score: 10
Accepted
time: 45ms
memory: 199400kb

input:

5000

output:

142907477

result:

ok 1 number(s): "142907477"