답안 #958755

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
958755 2024-04-06T14:27:57 Z TAhmed33 Snake Escaping (JOI18_snake_escaping) C++
100 / 100
543 ms 42088 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("popcnt")
const int B = 20;
int dp[1 << B], dq[1 << B], arr[1 << B];
int n, q;
int main () {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> q;
	for (int i = 0; i < (1 << n); i++) {
		char s; cin >> s; int x = s - '0';
		arr[i] = dp[i] = dq[i] = x;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < (1 << n); j++) {
			if (!(j & (1 << i))) dq[j] += dq[j ^ (1 << i)];
		}
		for (int j = (1 << n) - 1; j >= 0; j--) {
			if (j & (1 << i)) dp[j] += dp[j ^ (1 << i)];
		}
	}
	while (q--) {
		string s; cin >> s;
		int a = 0, b = 0, c = 0;
		for (int i = 0; i < n; i++) {
			if (s[i] == '0') {
				a ^= 1 << (n - i - 1);
			} else if (s[i] == '1') {
				b ^= 1 << (n - i - 1);
			} else {
				c ^= 1 << (n - i - 1);
			}
		}
		int ret = 0;
		if (__builtin_popcount(a) <= 6) {
			int m = a;
			while (true) {
				if (__builtin_popcount(m) & 1) {
					ret -= dq[b | m];
				} else {
					ret += dq[b | m];
				}
				if (m == 0) break;
				m = (m - 1) & a;
			}
		} else if (__builtin_popcount(b) <= 6) {
			int m = b;
			while (true) {
				if (__builtin_popcount(m) & 1) {
					ret -= dp[c | m];
				} else {
					ret += dp[c | m];
				}
				if (m == 0) break;
				m = (m - 1) & b;
			} 
		} else {
			int m = c;
			while (true) {
				ret += arr[b ^ m];
				if (m == 0) break;
				m = (m - 1) & c;
			} 
		}
		cout << (ret < 0 ? -ret : ret) << '\n';
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4568 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 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4568 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 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 165 ms 8532 KB Output is correct
12 Correct 201 ms 8020 KB Output is correct
13 Correct 225 ms 7316 KB Output is correct
14 Correct 215 ms 7620 KB Output is correct
15 Correct 182 ms 8520 KB Output is correct
16 Correct 202 ms 7736 KB Output is correct
17 Correct 213 ms 7508 KB Output is correct
18 Correct 139 ms 9696 KB Output is correct
19 Correct 197 ms 6484 KB Output is correct
20 Correct 171 ms 8056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4568 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 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 165 ms 8532 KB Output is correct
12 Correct 201 ms 8020 KB Output is correct
13 Correct 225 ms 7316 KB Output is correct
14 Correct 215 ms 7620 KB Output is correct
15 Correct 182 ms 8520 KB Output is correct
16 Correct 202 ms 7736 KB Output is correct
17 Correct 213 ms 7508 KB Output is correct
18 Correct 139 ms 9696 KB Output is correct
19 Correct 197 ms 6484 KB Output is correct
20 Correct 171 ms 8056 KB Output is correct
21 Correct 202 ms 8548 KB Output is correct
22 Correct 224 ms 8636 KB Output is correct
23 Correct 321 ms 7688 KB Output is correct
24 Correct 251 ms 7584 KB Output is correct
25 Correct 208 ms 9380 KB Output is correct
26 Correct 244 ms 7888 KB Output is correct
27 Correct 255 ms 7888 KB Output is correct
28 Correct 155 ms 10428 KB Output is correct
29 Correct 275 ms 6484 KB Output is correct
30 Correct 205 ms 8532 KB Output is correct
31 Correct 242 ms 8692 KB Output is correct
32 Correct 239 ms 8528 KB Output is correct
33 Correct 208 ms 7452 KB Output is correct
34 Correct 252 ms 7508 KB Output is correct
35 Correct 253 ms 7872 KB Output is correct
36 Correct 139 ms 6412 KB Output is correct
37 Correct 183 ms 8516 KB Output is correct
38 Correct 253 ms 6432 KB Output is correct
39 Correct 239 ms 7768 KB Output is correct
40 Correct 240 ms 7440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4568 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 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 57 ms 12884 KB Output is correct
12 Correct 66 ms 12880 KB Output is correct
13 Correct 62 ms 12880 KB Output is correct
14 Correct 69 ms 12884 KB Output is correct
15 Correct 58 ms 12996 KB Output is correct
16 Correct 66 ms 12764 KB Output is correct
17 Correct 65 ms 12796 KB Output is correct
18 Correct 57 ms 13092 KB Output is correct
19 Correct 56 ms 12764 KB Output is correct
20 Correct 59 ms 12880 KB Output is correct
21 Correct 60 ms 13000 KB Output is correct
22 Correct 66 ms 12880 KB Output is correct
23 Correct 61 ms 12760 KB Output is correct
24 Correct 67 ms 12884 KB Output is correct
25 Correct 70 ms 12768 KB Output is correct
26 Correct 53 ms 12636 KB Output is correct
27 Correct 56 ms 12756 KB Output is correct
28 Correct 60 ms 12636 KB Output is correct
29 Correct 61 ms 12764 KB Output is correct
30 Correct 66 ms 12880 KB Output is correct
31 Correct 63 ms 12744 KB Output is correct
32 Correct 69 ms 12752 KB Output is correct
33 Correct 64 ms 12748 KB Output is correct
34 Correct 58 ms 12632 KB Output is correct
35 Correct 65 ms 12880 KB Output is correct
36 Correct 75 ms 13140 KB Output is correct
37 Correct 62 ms 12748 KB Output is correct
38 Correct 64 ms 12880 KB Output is correct
39 Correct 66 ms 12744 KB Output is correct
40 Correct 68 ms 12888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4568 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 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 165 ms 8532 KB Output is correct
12 Correct 201 ms 8020 KB Output is correct
13 Correct 225 ms 7316 KB Output is correct
14 Correct 215 ms 7620 KB Output is correct
15 Correct 182 ms 8520 KB Output is correct
16 Correct 202 ms 7736 KB Output is correct
17 Correct 213 ms 7508 KB Output is correct
18 Correct 139 ms 9696 KB Output is correct
19 Correct 197 ms 6484 KB Output is correct
20 Correct 171 ms 8056 KB Output is correct
21 Correct 202 ms 8548 KB Output is correct
22 Correct 224 ms 8636 KB Output is correct
23 Correct 321 ms 7688 KB Output is correct
24 Correct 251 ms 7584 KB Output is correct
25 Correct 208 ms 9380 KB Output is correct
26 Correct 244 ms 7888 KB Output is correct
27 Correct 255 ms 7888 KB Output is correct
28 Correct 155 ms 10428 KB Output is correct
29 Correct 275 ms 6484 KB Output is correct
30 Correct 205 ms 8532 KB Output is correct
31 Correct 242 ms 8692 KB Output is correct
32 Correct 239 ms 8528 KB Output is correct
33 Correct 208 ms 7452 KB Output is correct
34 Correct 252 ms 7508 KB Output is correct
35 Correct 253 ms 7872 KB Output is correct
36 Correct 139 ms 6412 KB Output is correct
37 Correct 183 ms 8516 KB Output is correct
38 Correct 253 ms 6432 KB Output is correct
39 Correct 239 ms 7768 KB Output is correct
40 Correct 240 ms 7440 KB Output is correct
41 Correct 57 ms 12884 KB Output is correct
42 Correct 66 ms 12880 KB Output is correct
43 Correct 62 ms 12880 KB Output is correct
44 Correct 69 ms 12884 KB Output is correct
45 Correct 58 ms 12996 KB Output is correct
46 Correct 66 ms 12764 KB Output is correct
47 Correct 65 ms 12796 KB Output is correct
48 Correct 57 ms 13092 KB Output is correct
49 Correct 56 ms 12764 KB Output is correct
50 Correct 59 ms 12880 KB Output is correct
51 Correct 60 ms 13000 KB Output is correct
52 Correct 66 ms 12880 KB Output is correct
53 Correct 61 ms 12760 KB Output is correct
54 Correct 67 ms 12884 KB Output is correct
55 Correct 70 ms 12768 KB Output is correct
56 Correct 53 ms 12636 KB Output is correct
57 Correct 56 ms 12756 KB Output is correct
58 Correct 60 ms 12636 KB Output is correct
59 Correct 61 ms 12764 KB Output is correct
60 Correct 66 ms 12880 KB Output is correct
61 Correct 63 ms 12744 KB Output is correct
62 Correct 69 ms 12752 KB Output is correct
63 Correct 64 ms 12748 KB Output is correct
64 Correct 58 ms 12632 KB Output is correct
65 Correct 65 ms 12880 KB Output is correct
66 Correct 75 ms 13140 KB Output is correct
67 Correct 62 ms 12748 KB Output is correct
68 Correct 64 ms 12880 KB Output is correct
69 Correct 66 ms 12744 KB Output is correct
70 Correct 68 ms 12888 KB Output is correct
71 Correct 306 ms 17620 KB Output is correct
72 Correct 368 ms 18032 KB Output is correct
73 Correct 420 ms 16260 KB Output is correct
74 Correct 463 ms 16720 KB Output is correct
75 Correct 336 ms 18564 KB Output is correct
76 Correct 460 ms 16980 KB Output is correct
77 Correct 466 ms 38352 KB Output is correct
78 Correct 264 ms 42088 KB Output is correct
79 Correct 308 ms 36044 KB Output is correct
80 Correct 321 ms 39348 KB Output is correct
81 Correct 400 ms 39264 KB Output is correct
82 Correct 463 ms 38340 KB Output is correct
83 Correct 344 ms 37448 KB Output is correct
84 Correct 543 ms 38276 KB Output is correct
85 Correct 522 ms 38592 KB Output is correct
86 Correct 247 ms 36176 KB Output is correct
87 Correct 310 ms 39116 KB Output is correct
88 Correct 342 ms 36788 KB Output is correct
89 Correct 400 ms 37716 KB Output is correct
90 Correct 434 ms 38084 KB Output is correct
91 Correct 365 ms 37512 KB Output is correct
92 Correct 538 ms 38480 KB Output is correct
93 Correct 511 ms 38480 KB Output is correct
94 Correct 217 ms 36052 KB Output is correct
95 Correct 435 ms 38256 KB Output is correct
96 Correct 432 ms 38228 KB Output is correct
97 Correct 448 ms 38360 KB Output is correct
98 Correct 441 ms 38280 KB Output is correct
99 Correct 429 ms 38480 KB Output is correct
100 Correct 413 ms 38228 KB Output is correct