Submission #248508

# Submission time Handle Problem Language Result Execution time Memory
248508 2020-07-12T14:43:08 Z Berted Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 44708 KB
#include <iostream>
#include <utility>
#include <tuple>
using namespace std;

const int SZ = 1 << 20;

int n, q, k;
string s;
string qry;

int dp[2][2][SZ] = {};

/*
	We can decompose a query into three types,
	min(0, 1, ?) == 0, min(0, 1, ?) == 1, min(0, 1, ?) == ?
	Then, we could find an algorithm that runs in 2^k for each possible type
	Since k is at most 6, 2^k is at most 64, therefore 2^k * Q still works.
*/

int qry1(int i, int val)
{
	if (i < n)
	{
		if (qry[i] == '?') return qry1(i + 1, val << 1) + qry1(i + 1, (val << 1) ^ 1);
		else return qry1(i + 1, (val << 1) ^ (qry[i] - '0'));
	}
	else {return s[val] - '0';}
}

int qry2(int t, int i, int val, bool c)
{
	if (i < n)
	{
		if (qry[i] == '?') return qry2(t, i + 1, (val << 1) ^ t, c);
		else if (qry[i] - '0' == t) return qry2(t, i + 1, (val << 1), c ^ t) + qry2(t, i + 1, (val << 1) ^ 1, c ^ (!t));
		else return qry2(t, i + 1, (val << 1) ^ (qry[i] - '0'), c);
	}
	else 
	{
		if (c) return -dp[t][n & 1][val];
		else return dp[t][n & 1][val];
	}
}

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> q;
	cin >> s;
	k = s.size();
	for (int i = 0; i < k; i++) {dp[0][0][i] = dp[1][0][i] = s[i] - '0';}
	for (int i = 1; i <= n; i++)
	{
		int cri = i & 1, pv = !cri, msk = 1 << (i - 1);
		for (int j = 0; j < k; j++)
		{
			for (int k = 0; k < 2; k++)
			{
				dp[k][cri][j] = dp[k][pv][j];
				if (((j & msk) > 0) ^ !k) dp[k][cri][j] += dp[k][pv][j ^ msk];
			}
		}
	}
	for (int i = 0; i < q; i++)
	{
		cin >> qry;
		int a = 0, b = 0, c = 0, mn;
		for (int j = 0; j < n; j++)
		{
			a += (qry[j] == '0');
			b += (qry[j] == '1');
			c += (qry[j] == '?');
		}
		mn = min(a, min(b, c));
		if (mn == a) {cout << qry2(0, 0, 0, 0) << "\n";}
		else if (mn == b) {cout << qry2(1, 0, 0, 0) << "\n";}
		else {cout << qry1(0, 0) << "\n";}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 255 ms 15352 KB Output is correct
12 Correct 265 ms 14968 KB Output is correct
13 Correct 294 ms 14072 KB Output is correct
14 Correct 479 ms 14200 KB Output is correct
15 Correct 332 ms 15224 KB Output is correct
16 Correct 421 ms 14328 KB Output is correct
17 Correct 366 ms 14328 KB Output is correct
18 Correct 231 ms 16376 KB Output is correct
19 Correct 190 ms 13176 KB Output is correct
20 Correct 276 ms 14920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 255 ms 15352 KB Output is correct
12 Correct 265 ms 14968 KB Output is correct
13 Correct 294 ms 14072 KB Output is correct
14 Correct 479 ms 14200 KB Output is correct
15 Correct 332 ms 15224 KB Output is correct
16 Correct 421 ms 14328 KB Output is correct
17 Correct 366 ms 14328 KB Output is correct
18 Correct 231 ms 16376 KB Output is correct
19 Correct 190 ms 13176 KB Output is correct
20 Correct 276 ms 14920 KB Output is correct
21 Correct 295 ms 18296 KB Output is correct
22 Correct 298 ms 18424 KB Output is correct
23 Correct 424 ms 17668 KB Output is correct
24 Correct 785 ms 17300 KB Output is correct
25 Correct 424 ms 19364 KB Output is correct
26 Correct 632 ms 17828 KB Output is correct
27 Correct 539 ms 17652 KB Output is correct
28 Correct 279 ms 20244 KB Output is correct
29 Correct 212 ms 16236 KB Output is correct
30 Correct 333 ms 18424 KB Output is correct
31 Correct 543 ms 18192 KB Output is correct
32 Correct 820 ms 18296 KB Output is correct
33 Correct 327 ms 17192 KB Output is correct
34 Correct 668 ms 17244 KB Output is correct
35 Correct 548 ms 17736 KB Output is correct
36 Correct 248 ms 16248 KB Output is correct
37 Correct 281 ms 18204 KB Output is correct
38 Correct 215 ms 16376 KB Output is correct
39 Correct 435 ms 17400 KB Output is correct
40 Correct 788 ms 17208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 74 ms 20248 KB Output is correct
12 Correct 78 ms 20248 KB Output is correct
13 Correct 104 ms 20120 KB Output is correct
14 Correct 195 ms 20376 KB Output is correct
15 Correct 88 ms 20248 KB Output is correct
16 Correct 173 ms 20124 KB Output is correct
17 Correct 142 ms 20140 KB Output is correct
18 Correct 70 ms 20376 KB Output is correct
19 Correct 66 ms 20120 KB Output is correct
20 Correct 76 ms 20248 KB Output is correct
21 Correct 118 ms 20248 KB Output is correct
22 Correct 177 ms 20120 KB Output is correct
23 Correct 81 ms 20120 KB Output is correct
24 Correct 154 ms 20120 KB Output is correct
25 Correct 134 ms 20124 KB Output is correct
26 Correct 67 ms 20120 KB Output is correct
27 Correct 73 ms 20248 KB Output is correct
28 Correct 66 ms 20120 KB Output is correct
29 Correct 105 ms 20128 KB Output is correct
30 Correct 117 ms 20124 KB Output is correct
31 Correct 85 ms 20120 KB Output is correct
32 Correct 178 ms 20268 KB Output is correct
33 Correct 154 ms 20120 KB Output is correct
34 Correct 69 ms 20120 KB Output is correct
35 Correct 115 ms 20120 KB Output is correct
36 Correct 112 ms 20116 KB Output is correct
37 Correct 115 ms 20120 KB Output is correct
38 Correct 116 ms 20120 KB Output is correct
39 Correct 126 ms 20248 KB Output is correct
40 Correct 119 ms 20120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 255 ms 15352 KB Output is correct
12 Correct 265 ms 14968 KB Output is correct
13 Correct 294 ms 14072 KB Output is correct
14 Correct 479 ms 14200 KB Output is correct
15 Correct 332 ms 15224 KB Output is correct
16 Correct 421 ms 14328 KB Output is correct
17 Correct 366 ms 14328 KB Output is correct
18 Correct 231 ms 16376 KB Output is correct
19 Correct 190 ms 13176 KB Output is correct
20 Correct 276 ms 14920 KB Output is correct
21 Correct 295 ms 18296 KB Output is correct
22 Correct 298 ms 18424 KB Output is correct
23 Correct 424 ms 17668 KB Output is correct
24 Correct 785 ms 17300 KB Output is correct
25 Correct 424 ms 19364 KB Output is correct
26 Correct 632 ms 17828 KB Output is correct
27 Correct 539 ms 17652 KB Output is correct
28 Correct 279 ms 20244 KB Output is correct
29 Correct 212 ms 16236 KB Output is correct
30 Correct 333 ms 18424 KB Output is correct
31 Correct 543 ms 18192 KB Output is correct
32 Correct 820 ms 18296 KB Output is correct
33 Correct 327 ms 17192 KB Output is correct
34 Correct 668 ms 17244 KB Output is correct
35 Correct 548 ms 17736 KB Output is correct
36 Correct 248 ms 16248 KB Output is correct
37 Correct 281 ms 18204 KB Output is correct
38 Correct 215 ms 16376 KB Output is correct
39 Correct 435 ms 17400 KB Output is correct
40 Correct 788 ms 17208 KB Output is correct
41 Correct 74 ms 20248 KB Output is correct
42 Correct 78 ms 20248 KB Output is correct
43 Correct 104 ms 20120 KB Output is correct
44 Correct 195 ms 20376 KB Output is correct
45 Correct 88 ms 20248 KB Output is correct
46 Correct 173 ms 20124 KB Output is correct
47 Correct 142 ms 20140 KB Output is correct
48 Correct 70 ms 20376 KB Output is correct
49 Correct 66 ms 20120 KB Output is correct
50 Correct 76 ms 20248 KB Output is correct
51 Correct 118 ms 20248 KB Output is correct
52 Correct 177 ms 20120 KB Output is correct
53 Correct 81 ms 20120 KB Output is correct
54 Correct 154 ms 20120 KB Output is correct
55 Correct 134 ms 20124 KB Output is correct
56 Correct 67 ms 20120 KB Output is correct
57 Correct 73 ms 20248 KB Output is correct
58 Correct 66 ms 20120 KB Output is correct
59 Correct 105 ms 20128 KB Output is correct
60 Correct 117 ms 20124 KB Output is correct
61 Correct 85 ms 20120 KB Output is correct
62 Correct 178 ms 20268 KB Output is correct
63 Correct 154 ms 20120 KB Output is correct
64 Correct 69 ms 20120 KB Output is correct
65 Correct 115 ms 20120 KB Output is correct
66 Correct 112 ms 20116 KB Output is correct
67 Correct 115 ms 20120 KB Output is correct
68 Correct 116 ms 20120 KB Output is correct
69 Correct 126 ms 20248 KB Output is correct
70 Correct 119 ms 20120 KB Output is correct
71 Correct 463 ms 44568 KB Output is correct
72 Correct 503 ms 44708 KB Output is correct
73 Correct 1225 ms 43276 KB Output is correct
74 Execution timed out 2029 ms 37832 KB Time limit exceeded
75 Halted 0 ms 0 KB -