Submission #95843

# Submission time Handle Problem Language Result Execution time Memory
95843 2019-02-03T03:18:48 Z diamond_duke Security Gate (JOI18_security_gate) C++11
100 / 100
1488 ms 784988 KB
#include <algorithm>
#include <cstring>
#include <cstdio>
using ll = long long;
constexpr int MOD = 1e9 + 7;
inline void upd(int &x, int y) { (x += y) >= MOD ? (x -= MOD) : 0; }
int arr[305], n;
namespace solver_0
{
	int dp[305][305];
	int solve()
	{
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j <= n; j++)
			{
				if (!dp[i][j])
					continue;
				if (arr[i] != -1 && j != n)
					upd(dp[i + 1][j + 1], dp[i][j]);
				if (arr[i] != 1 && j)
					upd(dp[i + 1][j - 1], dp[i][j]);
			}
		}
		return dp[n][0];
	}
}
namespace solver_1
{
	int dp_l[155][305][305][2], dp_r[305][305][305][2], suf[305][305];
	int solve()
	{
		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 (j != a && arr[i] != -1)
							upd(dp[i + 1][j + 1][x || j + 1 == a], dp[i][j][x]);
						if (j && arr[i] != 1)
							upd(dp[i + 1][j - 1][x], dp[i][j][x]);
					}
				}
			}
		}
		memset(dp_r, 0, sizeof(dp_r));
		for (int c = 0; c < n; c++)
		{
			auto dp = dp_r[c];
			dp[n][0][!c] = 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)
						upd(dp[i][j + 1][0], dp[i + 1][j][0]);
					if (j && arr[i] != -1)
						upd(dp[i][j - 1][0], dp[i + 1][j][0]);
				}
				if (c && arr[i] != 1)
					upd(dp[i][c][1], dp[i + 1][c - 1][0]);
				if (arr[i] != -1)
					upd(dp[i][c][1], dp[i + 1][c + 1][0]);
				for (int j = 0; j <= std::min(c * 2, n - 1); j++)
				{
					if (!dp[i + 1][j][1])
						continue;
					if (j >= c && arr[i] != 1)
						upd(dp[i][j + 1][1], dp[i + 1][j][1]);
					if (j >= c + 2 && arr[i] != -1)
						upd(dp[i][j - 1][1], dp[i + 1][j][1]);
				}
			}
		}
		memset(suf, 0, sizeof(suf));
		suf[n][0] = 1;
		for (int i = n - 1; i >= 0; i--)
		{
			for (int j = 0; j <= n; j++)
			{
				if (j != n && arr[i] != 1)
					upd(suf[i][j + 1], suf[i + 1][j]);
				if (j && arr[i] != -1)
					upd(suf[i][j - 1], suf[i + 1][j]);
			}
		}
		int res = 0;
		for (int a = 0; a <= n / 2; a++)
		{
			for (int c = 1; a + c <= n / 2; c++)
			{
				for (int len = 0; len < n; len += 2)
				{
					if (arr[len] == 1)
						continue;
					int coef = a >= c ? suf[len + 1][2 * c - 1] :
							   dp_r[a + c][len + 1][2 * c - 1][1];
					res = (res + (ll)dp_l[a][len][0][1] * coef) % MOD;
				}
			}
		}
		return res;
	}
}
namespace solver_2
{
	int dp_l[155][305][305][2], dp_r[155][305][605][3];
	void init(int add)
	{
		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 (j != a && arr[i] != -1)
							upd(dp[i + 1][j + 1][x || j + 1 == a], dp[i][j][x]);
						if (j && arr[i] != 1)
							upd(dp[i + 1][j - 1][x], dp[i][j][x]);
					}
				}
			}
		}
		memset(dp_r, 0, sizeof(dp_r));
		for (int c = 0; c < n / 2; c++)
		{
			auto dp = dp_r[c];
			dp[n][n][!(c + add)] = 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)
						upd(dp[i][j + 1 + n][j + 1 == c + add], dp[i + 1][j + n][0]);
					if (j && arr[i] != -1)
						upd(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)
						upd(dp[i][j + 1 + n][1], dp[i + 1][j + n][1]);
					if (arr[i] != -1)
						upd(dp[i][j - 1 + n][1 + !j], dp[i + 1][j + n][1]);
				}
				for (int j = -n; j <= std::min(c * 2, n - 1); j++)
				{
					if (!dp[i + 1][j + n][2])
						continue;
					if (j != c * 2 && arr[i] != 1)
						upd(dp[i][j + 1 + n][2], dp[i + 1][j + n][2]);
					if (j != -n && arr[i] != -1)
						upd(dp[i][j - 1 + n][2], dp[i + 1][j + n][2]);
				}
			}
		}
	}
	int solve()
	{
		int res = 0;
		for (int t = 0; t < 2; t++)
		{
			init(t);
			for (int a = 0; a <= n / 2; a++)
			{
				for (int c = -n / 2 + 1; c < n / 2; c++)
				{
					if (a + c < 0 || a + (c < 0 ? -c : c) >= n / 2)
						continue;
					for (int len = 0; len < n; len += 2)
					{
						if (arr[len] == 1)
							continue;
						res = (res + (ll)dp_l[a][len][0][1] *
							   dp_r[a + c][len + 1][c * 2 - 1 + n][2]) % MOD;
					}
				}
			}
			std::reverse(arr, arr + n);
			for (int i = 0; i < n; i++)
				arr[i] *= -1;
		}
		return res;
	}
}
char str[305];
int main()
{
	// freopen("loj-2839.in", "r", stdin);
	scanf("%d%s", &n, str);
	if (n & 1)
	{
		puts("0");
		return 0;
	}
	for (int i = 0; i < n; i++)
		arr[i] = str[i] == '(' ? 1 : str[i] == ')' ? -1 : 0;
	int ans = solver_0::solve();
	for (int t = 0; t < 2; t++)
	{
		upd(ans, solver_1::solve());
		std::reverse(arr, arr + n);
		for (int i = 0; i < n; i++)
			arr[i] *= -1;
	}
	upd(ans, solver_2::solve());
	printf("%d\n", ans);
	return 0;
}

Compilation message

securitygate.cpp: In function 'int main()':
securitygate.cpp:212: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 694 ms 784760 KB Output is correct
2 Correct 833 ms 784732 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 808 ms 784612 KB Output is correct
5 Correct 811 ms 784792 KB Output is correct
6 Correct 806 ms 784780 KB Output is correct
7 Correct 810 ms 784880 KB Output is correct
8 Correct 690 ms 784888 KB Output is correct
9 Correct 692 ms 784888 KB Output is correct
10 Correct 710 ms 784760 KB Output is correct
11 Correct 828 ms 784816 KB Output is correct
12 Correct 819 ms 784716 KB Output is correct
13 Correct 827 ms 784744 KB Output is correct
14 Correct 813 ms 784724 KB Output is correct
15 Correct 836 ms 784760 KB Output is correct
16 Correct 702 ms 784788 KB Output is correct
17 Correct 688 ms 784732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 694 ms 784760 KB Output is correct
2 Correct 833 ms 784732 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 808 ms 784612 KB Output is correct
5 Correct 811 ms 784792 KB Output is correct
6 Correct 806 ms 784780 KB Output is correct
7 Correct 810 ms 784880 KB Output is correct
8 Correct 690 ms 784888 KB Output is correct
9 Correct 692 ms 784888 KB Output is correct
10 Correct 710 ms 784760 KB Output is correct
11 Correct 828 ms 784816 KB Output is correct
12 Correct 819 ms 784716 KB Output is correct
13 Correct 827 ms 784744 KB Output is correct
14 Correct 813 ms 784724 KB Output is correct
15 Correct 836 ms 784760 KB Output is correct
16 Correct 702 ms 784788 KB Output is correct
17 Correct 688 ms 784732 KB Output is correct
18 Correct 677 ms 784700 KB Output is correct
19 Correct 676 ms 784760 KB Output is correct
20 Correct 681 ms 784732 KB Output is correct
21 Correct 690 ms 784732 KB Output is correct
22 Correct 713 ms 784732 KB Output is correct
23 Correct 800 ms 784812 KB Output is correct
24 Correct 694 ms 784632 KB Output is correct
25 Correct 712 ms 784760 KB Output is correct
26 Correct 716 ms 784792 KB Output is correct
27 Correct 755 ms 784888 KB Output is correct
28 Correct 812 ms 784860 KB Output is correct
29 Correct 834 ms 784656 KB Output is correct
30 Correct 839 ms 784888 KB Output is correct
31 Correct 836 ms 784764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 694 ms 784760 KB Output is correct
2 Correct 833 ms 784732 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 808 ms 784612 KB Output is correct
5 Correct 811 ms 784792 KB Output is correct
6 Correct 806 ms 784780 KB Output is correct
7 Correct 810 ms 784880 KB Output is correct
8 Correct 690 ms 784888 KB Output is correct
9 Correct 692 ms 784888 KB Output is correct
10 Correct 710 ms 784760 KB Output is correct
11 Correct 828 ms 784816 KB Output is correct
12 Correct 819 ms 784716 KB Output is correct
13 Correct 827 ms 784744 KB Output is correct
14 Correct 813 ms 784724 KB Output is correct
15 Correct 836 ms 784760 KB Output is correct
16 Correct 702 ms 784788 KB Output is correct
17 Correct 688 ms 784732 KB Output is correct
18 Correct 677 ms 784700 KB Output is correct
19 Correct 676 ms 784760 KB Output is correct
20 Correct 681 ms 784732 KB Output is correct
21 Correct 690 ms 784732 KB Output is correct
22 Correct 713 ms 784732 KB Output is correct
23 Correct 800 ms 784812 KB Output is correct
24 Correct 694 ms 784632 KB Output is correct
25 Correct 712 ms 784760 KB Output is correct
26 Correct 716 ms 784792 KB Output is correct
27 Correct 755 ms 784888 KB Output is correct
28 Correct 812 ms 784860 KB Output is correct
29 Correct 834 ms 784656 KB Output is correct
30 Correct 839 ms 784888 KB Output is correct
31 Correct 836 ms 784764 KB Output is correct
32 Correct 816 ms 784860 KB Output is correct
33 Correct 810 ms 784860 KB Output is correct
34 Correct 824 ms 784888 KB Output is correct
35 Correct 812 ms 784848 KB Output is correct
36 Correct 838 ms 784772 KB Output is correct
37 Correct 761 ms 784632 KB Output is correct
38 Correct 792 ms 784732 KB Output is correct
39 Correct 807 ms 784760 KB Output is correct
40 Correct 834 ms 784888 KB Output is correct
41 Correct 844 ms 784756 KB Output is correct
42 Correct 834 ms 784848 KB Output is correct
43 Correct 833 ms 784912 KB Output is correct
44 Correct 838 ms 784952 KB Output is correct
45 Correct 837 ms 784732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 694 ms 784760 KB Output is correct
2 Correct 833 ms 784732 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 808 ms 784612 KB Output is correct
5 Correct 811 ms 784792 KB Output is correct
6 Correct 806 ms 784780 KB Output is correct
7 Correct 810 ms 784880 KB Output is correct
8 Correct 690 ms 784888 KB Output is correct
9 Correct 692 ms 784888 KB Output is correct
10 Correct 710 ms 784760 KB Output is correct
11 Correct 828 ms 784816 KB Output is correct
12 Correct 819 ms 784716 KB Output is correct
13 Correct 827 ms 784744 KB Output is correct
14 Correct 813 ms 784724 KB Output is correct
15 Correct 836 ms 784760 KB Output is correct
16 Correct 702 ms 784788 KB Output is correct
17 Correct 688 ms 784732 KB Output is correct
18 Correct 677 ms 784700 KB Output is correct
19 Correct 676 ms 784760 KB Output is correct
20 Correct 681 ms 784732 KB Output is correct
21 Correct 690 ms 784732 KB Output is correct
22 Correct 713 ms 784732 KB Output is correct
23 Correct 800 ms 784812 KB Output is correct
24 Correct 694 ms 784632 KB Output is correct
25 Correct 712 ms 784760 KB Output is correct
26 Correct 716 ms 784792 KB Output is correct
27 Correct 755 ms 784888 KB Output is correct
28 Correct 812 ms 784860 KB Output is correct
29 Correct 834 ms 784656 KB Output is correct
30 Correct 839 ms 784888 KB Output is correct
31 Correct 836 ms 784764 KB Output is correct
32 Correct 816 ms 784860 KB Output is correct
33 Correct 810 ms 784860 KB Output is correct
34 Correct 824 ms 784888 KB Output is correct
35 Correct 812 ms 784848 KB Output is correct
36 Correct 838 ms 784772 KB Output is correct
37 Correct 761 ms 784632 KB Output is correct
38 Correct 792 ms 784732 KB Output is correct
39 Correct 807 ms 784760 KB Output is correct
40 Correct 834 ms 784888 KB Output is correct
41 Correct 844 ms 784756 KB Output is correct
42 Correct 834 ms 784848 KB Output is correct
43 Correct 833 ms 784912 KB Output is correct
44 Correct 838 ms 784952 KB Output is correct
45 Correct 837 ms 784732 KB Output is correct
46 Correct 819 ms 784812 KB Output is correct
47 Correct 839 ms 784760 KB Output is correct
48 Correct 2 ms 256 KB Output is correct
49 Correct 849 ms 784760 KB Output is correct
50 Correct 759 ms 784888 KB Output is correct
51 Correct 672 ms 784760 KB Output is correct
52 Correct 688 ms 784760 KB Output is correct
53 Correct 757 ms 784760 KB Output is correct
54 Correct 693 ms 784664 KB Output is correct
55 Correct 693 ms 784760 KB Output is correct
56 Correct 667 ms 784632 KB Output is correct
57 Correct 724 ms 784920 KB Output is correct
58 Correct 817 ms 784800 KB Output is correct
59 Correct 813 ms 784692 KB Output is correct
60 Correct 824 ms 784888 KB Output is correct
61 Correct 757 ms 784700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 694 ms 784760 KB Output is correct
2 Correct 833 ms 784732 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 808 ms 784612 KB Output is correct
5 Correct 811 ms 784792 KB Output is correct
6 Correct 806 ms 784780 KB Output is correct
7 Correct 810 ms 784880 KB Output is correct
8 Correct 690 ms 784888 KB Output is correct
9 Correct 692 ms 784888 KB Output is correct
10 Correct 710 ms 784760 KB Output is correct
11 Correct 828 ms 784816 KB Output is correct
12 Correct 819 ms 784716 KB Output is correct
13 Correct 827 ms 784744 KB Output is correct
14 Correct 813 ms 784724 KB Output is correct
15 Correct 836 ms 784760 KB Output is correct
16 Correct 702 ms 784788 KB Output is correct
17 Correct 688 ms 784732 KB Output is correct
18 Correct 677 ms 784700 KB Output is correct
19 Correct 676 ms 784760 KB Output is correct
20 Correct 681 ms 784732 KB Output is correct
21 Correct 690 ms 784732 KB Output is correct
22 Correct 713 ms 784732 KB Output is correct
23 Correct 800 ms 784812 KB Output is correct
24 Correct 694 ms 784632 KB Output is correct
25 Correct 712 ms 784760 KB Output is correct
26 Correct 716 ms 784792 KB Output is correct
27 Correct 755 ms 784888 KB Output is correct
28 Correct 812 ms 784860 KB Output is correct
29 Correct 834 ms 784656 KB Output is correct
30 Correct 839 ms 784888 KB Output is correct
31 Correct 836 ms 784764 KB Output is correct
32 Correct 816 ms 784860 KB Output is correct
33 Correct 810 ms 784860 KB Output is correct
34 Correct 824 ms 784888 KB Output is correct
35 Correct 812 ms 784848 KB Output is correct
36 Correct 838 ms 784772 KB Output is correct
37 Correct 761 ms 784632 KB Output is correct
38 Correct 792 ms 784732 KB Output is correct
39 Correct 807 ms 784760 KB Output is correct
40 Correct 834 ms 784888 KB Output is correct
41 Correct 844 ms 784756 KB Output is correct
42 Correct 834 ms 784848 KB Output is correct
43 Correct 833 ms 784912 KB Output is correct
44 Correct 838 ms 784952 KB Output is correct
45 Correct 837 ms 784732 KB Output is correct
46 Correct 819 ms 784812 KB Output is correct
47 Correct 839 ms 784760 KB Output is correct
48 Correct 2 ms 256 KB Output is correct
49 Correct 849 ms 784760 KB Output is correct
50 Correct 759 ms 784888 KB Output is correct
51 Correct 672 ms 784760 KB Output is correct
52 Correct 688 ms 784760 KB Output is correct
53 Correct 757 ms 784760 KB Output is correct
54 Correct 693 ms 784664 KB Output is correct
55 Correct 693 ms 784760 KB Output is correct
56 Correct 667 ms 784632 KB Output is correct
57 Correct 724 ms 784920 KB Output is correct
58 Correct 817 ms 784800 KB Output is correct
59 Correct 813 ms 784692 KB Output is correct
60 Correct 824 ms 784888 KB Output is correct
61 Correct 757 ms 784700 KB Output is correct
62 Correct 1079 ms 784812 KB Output is correct
63 Correct 919 ms 784888 KB Output is correct
64 Correct 792 ms 784696 KB Output is correct
65 Correct 796 ms 784888 KB Output is correct
66 Correct 1352 ms 784692 KB Output is correct
67 Correct 1170 ms 784912 KB Output is correct
68 Correct 1270 ms 784988 KB Output is correct
69 Correct 831 ms 784820 KB Output is correct
70 Correct 812 ms 784772 KB Output is correct
71 Correct 795 ms 784888 KB Output is correct
72 Correct 892 ms 784760 KB Output is correct
73 Correct 1488 ms 784760 KB Output is correct
74 Correct 1347 ms 784888 KB Output is correct