Submission #940768

# Submission time Handle Problem Language Result Execution time Memory
940768 2024-03-07T15:32:37 Z lorenzoferrari Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
750 ms 42212 KB
#include "bits/stdc++.h"
using namespace std;
using LL = long long;

static int constexpr L = 20;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int l; cin >> l;
	int q; cin >> q;
	auto neg = [&](int mask) -> int {
		return ~mask & ((1 << l) - 1);
	};
	static int s[1 << L]{};
	for (int i = 0; i < (1 << l); ++i) {
		char c; cin >> c;
		s[i] = c - '0';
	}

	static int sos[2][1 << L]{};
	for (int i = 0; i < (1 << l); ++i) {
		sos[0][i] += s[i];
		sos[1][neg(i)] += s[i];
	}
	for (int i = 0; i < l; ++i) {
		for (int mask = 0; mask < (1 << l); ++mask) {
			if (mask & (1 << i)) {
				sos[0][mask] += sos[0][mask ^ (1 << i)];
			}
		}
	}
	for (int i = 0; i < l; ++i) {
		for (int mask = 0; mask < (1 << l); ++mask) {
			if (mask & (1 << i)) {
				sos[1][mask] += sos[1][mask ^ (1 << i)];
			}
		}
	}

	for (int i = 0; i < q; ++i) {
		string t; cin >> t;
		int zeros = 0, ones = 0, marks = 0;
		for (int j = 0; j < l; ++j) {
			if (t[j] == '0') zeros|= (1 << (l - 1 - j));
			if (t[j] == '1') ones |= (1 << (l - 1 - j));
			if (t[j] == '?') marks|= (1 << (l - 1 - j));
		}
		int c0 = __builtin_popcount(zeros);
		int c1 = __builtin_popcount(ones);
		int cm = __builtin_popcount(marks);
		int ans = 0;
		if (cm <= c0 && cm <= c1) {
			ans = s[ones];
			for (int mask = marks; mask > 0; mask = (mask - 1) & marks) {
				ans += s[ones | mask];
			}
		} else if (c1 <= c0 && c1 <= cm) {
			ans = sos[0][ones | marks];
			for (int diff = ones; diff > 0; diff = (diff - 1) & ones) {
				if (__builtin_popcount(diff) & 1) {
					ans -= sos[0][(ones ^ diff) | marks];
				} else {
					ans += sos[0][(ones ^ diff) | marks];
				}
			}
		} else {
			ans = sos[1][zeros | marks];
			for (int diff = zeros; diff > 0; diff = (diff - 1) & zeros) {
				if (__builtin_popcount(diff) & 1) {
					ans -= sos[1][(zeros ^ diff) | marks];
				} else {
					ans += sos[1][(zeros ^ diff) | marks];
				}
			}
		}
		cout << ans << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 171 ms 19280 KB Output is correct
12 Correct 179 ms 18924 KB Output is correct
13 Correct 201 ms 18000 KB Output is correct
14 Correct 197 ms 18148 KB Output is correct
15 Correct 183 ms 19260 KB Output is correct
16 Correct 215 ms 18832 KB Output is correct
17 Correct 216 ms 18232 KB Output is correct
18 Correct 145 ms 20152 KB Output is correct
19 Correct 161 ms 17236 KB Output is correct
20 Correct 212 ms 18772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 171 ms 19280 KB Output is correct
12 Correct 179 ms 18924 KB Output is correct
13 Correct 201 ms 18000 KB Output is correct
14 Correct 197 ms 18148 KB Output is correct
15 Correct 183 ms 19260 KB Output is correct
16 Correct 215 ms 18832 KB Output is correct
17 Correct 216 ms 18232 KB Output is correct
18 Correct 145 ms 20152 KB Output is correct
19 Correct 161 ms 17236 KB Output is correct
20 Correct 212 ms 18772 KB Output is correct
21 Correct 190 ms 22196 KB Output is correct
22 Correct 203 ms 22336 KB Output is correct
23 Correct 264 ms 21360 KB Output is correct
24 Correct 221 ms 21080 KB Output is correct
25 Correct 210 ms 23440 KB Output is correct
26 Correct 243 ms 21784 KB Output is correct
27 Correct 249 ms 21624 KB Output is correct
28 Correct 154 ms 24216 KB Output is correct
29 Correct 184 ms 20496 KB Output is correct
30 Correct 201 ms 22352 KB Output is correct
31 Correct 236 ms 22224 KB Output is correct
32 Correct 274 ms 22232 KB Output is correct
33 Correct 245 ms 21416 KB Output is correct
34 Correct 241 ms 21076 KB Output is correct
35 Correct 250 ms 21844 KB Output is correct
36 Correct 146 ms 20052 KB Output is correct
37 Correct 189 ms 22236 KB Output is correct
38 Correct 198 ms 20224 KB Output is correct
39 Correct 227 ms 21396 KB Output is correct
40 Correct 227 ms 21052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 59 ms 14928 KB Output is correct
12 Correct 55 ms 14928 KB Output is correct
13 Correct 59 ms 14780 KB Output is correct
14 Correct 69 ms 14928 KB Output is correct
15 Correct 56 ms 14932 KB Output is correct
16 Correct 69 ms 14780 KB Output is correct
17 Correct 76 ms 14908 KB Output is correct
18 Correct 59 ms 15184 KB Output is correct
19 Correct 56 ms 14676 KB Output is correct
20 Correct 55 ms 15044 KB Output is correct
21 Correct 62 ms 15036 KB Output is correct
22 Correct 69 ms 14836 KB Output is correct
23 Correct 55 ms 14928 KB Output is correct
24 Correct 64 ms 14780 KB Output is correct
25 Correct 63 ms 14936 KB Output is correct
26 Correct 52 ms 14788 KB Output is correct
27 Correct 56 ms 15036 KB Output is correct
28 Correct 55 ms 14776 KB Output is correct
29 Correct 59 ms 14908 KB Output is correct
30 Correct 61 ms 14932 KB Output is correct
31 Correct 61 ms 14928 KB Output is correct
32 Correct 71 ms 14836 KB Output is correct
33 Correct 65 ms 14788 KB Output is correct
34 Correct 53 ms 14672 KB Output is correct
35 Correct 66 ms 14888 KB Output is correct
36 Correct 66 ms 14932 KB Output is correct
37 Correct 72 ms 14776 KB Output is correct
38 Correct 73 ms 14892 KB Output is correct
39 Correct 64 ms 14780 KB Output is correct
40 Correct 66 ms 14800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 171 ms 19280 KB Output is correct
12 Correct 179 ms 18924 KB Output is correct
13 Correct 201 ms 18000 KB Output is correct
14 Correct 197 ms 18148 KB Output is correct
15 Correct 183 ms 19260 KB Output is correct
16 Correct 215 ms 18832 KB Output is correct
17 Correct 216 ms 18232 KB Output is correct
18 Correct 145 ms 20152 KB Output is correct
19 Correct 161 ms 17236 KB Output is correct
20 Correct 212 ms 18772 KB Output is correct
21 Correct 190 ms 22196 KB Output is correct
22 Correct 203 ms 22336 KB Output is correct
23 Correct 264 ms 21360 KB Output is correct
24 Correct 221 ms 21080 KB Output is correct
25 Correct 210 ms 23440 KB Output is correct
26 Correct 243 ms 21784 KB Output is correct
27 Correct 249 ms 21624 KB Output is correct
28 Correct 154 ms 24216 KB Output is correct
29 Correct 184 ms 20496 KB Output is correct
30 Correct 201 ms 22352 KB Output is correct
31 Correct 236 ms 22224 KB Output is correct
32 Correct 274 ms 22232 KB Output is correct
33 Correct 245 ms 21416 KB Output is correct
34 Correct 241 ms 21076 KB Output is correct
35 Correct 250 ms 21844 KB Output is correct
36 Correct 146 ms 20052 KB Output is correct
37 Correct 189 ms 22236 KB Output is correct
38 Correct 198 ms 20224 KB Output is correct
39 Correct 227 ms 21396 KB Output is correct
40 Correct 227 ms 21052 KB Output is correct
41 Correct 59 ms 14928 KB Output is correct
42 Correct 55 ms 14928 KB Output is correct
43 Correct 59 ms 14780 KB Output is correct
44 Correct 69 ms 14928 KB Output is correct
45 Correct 56 ms 14932 KB Output is correct
46 Correct 69 ms 14780 KB Output is correct
47 Correct 76 ms 14908 KB Output is correct
48 Correct 59 ms 15184 KB Output is correct
49 Correct 56 ms 14676 KB Output is correct
50 Correct 55 ms 15044 KB Output is correct
51 Correct 62 ms 15036 KB Output is correct
52 Correct 69 ms 14836 KB Output is correct
53 Correct 55 ms 14928 KB Output is correct
54 Correct 64 ms 14780 KB Output is correct
55 Correct 63 ms 14936 KB Output is correct
56 Correct 52 ms 14788 KB Output is correct
57 Correct 56 ms 15036 KB Output is correct
58 Correct 55 ms 14776 KB Output is correct
59 Correct 59 ms 14908 KB Output is correct
60 Correct 61 ms 14932 KB Output is correct
61 Correct 61 ms 14928 KB Output is correct
62 Correct 71 ms 14836 KB Output is correct
63 Correct 65 ms 14788 KB Output is correct
64 Correct 53 ms 14672 KB Output is correct
65 Correct 66 ms 14888 KB Output is correct
66 Correct 66 ms 14932 KB Output is correct
67 Correct 72 ms 14776 KB Output is correct
68 Correct 73 ms 14892 KB Output is correct
69 Correct 64 ms 14780 KB Output is correct
70 Correct 66 ms 14800 KB Output is correct
71 Correct 332 ms 39376 KB Output is correct
72 Correct 354 ms 39348 KB Output is correct
73 Correct 405 ms 37816 KB Output is correct
74 Correct 594 ms 38228 KB Output is correct
75 Correct 345 ms 40272 KB Output is correct
76 Correct 623 ms 38856 KB Output is correct
77 Correct 518 ms 38328 KB Output is correct
78 Correct 240 ms 42212 KB Output is correct
79 Correct 313 ms 36296 KB Output is correct
80 Correct 312 ms 39268 KB Output is correct
81 Correct 389 ms 39248 KB Output is correct
82 Correct 583 ms 38116 KB Output is correct
83 Correct 321 ms 37832 KB Output is correct
84 Correct 456 ms 38204 KB Output is correct
85 Correct 513 ms 38444 KB Output is correct
86 Correct 248 ms 36340 KB Output is correct
87 Correct 308 ms 39248 KB Output is correct
88 Correct 310 ms 36096 KB Output is correct
89 Correct 400 ms 37924 KB Output is correct
90 Correct 439 ms 38272 KB Output is correct
91 Correct 357 ms 37468 KB Output is correct
92 Correct 750 ms 38396 KB Output is correct
93 Correct 687 ms 38408 KB Output is correct
94 Correct 248 ms 36144 KB Output is correct
95 Correct 535 ms 38152 KB Output is correct
96 Correct 576 ms 38136 KB Output is correct
97 Correct 529 ms 38296 KB Output is correct
98 Correct 602 ms 38304 KB Output is correct
99 Correct 573 ms 38256 KB Output is correct
100 Correct 537 ms 38304 KB Output is correct