Submission #96964

# Submission time Handle Problem Language Result Execution time Memory
96964 2019-02-13T02:55:00 Z diamond_duke Security Gate (JOI18_security_gate) C++11
100 / 100
1407 ms 671772 KB
#include <algorithm>
#include <cstring>
#include <cstdio>
using ll = long long;
constexpr int MOD = 1e9 + 7;
inline void add(int &x, const int &y) { (x += y) >= MOD ? (x -= MOD) : 0; }
int n, arr[305], any[305][305], dp_l[155][305][305][2];
namespace solver_1 { int dp_r[305][305][305][2]; }
namespace solver_2 { int dp_r[155][305][605][3]; }
inline void init_any()
{
	memset(any, 0, sizeof(any));
	any[n][0] = 1;
	for (int i = n - 1; i >= 0; i--)
	{
		for (int j = 0; j <= n; j++)
		{
			if (!any[i + 1][j])
				continue;
			if (arr[i] != +1 && j < n)
				add(any[i][j + 1], any[i + 1][j]);
			if (arr[i] != -1 && j > 0)
				add(any[i][j - 1], any[i + 1][j]);
		}
	}
}
inline void init_dp_l()
{
	memset(dp_l, 0, sizeof(dp_l));
	for (int a = 0; a < n / 2; a++)
	{
		auto dp = dp_l[a];
		dp[0][0][!a] = 1;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j <= a; j++)
			{
				for (int x = 0; x < 2; x++)
				{
					if (!dp[i][j][x])
						continue;
					if (arr[i] != -1 && j < a)
						add(dp[i + 1][j + 1][x || j + 1 == a], dp[i][j][x]);
					if (arr[i] != +1 && j > 0)
						add(dp[i + 1][j - 1][x], dp[i][j][x]);
				}
			}
		}
	}
}
inline int solve_1()
{
	using namespace solver_1;
	memset(dp_r, 0, sizeof(dp_r));
	for (int a = 0; a < n; a++)
	{
		auto dp = dp_r[a];
		dp[n][0][!a] = 1;
		for (int i = n - 1; i >= 0; i--)
		{
			for (int j = 0; j <= n; j++)
			{
				if (!dp[i + 1][j][0])
					continue;
				if (arr[i] != +1 && j < n)
					add(dp[i][j + 1][0], dp[i + 1][j][0]);
				if (arr[i] != -1 && j > 0)
					add(dp[i][j - 1][0], dp[i + 1][j][0]);
			}
			if (arr[i] != -1 && a < n)
				add(dp[i][a][1], dp[i + 1][a + 1][0]);
			if (arr[i] != +1 && a > 0)
				add(dp[i][a][1], dp[i + 1][a - 1][0]);
			for (int j = 0; j <= std::min(a * 2, n - 1); j++)
			{
				if (!dp[i + 1][j][1])
					continue;
				if (arr[i] != +1 && j >= a)
					add(dp[i][j + 1][1], dp[i + 1][j][1]);
				if (arr[i] != -1 && j >= a + 2)
					add(dp[i][j - 1][1], dp[i + 1][j][1]);
			}
		}
	}
	int res = 0;
	for (int a = 0; a <= n / 2; a++)
	{
		for (int b = 1; a + b <= n / 2; b++)
		{
			for (int pos = 0; pos < n; pos += 2)
			{
				if (arr[pos] == 1)
					continue;
				int val = a >= b ? any[pos + 1][2 * b - 1] :
						  dp_r[a + b][pos + 1][2 * b - 1][1];
				res = (res + (ll)dp_l[a][pos][0][1] * val) % MOD;
			}
		}
	}
	return res;
}
inline int solve_2(int t)
{
	using namespace solver_2;
	memset(dp_r, 0, sizeof(dp_r));
	for (int a = 0; a < n / 2; a++)
	{
		auto dp = dp_r[a];
		dp[n][n][!(a + t)] = 1;
		for (int i = n - 1; i >= 0; i--)
		{
			for (int j = 0; j <= n; j++)
			{
				if (!dp[i + 1][j + n][0])
					continue;
				if (arr[i] != +1 && j < n)
					add(dp[i][j + 1 + n][j + 1 == a + t], dp[i + 1][j + n][0]);
				if (arr[i] != -1 && j > 0)
					add(dp[i][j - 1 + n][0], dp[i + 1][j + n][0]);
			}
			for (int j = 0; j <= n; j++)
			{
				if (!dp[i + 1][j + n][1])
					continue;
				if (arr[i] != +1)
					add(dp[i][j + 1 + n][1], dp[i + 1][j + n][1]);
				if (arr[i] != -1)
					add(dp[i][j - 1 + n][1 + !j], dp[i + 1][j + n][1]);
			}
			for (int j = -n; j <= std::min(a * 2, n - 1); j++)
			{
				if (!dp[i + 1][j + n][2])
					continue;
				if (arr[i] != +1 && j < a * 2)
					add(dp[i][j + 1 + n][2], dp[i + 1][j + n][2]);
				if (arr[i] != -1 && j > -n)
					add(dp[i][j - 1 + n][2], dp[i + 1][j + n][2]);
			}
		}
	}
	int res = 0;
	for (int a = 0; a <= n / 2; a++)
	{
		for (int b = -n / 2 + 1; b < n / 2; b++)
		{
			if (a + b < 0 || a + (b < 0 ? -b : b) >= n / 2)
				continue;
			for (int pos = 0; pos < n; pos += 2)
			{
				if (arr[pos] == 1)
					continue;
				res = (res + (ll)dp_l[a][pos][0][1] *
					   dp_r[a + b][pos + 1][b * 2 - 1 + n][2]) % MOD;
			}
		}
	}
	return res;
}
char str[305];
int main()
{
	// freopen("JOISC2018-D3T3.in", "r", stdin);
	scanf("%d%s", &n, str);
	for (int i = 0; i < n; i++)
		arr[i] = str[i] == '(' ? 1 : str[i] == ')' ? -1 : 0;
	init_any();
	int ans = any[0][0];
	init_dp_l();
	add(ans, solve_1());
	add(ans, solve_2(0));
	std::reverse(arr, arr + n);
	for (int i = 0; i < n; i++)
		arr[i] = -arr[i];
	init_any();
	init_dp_l();
	add(ans, solve_1());
	add(ans, solve_2(1));
	printf("%d\n", ans);
	return 0;
}

Compilation message

securitygate.cpp: In function 'int main()':
securitygate.cpp:163:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%s", &n, str);
  ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1221 ms 671528 KB Output is correct
2 Correct 719 ms 671608 KB Output is correct
3 Correct 829 ms 671492 KB Output is correct
4 Correct 762 ms 671572 KB Output is correct
5 Correct 719 ms 671520 KB Output is correct
6 Correct 717 ms 671736 KB Output is correct
7 Correct 701 ms 671572 KB Output is correct
8 Correct 653 ms 671500 KB Output is correct
9 Correct 655 ms 671608 KB Output is correct
10 Correct 592 ms 671524 KB Output is correct
11 Correct 618 ms 671540 KB Output is correct
12 Correct 665 ms 671516 KB Output is correct
13 Correct 662 ms 671480 KB Output is correct
14 Correct 613 ms 671608 KB Output is correct
15 Correct 642 ms 671580 KB Output is correct
16 Correct 750 ms 671528 KB Output is correct
17 Correct 733 ms 671584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1221 ms 671528 KB Output is correct
2 Correct 719 ms 671608 KB Output is correct
3 Correct 829 ms 671492 KB Output is correct
4 Correct 762 ms 671572 KB Output is correct
5 Correct 719 ms 671520 KB Output is correct
6 Correct 717 ms 671736 KB Output is correct
7 Correct 701 ms 671572 KB Output is correct
8 Correct 653 ms 671500 KB Output is correct
9 Correct 655 ms 671608 KB Output is correct
10 Correct 592 ms 671524 KB Output is correct
11 Correct 618 ms 671540 KB Output is correct
12 Correct 665 ms 671516 KB Output is correct
13 Correct 662 ms 671480 KB Output is correct
14 Correct 613 ms 671608 KB Output is correct
15 Correct 642 ms 671580 KB Output is correct
16 Correct 750 ms 671528 KB Output is correct
17 Correct 733 ms 671584 KB Output is correct
18 Correct 666 ms 671592 KB Output is correct
19 Correct 694 ms 671704 KB Output is correct
20 Correct 652 ms 671684 KB Output is correct
21 Correct 722 ms 671480 KB Output is correct
22 Correct 710 ms 671564 KB Output is correct
23 Correct 635 ms 671460 KB Output is correct
24 Correct 690 ms 671412 KB Output is correct
25 Correct 646 ms 671480 KB Output is correct
26 Correct 732 ms 671528 KB Output is correct
27 Correct 704 ms 671648 KB Output is correct
28 Correct 730 ms 671512 KB Output is correct
29 Correct 682 ms 671608 KB Output is correct
30 Correct 694 ms 671532 KB Output is correct
31 Correct 723 ms 671456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1221 ms 671528 KB Output is correct
2 Correct 719 ms 671608 KB Output is correct
3 Correct 829 ms 671492 KB Output is correct
4 Correct 762 ms 671572 KB Output is correct
5 Correct 719 ms 671520 KB Output is correct
6 Correct 717 ms 671736 KB Output is correct
7 Correct 701 ms 671572 KB Output is correct
8 Correct 653 ms 671500 KB Output is correct
9 Correct 655 ms 671608 KB Output is correct
10 Correct 592 ms 671524 KB Output is correct
11 Correct 618 ms 671540 KB Output is correct
12 Correct 665 ms 671516 KB Output is correct
13 Correct 662 ms 671480 KB Output is correct
14 Correct 613 ms 671608 KB Output is correct
15 Correct 642 ms 671580 KB Output is correct
16 Correct 750 ms 671528 KB Output is correct
17 Correct 733 ms 671584 KB Output is correct
18 Correct 666 ms 671592 KB Output is correct
19 Correct 694 ms 671704 KB Output is correct
20 Correct 652 ms 671684 KB Output is correct
21 Correct 722 ms 671480 KB Output is correct
22 Correct 710 ms 671564 KB Output is correct
23 Correct 635 ms 671460 KB Output is correct
24 Correct 690 ms 671412 KB Output is correct
25 Correct 646 ms 671480 KB Output is correct
26 Correct 732 ms 671528 KB Output is correct
27 Correct 704 ms 671648 KB Output is correct
28 Correct 730 ms 671512 KB Output is correct
29 Correct 682 ms 671608 KB Output is correct
30 Correct 694 ms 671532 KB Output is correct
31 Correct 723 ms 671456 KB Output is correct
32 Correct 716 ms 671604 KB Output is correct
33 Correct 785 ms 671612 KB Output is correct
34 Correct 702 ms 671548 KB Output is correct
35 Correct 725 ms 671576 KB Output is correct
36 Correct 650 ms 671560 KB Output is correct
37 Correct 726 ms 671484 KB Output is correct
38 Correct 728 ms 671612 KB Output is correct
39 Correct 692 ms 671480 KB Output is correct
40 Correct 732 ms 671480 KB Output is correct
41 Correct 698 ms 671484 KB Output is correct
42 Correct 687 ms 671608 KB Output is correct
43 Correct 620 ms 671608 KB Output is correct
44 Correct 610 ms 671688 KB Output is correct
45 Correct 637 ms 671480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1221 ms 671528 KB Output is correct
2 Correct 719 ms 671608 KB Output is correct
3 Correct 829 ms 671492 KB Output is correct
4 Correct 762 ms 671572 KB Output is correct
5 Correct 719 ms 671520 KB Output is correct
6 Correct 717 ms 671736 KB Output is correct
7 Correct 701 ms 671572 KB Output is correct
8 Correct 653 ms 671500 KB Output is correct
9 Correct 655 ms 671608 KB Output is correct
10 Correct 592 ms 671524 KB Output is correct
11 Correct 618 ms 671540 KB Output is correct
12 Correct 665 ms 671516 KB Output is correct
13 Correct 662 ms 671480 KB Output is correct
14 Correct 613 ms 671608 KB Output is correct
15 Correct 642 ms 671580 KB Output is correct
16 Correct 750 ms 671528 KB Output is correct
17 Correct 733 ms 671584 KB Output is correct
18 Correct 666 ms 671592 KB Output is correct
19 Correct 694 ms 671704 KB Output is correct
20 Correct 652 ms 671684 KB Output is correct
21 Correct 722 ms 671480 KB Output is correct
22 Correct 710 ms 671564 KB Output is correct
23 Correct 635 ms 671460 KB Output is correct
24 Correct 690 ms 671412 KB Output is correct
25 Correct 646 ms 671480 KB Output is correct
26 Correct 732 ms 671528 KB Output is correct
27 Correct 704 ms 671648 KB Output is correct
28 Correct 730 ms 671512 KB Output is correct
29 Correct 682 ms 671608 KB Output is correct
30 Correct 694 ms 671532 KB Output is correct
31 Correct 723 ms 671456 KB Output is correct
32 Correct 716 ms 671604 KB Output is correct
33 Correct 785 ms 671612 KB Output is correct
34 Correct 702 ms 671548 KB Output is correct
35 Correct 725 ms 671576 KB Output is correct
36 Correct 650 ms 671560 KB Output is correct
37 Correct 726 ms 671484 KB Output is correct
38 Correct 728 ms 671612 KB Output is correct
39 Correct 692 ms 671480 KB Output is correct
40 Correct 732 ms 671480 KB Output is correct
41 Correct 698 ms 671484 KB Output is correct
42 Correct 687 ms 671608 KB Output is correct
43 Correct 620 ms 671608 KB Output is correct
44 Correct 610 ms 671688 KB Output is correct
45 Correct 637 ms 671480 KB Output is correct
46 Correct 631 ms 671504 KB Output is correct
47 Correct 634 ms 671612 KB Output is correct
48 Correct 626 ms 671524 KB Output is correct
49 Correct 718 ms 671736 KB Output is correct
50 Correct 711 ms 671612 KB Output is correct
51 Correct 689 ms 671608 KB Output is correct
52 Correct 692 ms 671608 KB Output is correct
53 Correct 695 ms 671544 KB Output is correct
54 Correct 638 ms 671508 KB Output is correct
55 Correct 678 ms 671620 KB Output is correct
56 Correct 719 ms 671452 KB Output is correct
57 Correct 706 ms 671608 KB Output is correct
58 Correct 637 ms 671456 KB Output is correct
59 Correct 689 ms 671632 KB Output is correct
60 Correct 630 ms 671628 KB Output is correct
61 Correct 647 ms 671772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1221 ms 671528 KB Output is correct
2 Correct 719 ms 671608 KB Output is correct
3 Correct 829 ms 671492 KB Output is correct
4 Correct 762 ms 671572 KB Output is correct
5 Correct 719 ms 671520 KB Output is correct
6 Correct 717 ms 671736 KB Output is correct
7 Correct 701 ms 671572 KB Output is correct
8 Correct 653 ms 671500 KB Output is correct
9 Correct 655 ms 671608 KB Output is correct
10 Correct 592 ms 671524 KB Output is correct
11 Correct 618 ms 671540 KB Output is correct
12 Correct 665 ms 671516 KB Output is correct
13 Correct 662 ms 671480 KB Output is correct
14 Correct 613 ms 671608 KB Output is correct
15 Correct 642 ms 671580 KB Output is correct
16 Correct 750 ms 671528 KB Output is correct
17 Correct 733 ms 671584 KB Output is correct
18 Correct 666 ms 671592 KB Output is correct
19 Correct 694 ms 671704 KB Output is correct
20 Correct 652 ms 671684 KB Output is correct
21 Correct 722 ms 671480 KB Output is correct
22 Correct 710 ms 671564 KB Output is correct
23 Correct 635 ms 671460 KB Output is correct
24 Correct 690 ms 671412 KB Output is correct
25 Correct 646 ms 671480 KB Output is correct
26 Correct 732 ms 671528 KB Output is correct
27 Correct 704 ms 671648 KB Output is correct
28 Correct 730 ms 671512 KB Output is correct
29 Correct 682 ms 671608 KB Output is correct
30 Correct 694 ms 671532 KB Output is correct
31 Correct 723 ms 671456 KB Output is correct
32 Correct 716 ms 671604 KB Output is correct
33 Correct 785 ms 671612 KB Output is correct
34 Correct 702 ms 671548 KB Output is correct
35 Correct 725 ms 671576 KB Output is correct
36 Correct 650 ms 671560 KB Output is correct
37 Correct 726 ms 671484 KB Output is correct
38 Correct 728 ms 671612 KB Output is correct
39 Correct 692 ms 671480 KB Output is correct
40 Correct 732 ms 671480 KB Output is correct
41 Correct 698 ms 671484 KB Output is correct
42 Correct 687 ms 671608 KB Output is correct
43 Correct 620 ms 671608 KB Output is correct
44 Correct 610 ms 671688 KB Output is correct
45 Correct 637 ms 671480 KB Output is correct
46 Correct 631 ms 671504 KB Output is correct
47 Correct 634 ms 671612 KB Output is correct
48 Correct 626 ms 671524 KB Output is correct
49 Correct 718 ms 671736 KB Output is correct
50 Correct 711 ms 671612 KB Output is correct
51 Correct 689 ms 671608 KB Output is correct
52 Correct 692 ms 671608 KB Output is correct
53 Correct 695 ms 671544 KB Output is correct
54 Correct 638 ms 671508 KB Output is correct
55 Correct 678 ms 671620 KB Output is correct
56 Correct 719 ms 671452 KB Output is correct
57 Correct 706 ms 671608 KB Output is correct
58 Correct 637 ms 671456 KB Output is correct
59 Correct 689 ms 671632 KB Output is correct
60 Correct 630 ms 671628 KB Output is correct
61 Correct 647 ms 671772 KB Output is correct
62 Correct 1053 ms 671392 KB Output is correct
63 Correct 997 ms 671684 KB Output is correct
64 Correct 827 ms 671524 KB Output is correct
65 Correct 746 ms 671608 KB Output is correct
66 Correct 1363 ms 671484 KB Output is correct
67 Correct 1384 ms 671656 KB Output is correct
68 Correct 1407 ms 671556 KB Output is correct
69 Correct 797 ms 671480 KB Output is correct
70 Correct 807 ms 671480 KB Output is correct
71 Correct 758 ms 671536 KB Output is correct
72 Correct 726 ms 671484 KB Output is correct
73 Correct 1311 ms 671496 KB Output is correct
74 Correct 1128 ms 671560 KB Output is correct