Submission #796975

# Submission time Handle Problem Language Result Execution time Memory
796975 2023-07-29T02:15:40 Z GusterGoose27 Festivals in JOI Kingdom 2 (JOI23_festival2) C++17
87 / 100
9000 ms 1416 KB
#pragma GCC optimize("Ofast,unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define int long long

typedef long long ll;

int n, MOD;

const int MAXN = 20005;

int dp[MAXN][2];
int fac[3*MAXN];
int facinv[3*MAXN];

int expo(ll a, int b) {
	ll cur = 1;
	while (b) {
		if (b & 1) {
			cur = (cur*a)%MOD;
		}
		a = (a*a)%MOD;
		b /= 2;
	}
	return cur;
}

int inv(int a) {
	return expo(a, MOD-2);
}

ll choose(int a, int b) {
	ll cur = fac[a]*facinv[b];
	cur %= MOD;
	return (cur*facinv[a-b])%MOD;
}

signed main() {
	cin >> n >> MOD;
	fac[0] = facinv[0] = 1;
	for (int i = 1; i <= 3*n; i++) {
		fac[i] = ((ll)i*fac[i-1])%MOD;
	}
	facinv[3*n] = inv(fac[3*n]);
	for (int i = 3*n-1; i > 0; i--) {
		facinv[i] = ((ll)(i+1)*facinv[i+1])%MOD;
	}
	dp[0][0] = dp[0][1] = 1;
	// for (int i = 0; i <= n; i++) {
	// 	dp[0][0] = fac[i];
	// 	dp[0][1] = fac[i+1];
	// }
	for (int i = 1; i <= n; i++) {
		for (int a = 0; a <= i-1; a++) {
			for (int t1 = 0; t1 < 2; t1++) {
				for (int t2 = 0; t2 < 2; t2++) {
					if (t2 && a == i-1) continue; // factorial of 
					ll cur = (ll)dp[i-a-1-t2][t2]*fac[a+t1+t2];;
					cur %= MOD;
					cur *= choose(2*i-a-t2-2, a); cur %= MOD;
					// if (i == 2 && t1 == 0) cerr << cur << '\n';
					dp[i][t1] += cur;
					dp[i][t1] %= MOD;
				}
			}
		}
	}
	ll oth = 1;
	for (int i = 2*n-1; i > 0; i -= 2) {
		oth = (oth*i)%MOD;
	}
	cout << (oth+MOD-dp[n][0])%MOD << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 5 ms 212 KB Output is correct
20 Correct 6 ms 340 KB Output is correct
21 Correct 6 ms 304 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 5 ms 212 KB Output is correct
20 Correct 6 ms 340 KB Output is correct
21 Correct 6 ms 304 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 523 ms 456 KB Output is correct
25 Correct 517 ms 488 KB Output is correct
26 Correct 522 ms 592 KB Output is correct
27 Correct 7 ms 340 KB Output is correct
28 Correct 89 ms 364 KB Output is correct
29 Correct 72 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 5 ms 212 KB Output is correct
20 Correct 6 ms 340 KB Output is correct
21 Correct 6 ms 304 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 523 ms 456 KB Output is correct
25 Correct 517 ms 488 KB Output is correct
26 Correct 522 ms 592 KB Output is correct
27 Correct 7 ms 340 KB Output is correct
28 Correct 89 ms 364 KB Output is correct
29 Correct 72 ms 380 KB Output is correct
30 Execution timed out 9050 ms 1416 KB Time limit exceeded
31 Halted 0 ms 0 KB -