Submission #527741

# Submission time Handle Problem Language Result Execution time Memory
527741 2022-02-18T07:31:34 Z iliaM Ruins 3 (JOI20_ruins3) C++17
100 / 100
357 ms 35896 KB
#include <bits/stdc++.h>
using namespace std;
#define cerr cerr << "DEBUG "

constexpr int MOD = 1000 * 1000 * 1000 + 7;

void add(int &a, int b) {
	a += b;
	if (a >= MOD) {
		a -= MOD;
	}
}

int mult(int a, int b) {
	return 1ll * a * b % MOD;
}

int inv(int n) {
	int k = MOD - 2;
	int res = 1;
	for (; k; k >>= 1, n = mult(n, n)) {
		if (k & 1) {
			res = mult(res, n);
		}
	}
	return res;
}

constexpr int N = 3e3 + 10;

int n, dp[N], p[N];
int ncr[N][N];
bool used[N];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	ncr[0][0] = 1;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			if (i + j) {
				ncr[i][j] = (i ? ncr[i - 1][j] : 0);
				add(ncr[i][j], (i && j ? ncr[i - 1][j - 1] : 0));
			}
		}
	}

	p[0] = 1;
	for (int i = 1; i < N; ++i) {
		for (int j = 0; j < i; ++j) {
			add(p[i], mult(ncr[i - 1][j], mult(i - j + 1, mult(p[j], p[i - j - 1]))));
		}
	}
	
	cin >> n;
	for (int i = 0; i < n; ++i) {
		int x;
		cin >> x;
		used[x] = true;
	}

	dp[0] = 1;
	int cnt[2] = {};

	for (int k = n << 1; k > 0; --k) {
		if (used[k]) {
			for (int i = cnt[1] + 1; i >= 0; --i) {
				for (int j = 0; j < i; ++j) {
					add(dp[i], mult(mult(dp[j], mult(i - j + 1, p[i - j - 1])), ncr[cnt[1] - j][i - j - 1]));
				}
			}
		} else {
			for (int i = 0; i <= cnt[1]; ++i) {
				dp[i] = mult(dp[i], max(0, i - cnt[0]));
			}
		}
		cnt[used[k]]++;
		//cerr << used[k] << '\n';
		/*for (int i = 0; i <= cnt[1]; ++i) {
			cerr << "dp[" << i "] = " << dp[i] << '\n';
		}*/
	}
	
	int pw = 1;
	for (int i = 0; i < n; ++i) {
		pw = mult(pw, 2);
	}

	cout << mult(dp[n], inv(pw));
}
# Verdict Execution time Memory Grader output
1 Correct 74 ms 35768 KB Output is correct
2 Correct 71 ms 35772 KB Output is correct
3 Correct 76 ms 35760 KB Output is correct
4 Correct 76 ms 35768 KB Output is correct
5 Correct 85 ms 35772 KB Output is correct
6 Correct 71 ms 35676 KB Output is correct
7 Correct 74 ms 35880 KB Output is correct
8 Correct 88 ms 35704 KB Output is correct
9 Correct 81 ms 35652 KB Output is correct
10 Correct 76 ms 35664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 35768 KB Output is correct
2 Correct 71 ms 35772 KB Output is correct
3 Correct 76 ms 35760 KB Output is correct
4 Correct 76 ms 35768 KB Output is correct
5 Correct 85 ms 35772 KB Output is correct
6 Correct 71 ms 35676 KB Output is correct
7 Correct 74 ms 35880 KB Output is correct
8 Correct 88 ms 35704 KB Output is correct
9 Correct 81 ms 35652 KB Output is correct
10 Correct 76 ms 35664 KB Output is correct
11 Correct 73 ms 35768 KB Output is correct
12 Correct 76 ms 35892 KB Output is correct
13 Correct 79 ms 35780 KB Output is correct
14 Correct 72 ms 35652 KB Output is correct
15 Correct 73 ms 35652 KB Output is correct
16 Correct 72 ms 35772 KB Output is correct
17 Correct 72 ms 35736 KB Output is correct
18 Correct 93 ms 35756 KB Output is correct
19 Correct 91 ms 35688 KB Output is correct
20 Correct 72 ms 35704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 35768 KB Output is correct
2 Correct 71 ms 35772 KB Output is correct
3 Correct 76 ms 35760 KB Output is correct
4 Correct 76 ms 35768 KB Output is correct
5 Correct 85 ms 35772 KB Output is correct
6 Correct 71 ms 35676 KB Output is correct
7 Correct 74 ms 35880 KB Output is correct
8 Correct 88 ms 35704 KB Output is correct
9 Correct 81 ms 35652 KB Output is correct
10 Correct 76 ms 35664 KB Output is correct
11 Correct 73 ms 35768 KB Output is correct
12 Correct 76 ms 35892 KB Output is correct
13 Correct 79 ms 35780 KB Output is correct
14 Correct 72 ms 35652 KB Output is correct
15 Correct 73 ms 35652 KB Output is correct
16 Correct 72 ms 35772 KB Output is correct
17 Correct 72 ms 35736 KB Output is correct
18 Correct 93 ms 35756 KB Output is correct
19 Correct 91 ms 35688 KB Output is correct
20 Correct 72 ms 35704 KB Output is correct
21 Correct 318 ms 35780 KB Output is correct
22 Correct 353 ms 35684 KB Output is correct
23 Correct 313 ms 35768 KB Output is correct
24 Correct 328 ms 35780 KB Output is correct
25 Correct 357 ms 35772 KB Output is correct
26 Correct 318 ms 35896 KB Output is correct
27 Correct 350 ms 35696 KB Output is correct
28 Correct 315 ms 35740 KB Output is correct
29 Correct 325 ms 35876 KB Output is correct
30 Correct 338 ms 35784 KB Output is correct
31 Correct 356 ms 35876 KB Output is correct
32 Correct 333 ms 35732 KB Output is correct
33 Correct 351 ms 35852 KB Output is correct
34 Correct 318 ms 35776 KB Output is correct
35 Correct 355 ms 35784 KB Output is correct
36 Correct 328 ms 35780 KB Output is correct