Submission #535668

# Submission time Handle Problem Language Result Execution time Memory
535668 2022-03-10T21:37:09 Z iliaM Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 21780 KB
#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
#pragma GCC target("avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define cerr cerr << "DEBUG "

constexpr int N = (1 << 20) + 10;

int a[N], f[N], g[N], h[N];
int n, m;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> n >> m;
	for (int i = 0; i < (1 << n); ++i) {
		char c;
		cin >> c;
		a[i] = c - '0';
	}
	
	int comp = (1 << n) - 1;
	for (int i = 0; i < (1 << n); ++i) {
		f[i] = g[i ^ comp] = a[i];
	}
	for (int i = 0; i < n; ++i) {
		for (int mask = 0; mask < (1 << n); ++mask) {
			if ((mask >> i) & 1) {
				f[mask] += f[mask ^ (1 << i)];
				g[mask] += g[mask ^ (1 << i)];
			}
		}
	}
	for (int i = 0; i < (1 << n); ++i) {
		h[i] = g[i ^ comp];
	}
	
	while (m--) {
		string s;
		cin >> s;
		int cnt0 = 0, cnt1 = 0, num = 0, numm = 0;
		reverse(s.begin(), s.end());
		for (int i = 0; i < n; ++i) {
			cnt0 += s[i] == '0';
			cnt1 += s[i] == '1';
			if (s[i] == '1') {
				num += (1 << i);
				numm += (1 << i);
			}
			if (s[i] == '?') {
				numm += (1 << i);
			}
		}

		int sum = 0;
		if (cnt1 <= 6) {
			for (int mask = 0; mask < (1 << cnt1); ++mask) {
				int num2 = numm;
				for (int c = 0, i = 0; i < n; ++i) {
					if (s[i] == '1') {
						num2 ^= ((mask >> c) & 1) << i;
						++c;
					}
				}
				sum += (__builtin_parity(mask) ? -1 : +1) * f[num2];
			}
		} else if (n - cnt0 - cnt1 <= 6) {
			int cnt = n - cnt0 - cnt1;
			for (int mask = 0; mask < (1 << cnt); ++mask) {
				int num2 = num;
				for (int c = 0, i = 0; i < n; ++i) {
					if (s[i] == '?') {
						num2 ^= ((mask >> c) & 1) << i;
						++c;
					}
				}
				sum += a[num2];
			}
		} else {
			for (int mask = 0; mask < (1 << cnt0); ++mask) {
				int num2 = num;
				for (int c = 0, i = 0; i < n; ++i) {
					if (s[i] == '0') {
						num2 ^= ((mask >> c) & 1) << i;
						++c;
					}
				}
				sum += (__builtin_parity(mask) ? -1 : +1) * h[num2];
			}
		}
		cout << sum << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 679 ms 4436 KB Output is correct
12 Correct 176 ms 4048 KB Output is correct
13 Correct 326 ms 3396 KB Output is correct
14 Correct 311 ms 3252 KB Output is correct
15 Correct 272 ms 4360 KB Output is correct
16 Correct 516 ms 3540 KB Output is correct
17 Correct 466 ms 3404 KB Output is correct
18 Correct 163 ms 5320 KB Output is correct
19 Correct 615 ms 2432 KB Output is correct
20 Correct 669 ms 3960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 679 ms 4436 KB Output is correct
12 Correct 176 ms 4048 KB Output is correct
13 Correct 326 ms 3396 KB Output is correct
14 Correct 311 ms 3252 KB Output is correct
15 Correct 272 ms 4360 KB Output is correct
16 Correct 516 ms 3540 KB Output is correct
17 Correct 466 ms 3404 KB Output is correct
18 Correct 163 ms 5320 KB Output is correct
19 Correct 615 ms 2432 KB Output is correct
20 Correct 669 ms 3960 KB Output is correct
21 Correct 1299 ms 4440 KB Output is correct
22 Correct 177 ms 4636 KB Output is correct
23 Correct 603 ms 3640 KB Output is correct
24 Correct 545 ms 3388 KB Output is correct
25 Correct 392 ms 5432 KB Output is correct
26 Correct 768 ms 3928 KB Output is correct
27 Correct 753 ms 4044 KB Output is correct
28 Correct 178 ms 6380 KB Output is correct
29 Correct 231 ms 2352 KB Output is correct
30 Correct 893 ms 4772 KB Output is correct
31 Correct 618 ms 4396 KB Output is correct
32 Correct 550 ms 4400 KB Output is correct
33 Correct 385 ms 3360 KB Output is correct
34 Correct 792 ms 3500 KB Output is correct
35 Correct 735 ms 3840 KB Output is correct
36 Correct 174 ms 2448 KB Output is correct
37 Correct 195 ms 4496 KB Output is correct
38 Correct 639 ms 2424 KB Output is correct
39 Correct 1304 ms 3752 KB Output is correct
40 Correct 880 ms 3432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 55 ms 16972 KB Output is correct
12 Correct 56 ms 16964 KB Output is correct
13 Correct 92 ms 16816 KB Output is correct
14 Correct 147 ms 16912 KB Output is correct
15 Correct 80 ms 16972 KB Output is correct
16 Correct 141 ms 16904 KB Output is correct
17 Correct 143 ms 16844 KB Output is correct
18 Correct 48 ms 17116 KB Output is correct
19 Correct 52 ms 16728 KB Output is correct
20 Correct 67 ms 16972 KB Output is correct
21 Correct 98 ms 16872 KB Output is correct
22 Correct 149 ms 16936 KB Output is correct
23 Correct 74 ms 16844 KB Output is correct
24 Correct 128 ms 16888 KB Output is correct
25 Correct 126 ms 16848 KB Output is correct
26 Correct 48 ms 16728 KB Output is correct
27 Correct 50 ms 16968 KB Output is correct
28 Correct 55 ms 16728 KB Output is correct
29 Correct 110 ms 16800 KB Output is correct
30 Correct 118 ms 16892 KB Output is correct
31 Correct 78 ms 16856 KB Output is correct
32 Correct 142 ms 16924 KB Output is correct
33 Correct 162 ms 16944 KB Output is correct
34 Correct 47 ms 16748 KB Output is correct
35 Correct 95 ms 16804 KB Output is correct
36 Correct 101 ms 16836 KB Output is correct
37 Correct 107 ms 16872 KB Output is correct
38 Correct 114 ms 16884 KB Output is correct
39 Correct 101 ms 16908 KB Output is correct
40 Correct 95 ms 16916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 679 ms 4436 KB Output is correct
12 Correct 176 ms 4048 KB Output is correct
13 Correct 326 ms 3396 KB Output is correct
14 Correct 311 ms 3252 KB Output is correct
15 Correct 272 ms 4360 KB Output is correct
16 Correct 516 ms 3540 KB Output is correct
17 Correct 466 ms 3404 KB Output is correct
18 Correct 163 ms 5320 KB Output is correct
19 Correct 615 ms 2432 KB Output is correct
20 Correct 669 ms 3960 KB Output is correct
21 Correct 1299 ms 4440 KB Output is correct
22 Correct 177 ms 4636 KB Output is correct
23 Correct 603 ms 3640 KB Output is correct
24 Correct 545 ms 3388 KB Output is correct
25 Correct 392 ms 5432 KB Output is correct
26 Correct 768 ms 3928 KB Output is correct
27 Correct 753 ms 4044 KB Output is correct
28 Correct 178 ms 6380 KB Output is correct
29 Correct 231 ms 2352 KB Output is correct
30 Correct 893 ms 4772 KB Output is correct
31 Correct 618 ms 4396 KB Output is correct
32 Correct 550 ms 4400 KB Output is correct
33 Correct 385 ms 3360 KB Output is correct
34 Correct 792 ms 3500 KB Output is correct
35 Correct 735 ms 3840 KB Output is correct
36 Correct 174 ms 2448 KB Output is correct
37 Correct 195 ms 4496 KB Output is correct
38 Correct 639 ms 2424 KB Output is correct
39 Correct 1304 ms 3752 KB Output is correct
40 Correct 880 ms 3432 KB Output is correct
41 Correct 55 ms 16972 KB Output is correct
42 Correct 56 ms 16964 KB Output is correct
43 Correct 92 ms 16816 KB Output is correct
44 Correct 147 ms 16912 KB Output is correct
45 Correct 80 ms 16972 KB Output is correct
46 Correct 141 ms 16904 KB Output is correct
47 Correct 143 ms 16844 KB Output is correct
48 Correct 48 ms 17116 KB Output is correct
49 Correct 52 ms 16728 KB Output is correct
50 Correct 67 ms 16972 KB Output is correct
51 Correct 98 ms 16872 KB Output is correct
52 Correct 149 ms 16936 KB Output is correct
53 Correct 74 ms 16844 KB Output is correct
54 Correct 128 ms 16888 KB Output is correct
55 Correct 126 ms 16848 KB Output is correct
56 Correct 48 ms 16728 KB Output is correct
57 Correct 50 ms 16968 KB Output is correct
58 Correct 55 ms 16728 KB Output is correct
59 Correct 110 ms 16800 KB Output is correct
60 Correct 118 ms 16892 KB Output is correct
61 Correct 78 ms 16856 KB Output is correct
62 Correct 142 ms 16924 KB Output is correct
63 Correct 162 ms 16944 KB Output is correct
64 Correct 47 ms 16748 KB Output is correct
65 Correct 95 ms 16804 KB Output is correct
66 Correct 101 ms 16836 KB Output is correct
67 Correct 107 ms 16872 KB Output is correct
68 Correct 114 ms 16884 KB Output is correct
69 Correct 101 ms 16908 KB Output is correct
70 Correct 95 ms 16916 KB Output is correct
71 Correct 447 ms 21628 KB Output is correct
72 Correct 292 ms 21780 KB Output is correct
73 Correct 1202 ms 20352 KB Output is correct
74 Execution timed out 2066 ms 20596 KB Time limit exceeded
75 Halted 0 ms 0 KB -