QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#591000#8635. 圆zhenghanyun100 ✓71ms394828kbC++141.4kb2024-09-26 13:33:022024-09-26 13:33:02

Judging History

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

  • [2024-09-26 13:33:02]
  • 评测
  • 测评结果:100
  • 用时:71ms
  • 内存:394828kb
  • [2024-09-26 13:33:02]
  • 提交

answer

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

typedef long long ll;

const int N = 5005;
const int mod = 998244353;

ll n, sum = 1, ans = 1, C[N][N], dp[N][N], t[N];

inline ll qpow(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1) {
			res = res * a % mod;
		}
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}

inline ll inv(ll a) {
	return qpow(a, mod - 2);
}

int main() {
	#ifdef LOCAL
		assert(freopen("T1.in", "r", stdin));
		assert(freopen("T1.out", "w", stdout));
	#endif
	ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	cin >> n;
	if (n == 3) {
		cout << "1\n";
		return 0;
	}
	--n;
	C[0][0] = 1;
	for (int i = 1; i <= n; ++i) {
		C[i][0] = 1;
		for (int j = 1; j <= i; ++j) {
			C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
		}
	}
	dp[0][0] = 1;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			dp[i][j] = dp[i - 1][j - 1];
			if (i > 1) {
				dp[i][j] = (dp[i][j] + dp[i - 2][j - 1]) % mod;
			}
			if (i > 2) {
				dp[i][j] = (dp[i][j] + dp[i - 3][j - 1]) % mod;
			}
		}
	}
	ans = 2;
	for (int i = 1; i <= n; ++i) {
		sum = sum * i % mod;
		t[i] = (dp[n][i] + dp[n - 1][i] + dp[n - 2][i]) % mod;
		t[i] = (C[n][i] - t[i] + mod) % mod;
		t[i] = (t[i] * sum) % mod;
	}
	sum = 1;
	for (int i = 1; i <= n; ++i) {
		sum = sum * (n - i + 1) % mod;
		ans = (ans + t[i] * inv(sum)) % mod;
	}
	cout << ans << "\n";
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

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

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

4

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

6

output:

299473309

result:

ok 1 number(s): "299473309"

Test #4:

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

input:

10

output:

487238321

result:

ok 1 number(s): "487238321"

Test #5:

score: 10
Accepted
time: 2ms
memory: 13856kb

input:

100

output:

41620761

result:

ok 1 number(s): "41620761"

Test #6:

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

input:

200

output:

208771764

result:

ok 1 number(s): "208771764"

Test #7:

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

input:

500

output:

888621375

result:

ok 1 number(s): "888621375"

Test #8:

score: 10
Accepted
time: 60ms
memory: 381564kb

input:

4798

output:

319137015

result:

ok 1 number(s): "319137015"

Test #9:

score: 10
Accepted
time: 71ms
memory: 394828kb

input:

4999

output:

818467659

result:

ok 1 number(s): "818467659"

Test #10:

score: 10
Accepted
time: 64ms
memory: 394776kb

input:

5000

output:

142907477

result:

ok 1 number(s): "142907477"