답안 #248516

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
248516 2020-07-12T15:05:46 Z Berted Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1314 ms 47632 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] = {};
int cr[8], sz = 0;

/*
	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 < sz) {return qry1(i + 1, val) + qry1(i + 1, val ^ cr[i]);}
	else {return s[val] - '0';}
}

int qry2(int t, int i, int val, bool c)
{
	if (i < sz) {return qry2(t, i + 1, val, c ^ t) + qry2(t, i + 1, val ^ cr[i], c ^ (!t));}
	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) 
		{
			int val = 0;
			for (int j = 0; j < n; j++) 
			{
				if (qry[j] == '0') cr[sz++] = 1 << (n - 1 - j);
				else if (qry[j] == '1') val ^= 1 << (n - 1 - j);
			}
			cout << qry2(0, 0, val, 0) << "\n";
		}
		else if (mn == b) 
		{
			int val = 0;
			for (int j = 0; j < n; j++) 
			{
				if (qry[j] == '1') cr[sz++] = 1 << (n - 1 - j);
				else if (qry[j] == '?') val ^= 1 << (n - 1 - j);
			}
			cout << qry2(1, 0, val, 0) << "\n";
		}
		else 
		{
			int val = 0;
			for (int j = 0; j < n; j++) 
			{
				if (qry[j] == '?') cr[sz++] = 1 << (n - 1 - j);
				else if (qry[j] == '1') val ^= 1 << (n - 1 - j);
			}
			cout << qry1(0, val) << "\n";
		}
		sz = 0;
	}
	return 0;
}
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 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 261 ms 9044 KB Output is correct
12 Correct 263 ms 8764 KB Output is correct
13 Correct 280 ms 8312 KB Output is correct
14 Correct 297 ms 8056 KB Output is correct
15 Correct 265 ms 9020 KB Output is correct
16 Correct 295 ms 8312 KB Output is correct
17 Correct 296 ms 8184 KB Output is correct
18 Correct 202 ms 10104 KB Output is correct
19 Correct 234 ms 7032 KB Output is correct
20 Correct 255 ms 8956 KB Output is correct
# 결과 실행 시간 메모리 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 261 ms 9044 KB Output is correct
12 Correct 263 ms 8764 KB Output is correct
13 Correct 280 ms 8312 KB Output is correct
14 Correct 297 ms 8056 KB Output is correct
15 Correct 265 ms 9020 KB Output is correct
16 Correct 295 ms 8312 KB Output is correct
17 Correct 296 ms 8184 KB Output is correct
18 Correct 202 ms 10104 KB Output is correct
19 Correct 234 ms 7032 KB Output is correct
20 Correct 255 ms 8956 KB Output is correct
21 Correct 266 ms 8648 KB Output is correct
22 Correct 286 ms 8940 KB Output is correct
23 Correct 323 ms 7800 KB Output is correct
24 Correct 408 ms 7672 KB Output is correct
25 Correct 316 ms 9720 KB Output is correct
26 Correct 368 ms 8056 KB Output is correct
27 Correct 366 ms 8004 KB Output is correct
28 Correct 232 ms 10672 KB Output is correct
29 Correct 276 ms 6456 KB Output is correct
30 Correct 279 ms 8568 KB Output is correct
31 Correct 326 ms 8184 KB Output is correct
32 Correct 378 ms 7876 KB Output is correct
33 Correct 279 ms 6648 KB Output is correct
34 Correct 366 ms 6904 KB Output is correct
35 Correct 360 ms 7204 KB Output is correct
36 Correct 209 ms 5496 KB Output is correct
37 Correct 253 ms 7544 KB Output is correct
38 Correct 275 ms 5624 KB Output is correct
39 Correct 328 ms 6776 KB Output is correct
40 Correct 385 ms 6392 KB Output is correct
# 결과 실행 시간 메모리 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 73 ms 20248 KB Output is correct
13 Correct 85 ms 20120 KB Output is correct
14 Correct 118 ms 20120 KB Output is correct
15 Correct 75 ms 20248 KB Output is correct
16 Correct 125 ms 20248 KB Output is correct
17 Correct 103 ms 20156 KB Output is correct
18 Correct 63 ms 20376 KB Output is correct
19 Correct 69 ms 20120 KB Output is correct
20 Correct 74 ms 20248 KB Output is correct
21 Correct 87 ms 20252 KB Output is correct
22 Correct 107 ms 20120 KB Output is correct
23 Correct 75 ms 20124 KB Output is correct
24 Correct 114 ms 20120 KB Output is correct
25 Correct 106 ms 20376 KB Output is correct
26 Correct 63 ms 19992 KB Output is correct
27 Correct 73 ms 20252 KB Output is correct
28 Correct 69 ms 20116 KB Output is correct
29 Correct 79 ms 20120 KB Output is correct
30 Correct 91 ms 20248 KB Output is correct
31 Correct 69 ms 20080 KB Output is correct
32 Correct 118 ms 20220 KB Output is correct
33 Correct 95 ms 20120 KB Output is correct
34 Correct 62 ms 20120 KB Output is correct
35 Correct 95 ms 20124 KB Output is correct
36 Correct 86 ms 20120 KB Output is correct
37 Correct 86 ms 20120 KB Output is correct
38 Correct 97 ms 20120 KB Output is correct
39 Correct 89 ms 20124 KB Output is correct
40 Correct 88 ms 20152 KB Output is correct
# 결과 실행 시간 메모리 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 261 ms 9044 KB Output is correct
12 Correct 263 ms 8764 KB Output is correct
13 Correct 280 ms 8312 KB Output is correct
14 Correct 297 ms 8056 KB Output is correct
15 Correct 265 ms 9020 KB Output is correct
16 Correct 295 ms 8312 KB Output is correct
17 Correct 296 ms 8184 KB Output is correct
18 Correct 202 ms 10104 KB Output is correct
19 Correct 234 ms 7032 KB Output is correct
20 Correct 255 ms 8956 KB Output is correct
21 Correct 266 ms 8648 KB Output is correct
22 Correct 286 ms 8940 KB Output is correct
23 Correct 323 ms 7800 KB Output is correct
24 Correct 408 ms 7672 KB Output is correct
25 Correct 316 ms 9720 KB Output is correct
26 Correct 368 ms 8056 KB Output is correct
27 Correct 366 ms 8004 KB Output is correct
28 Correct 232 ms 10672 KB Output is correct
29 Correct 276 ms 6456 KB Output is correct
30 Correct 279 ms 8568 KB Output is correct
31 Correct 326 ms 8184 KB Output is correct
32 Correct 378 ms 7876 KB Output is correct
33 Correct 279 ms 6648 KB Output is correct
34 Correct 366 ms 6904 KB Output is correct
35 Correct 360 ms 7204 KB Output is correct
36 Correct 209 ms 5496 KB Output is correct
37 Correct 253 ms 7544 KB Output is correct
38 Correct 275 ms 5624 KB Output is correct
39 Correct 328 ms 6776 KB Output is correct
40 Correct 385 ms 6392 KB Output is correct
41 Correct 74 ms 20248 KB Output is correct
42 Correct 73 ms 20248 KB Output is correct
43 Correct 85 ms 20120 KB Output is correct
44 Correct 118 ms 20120 KB Output is correct
45 Correct 75 ms 20248 KB Output is correct
46 Correct 125 ms 20248 KB Output is correct
47 Correct 103 ms 20156 KB Output is correct
48 Correct 63 ms 20376 KB Output is correct
49 Correct 69 ms 20120 KB Output is correct
50 Correct 74 ms 20248 KB Output is correct
51 Correct 87 ms 20252 KB Output is correct
52 Correct 107 ms 20120 KB Output is correct
53 Correct 75 ms 20124 KB Output is correct
54 Correct 114 ms 20120 KB Output is correct
55 Correct 106 ms 20376 KB Output is correct
56 Correct 63 ms 19992 KB Output is correct
57 Correct 73 ms 20252 KB Output is correct
58 Correct 69 ms 20116 KB Output is correct
59 Correct 79 ms 20120 KB Output is correct
60 Correct 91 ms 20248 KB Output is correct
61 Correct 69 ms 20080 KB Output is correct
62 Correct 118 ms 20220 KB Output is correct
63 Correct 95 ms 20120 KB Output is correct
64 Correct 62 ms 20120 KB Output is correct
65 Correct 95 ms 20124 KB Output is correct
66 Correct 86 ms 20120 KB Output is correct
67 Correct 86 ms 20120 KB Output is correct
68 Correct 97 ms 20120 KB Output is correct
69 Correct 89 ms 20124 KB Output is correct
70 Correct 88 ms 20152 KB Output is correct
71 Correct 391 ms 23148 KB Output is correct
72 Correct 461 ms 23320 KB Output is correct
73 Correct 636 ms 21784 KB Output is correct
74 Correct 1025 ms 27236 KB Output is correct
75 Correct 473 ms 45540 KB Output is correct
76 Correct 1246 ms 43916 KB Output is correct
77 Correct 934 ms 43796 KB Output is correct
78 Correct 339 ms 47632 KB Output is correct
79 Correct 388 ms 41496 KB Output is correct
80 Correct 472 ms 44656 KB Output is correct
81 Correct 674 ms 44572 KB Output is correct
82 Correct 1314 ms 43580 KB Output is correct
83 Correct 473 ms 42648 KB Output is correct
84 Correct 1063 ms 43516 KB Output is correct
85 Correct 932 ms 43640 KB Output is correct
86 Correct 276 ms 41472 KB Output is correct
87 Correct 415 ms 44552 KB Output is correct
88 Correct 401 ms 41500 KB Output is correct
89 Correct 664 ms 43404 KB Output is correct
90 Correct 706 ms 43496 KB Output is correct
91 Correct 457 ms 42776 KB Output is correct
92 Correct 1152 ms 43968 KB Output is correct
93 Correct 1069 ms 43796 KB Output is correct
94 Correct 324 ms 41576 KB Output is correct
95 Correct 763 ms 43656 KB Output is correct
96 Correct 781 ms 43668 KB Output is correct
97 Correct 754 ms 43668 KB Output is correct
98 Correct 770 ms 43684 KB Output is correct
99 Correct 777 ms 43612 KB Output is correct
100 Correct 892 ms 43716 KB Output is correct