Submission #376036

# Submission time Handle Problem Language Result Execution time Memory
376036 2021-03-10T18:36:06 Z patrikpavic2 Ruins 3 (JOI20_ruins3) C++17
100 / 100
490 ms 11884 KB
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long ll;

const int N = 1205;
const int MOD = 1e9 + 7;

inline int add(int A, int B){
	if(A + B >= MOD)
		return A + B - MOD;
	return A + B;
}

inline int sub(int A, int B){
	if(A - B < 0)
		return A - B + MOD;
	return A - B;
}

inline int mul(int A, int B){
	return (ll)A * B % MOD; 
}

inline int pot(int A, int B){
	int ret = 1, bs = A;
	for(; B ; B >>= 1){
		if(B & 1) ret = mul(ret, bs);
		bs = mul(bs, bs);
	}
	return ret;
}

int C[N], cat[N], ch[N][N], div2 = (MOD + 1) / 2;
int ubac[N], van[N], A[N], n, dp[N][N], fak[N];

void precompute(){
	for(int i = 0;i < N;i++){
		ch[i][0] = 1, ch[i][i] = 1;
		fak[i] = (i ? mul(fak[i - 1], i) : 1);
	}
	for(int i = 0;i < N;i++)
		for(int j = 1;j < i;j++)
			ch[i][j] = add(ch[i - 1][j - 1], ch[i - 1][j]);
	for(int i = 0;2 * i < N;i++)
		cat[i] = mul(pot(i + 1, MOD - 2), ch[2 * i][i]);
	for(int i = 0;2 * i < N;i++){
		for(int j = 0;j < i;j++){
			if((i - j) % 2 == 1){
				int k = (i - j - 1) / 2;
				C[i] = add(C[i], mul(k, mul(mul(cat[k], ch[i - 1][j]), mul(fak[i - 1], pot(div2, 2 * k)))));
				C[i] = add(C[i], mul(j, mul(mul(cat[k], ch[i - 1][j]), mul(fak[i - 1], pot(div2, 2 * k + 1)))));
				C[i] = add(C[i], mul(1, mul(mul(cat[k], ch[i - 1][j]), mul(fak[i - 1], pot(div2, 2 * k)))));
			}
		}
	}
}

int f(int i, int prf){
	if(i == 0) return (prf == n);
	if(dp[i][prf] != -1) return dp[i][prf];
	if(!A[i]){
		dp[i][prf] = mul(f(i - 1, prf), prf - van[i + 1]);
		return dp[i][prf];
	}
	else{
		int ret = f(i - 1, prf);
		for(int prf2 = prf + 1;prf2 <= ubac[i];prf2++){
			if(prf2 != n - 1 || ubac[i] != n)
				ret = add(ret, mul(f(i - 1, prf2), mul(C[prf2 - prf], ch[ubac[i] - prf - 1][prf2 - prf - 1])));
		}
		return dp[i][prf] = ret;
	}
}

int main(){
	memset(dp, -1, sizeof(dp));
	scanf("%d", &n); precompute();
	for(int i = 0;i < n;i++){
		int x; scanf("%d", &x); 
		A[x] = 1;
	}
	for(int i = 2 * n;i >= 1;i--){
		ubac[i] = ubac[i + 1] + A[i];
		van[i] = van[i + 1] + !A[i];
	}
	printf("%d\n", f(2 * n, 0));
	return 0;
}

Compilation message

ruins3.cpp: In function 'int main()':
ruins3.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   80 |  scanf("%d", &n); precompute();
      |  ~~~~~^~~~~~~~~~
ruins3.cpp:82:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   82 |   int x; scanf("%d", &x);
      |          ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 11628 KB Output is correct
2 Correct 21 ms 11628 KB Output is correct
3 Correct 21 ms 11628 KB Output is correct
4 Correct 23 ms 11628 KB Output is correct
5 Correct 22 ms 11628 KB Output is correct
6 Correct 21 ms 11628 KB Output is correct
7 Correct 25 ms 11628 KB Output is correct
8 Correct 21 ms 11628 KB Output is correct
9 Correct 21 ms 11628 KB Output is correct
10 Correct 21 ms 11628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 11628 KB Output is correct
2 Correct 21 ms 11628 KB Output is correct
3 Correct 21 ms 11628 KB Output is correct
4 Correct 23 ms 11628 KB Output is correct
5 Correct 22 ms 11628 KB Output is correct
6 Correct 21 ms 11628 KB Output is correct
7 Correct 25 ms 11628 KB Output is correct
8 Correct 21 ms 11628 KB Output is correct
9 Correct 21 ms 11628 KB Output is correct
10 Correct 21 ms 11628 KB Output is correct
11 Correct 22 ms 11776 KB Output is correct
12 Correct 21 ms 11628 KB Output is correct
13 Correct 22 ms 11672 KB Output is correct
14 Correct 22 ms 11628 KB Output is correct
15 Correct 22 ms 11628 KB Output is correct
16 Correct 21 ms 11628 KB Output is correct
17 Correct 22 ms 11628 KB Output is correct
18 Correct 21 ms 11628 KB Output is correct
19 Correct 22 ms 11628 KB Output is correct
20 Correct 26 ms 11628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 11628 KB Output is correct
2 Correct 21 ms 11628 KB Output is correct
3 Correct 21 ms 11628 KB Output is correct
4 Correct 23 ms 11628 KB Output is correct
5 Correct 22 ms 11628 KB Output is correct
6 Correct 21 ms 11628 KB Output is correct
7 Correct 25 ms 11628 KB Output is correct
8 Correct 21 ms 11628 KB Output is correct
9 Correct 21 ms 11628 KB Output is correct
10 Correct 21 ms 11628 KB Output is correct
11 Correct 22 ms 11776 KB Output is correct
12 Correct 21 ms 11628 KB Output is correct
13 Correct 22 ms 11672 KB Output is correct
14 Correct 22 ms 11628 KB Output is correct
15 Correct 22 ms 11628 KB Output is correct
16 Correct 21 ms 11628 KB Output is correct
17 Correct 22 ms 11628 KB Output is correct
18 Correct 21 ms 11628 KB Output is correct
19 Correct 22 ms 11628 KB Output is correct
20 Correct 26 ms 11628 KB Output is correct
21 Correct 462 ms 11756 KB Output is correct
22 Correct 469 ms 11884 KB Output is correct
23 Correct 482 ms 11884 KB Output is correct
24 Correct 468 ms 11804 KB Output is correct
25 Correct 469 ms 11756 KB Output is correct
26 Correct 490 ms 11852 KB Output is correct
27 Correct 473 ms 11884 KB Output is correct
28 Correct 478 ms 11884 KB Output is correct
29 Correct 464 ms 11884 KB Output is correct
30 Correct 481 ms 11756 KB Output is correct
31 Correct 473 ms 11884 KB Output is correct
32 Correct 471 ms 11884 KB Output is correct
33 Correct 472 ms 11884 KB Output is correct
34 Correct 475 ms 11884 KB Output is correct
35 Correct 477 ms 11884 KB Output is correct
36 Correct 484 ms 11884 KB Output is correct