답안 #965366

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965366 2024-04-18T11:44:04 Z Trisanu_Das Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
587 ms 43664 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

int main() {
	using namespace std;
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL);
	int L, Q;
	cin >> L >> Q;
	string S;
	cin >> S;
	vector<int> A(1 << L), dp1(1 << L), dp2(1 << L);
	for (int i = 0; i < (1 << L); i++) A[i] = dp1[i] = dp2[i] = S[i] - '0';
	for (int b = 0; b < L; b++) {
		for (int m = 0; m < (1 << L); m++) {
			if (m >> b & 1) dp1[m] += dp1[m ^ (1 << b)];
			else dp2[m] += dp2[m ^ (1 << b)];
		}
	}
	while (Q--) {
		string S; cin >> S;
		reverse(S.begin(), S.end());
		int s0 = 0, s1 = 0, s2 = 0, m0 = 0, m1 = 0, m2 = 0;
		for (int i = 0; i < L; i++) {
			char c = S[i];
			if (c == '?') {
				s2++;
				m2 |= (1 << i);
			} else if (c == '0') {
				s0++;
				m0 |= (1 << i);
			} else if (c == '1') {
				s1++;
				m1 |= (1 << i);
			}
		}
		int s = min({s0, s1, s2});
		if (s == s2) {
			int ans = A[m1];
			for (int m = m2; m > 0; m = (m - 1) & m2)  ans += A[m1 | m];
			cout << ans << '\n';
		} else if (s == s0) {
			int ans = dp2[m1];
			for (int m = m0; m > 0; m = (m - 1) & m0) {
				if (__builtin_popcount(m) % 2 == 0)	ans += dp2[m1 | m];
				else ans -= dp2[m1 | m];
			}
			cout << ans << '\n';
		} else if (s == s1) {
			int ans = s1 % 2 == 0 ? dp1[m2] : -dp1[m2];
			for (int m = m1; m > 0; m = (m - 1) & m1) {
				if (__builtin_popcount(m) % 2 == s1 % 2) ans += dp1[m2 | m];
				else  ans -= dp1[m2 | m];
			}
			cout << ans << '\n';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 172 ms 15196 KB Output is correct
12 Correct 181 ms 14672 KB Output is correct
13 Correct 203 ms 14080 KB Output is correct
14 Correct 202 ms 14204 KB Output is correct
15 Correct 197 ms 14976 KB Output is correct
16 Correct 211 ms 14168 KB Output is correct
17 Correct 221 ms 14224 KB Output is correct
18 Correct 135 ms 16212 KB Output is correct
19 Correct 158 ms 13092 KB Output is correct
20 Correct 170 ms 14804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 172 ms 15196 KB Output is correct
12 Correct 181 ms 14672 KB Output is correct
13 Correct 203 ms 14080 KB Output is correct
14 Correct 202 ms 14204 KB Output is correct
15 Correct 197 ms 14976 KB Output is correct
16 Correct 211 ms 14168 KB Output is correct
17 Correct 221 ms 14224 KB Output is correct
18 Correct 135 ms 16212 KB Output is correct
19 Correct 158 ms 13092 KB Output is correct
20 Correct 170 ms 14804 KB Output is correct
21 Correct 182 ms 18256 KB Output is correct
22 Correct 209 ms 18372 KB Output is correct
23 Correct 230 ms 17384 KB Output is correct
24 Correct 221 ms 17236 KB Output is correct
25 Correct 228 ms 19284 KB Output is correct
26 Correct 242 ms 17748 KB Output is correct
27 Correct 254 ms 17708 KB Output is correct
28 Correct 143 ms 20368 KB Output is correct
29 Correct 182 ms 16212 KB Output is correct
30 Correct 201 ms 18260 KB Output is correct
31 Correct 247 ms 18004 KB Output is correct
32 Correct 275 ms 18256 KB Output is correct
33 Correct 207 ms 17108 KB Output is correct
34 Correct 247 ms 17236 KB Output is correct
35 Correct 251 ms 17492 KB Output is correct
36 Correct 136 ms 16192 KB Output is correct
37 Correct 195 ms 18048 KB Output is correct
38 Correct 218 ms 16212 KB Output is correct
39 Correct 230 ms 17424 KB Output is correct
40 Correct 218 ms 17232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 45 ms 16112 KB Output is correct
12 Correct 38 ms 16224 KB Output is correct
13 Correct 47 ms 16372 KB Output is correct
14 Correct 53 ms 16120 KB Output is correct
15 Correct 38 ms 16112 KB Output is correct
16 Correct 49 ms 16020 KB Output is correct
17 Correct 46 ms 16156 KB Output is correct
18 Correct 32 ms 16236 KB Output is correct
19 Correct 40 ms 15860 KB Output is correct
20 Correct 36 ms 16116 KB Output is correct
21 Correct 41 ms 16224 KB Output is correct
22 Correct 52 ms 16224 KB Output is correct
23 Correct 37 ms 16116 KB Output is correct
24 Correct 48 ms 15972 KB Output is correct
25 Correct 44 ms 16116 KB Output is correct
26 Correct 34 ms 15860 KB Output is correct
27 Correct 36 ms 16228 KB Output is correct
28 Correct 37 ms 15960 KB Output is correct
29 Correct 47 ms 16112 KB Output is correct
30 Correct 42 ms 15968 KB Output is correct
31 Correct 36 ms 16112 KB Output is correct
32 Correct 49 ms 16116 KB Output is correct
33 Correct 46 ms 15976 KB Output is correct
34 Correct 32 ms 15860 KB Output is correct
35 Correct 42 ms 16108 KB Output is correct
36 Correct 45 ms 16112 KB Output is correct
37 Correct 56 ms 16112 KB Output is correct
38 Correct 44 ms 16296 KB Output is correct
39 Correct 46 ms 15964 KB Output is correct
40 Correct 42 ms 16112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 172 ms 15196 KB Output is correct
12 Correct 181 ms 14672 KB Output is correct
13 Correct 203 ms 14080 KB Output is correct
14 Correct 202 ms 14204 KB Output is correct
15 Correct 197 ms 14976 KB Output is correct
16 Correct 211 ms 14168 KB Output is correct
17 Correct 221 ms 14224 KB Output is correct
18 Correct 135 ms 16212 KB Output is correct
19 Correct 158 ms 13092 KB Output is correct
20 Correct 170 ms 14804 KB Output is correct
21 Correct 182 ms 18256 KB Output is correct
22 Correct 209 ms 18372 KB Output is correct
23 Correct 230 ms 17384 KB Output is correct
24 Correct 221 ms 17236 KB Output is correct
25 Correct 228 ms 19284 KB Output is correct
26 Correct 242 ms 17748 KB Output is correct
27 Correct 254 ms 17708 KB Output is correct
28 Correct 143 ms 20368 KB Output is correct
29 Correct 182 ms 16212 KB Output is correct
30 Correct 201 ms 18260 KB Output is correct
31 Correct 247 ms 18004 KB Output is correct
32 Correct 275 ms 18256 KB Output is correct
33 Correct 207 ms 17108 KB Output is correct
34 Correct 247 ms 17236 KB Output is correct
35 Correct 251 ms 17492 KB Output is correct
36 Correct 136 ms 16192 KB Output is correct
37 Correct 195 ms 18048 KB Output is correct
38 Correct 218 ms 16212 KB Output is correct
39 Correct 230 ms 17424 KB Output is correct
40 Correct 218 ms 17232 KB Output is correct
41 Correct 45 ms 16112 KB Output is correct
42 Correct 38 ms 16224 KB Output is correct
43 Correct 47 ms 16372 KB Output is correct
44 Correct 53 ms 16120 KB Output is correct
45 Correct 38 ms 16112 KB Output is correct
46 Correct 49 ms 16020 KB Output is correct
47 Correct 46 ms 16156 KB Output is correct
48 Correct 32 ms 16236 KB Output is correct
49 Correct 40 ms 15860 KB Output is correct
50 Correct 36 ms 16116 KB Output is correct
51 Correct 41 ms 16224 KB Output is correct
52 Correct 52 ms 16224 KB Output is correct
53 Correct 37 ms 16116 KB Output is correct
54 Correct 48 ms 15972 KB Output is correct
55 Correct 44 ms 16116 KB Output is correct
56 Correct 34 ms 15860 KB Output is correct
57 Correct 36 ms 16228 KB Output is correct
58 Correct 37 ms 15960 KB Output is correct
59 Correct 47 ms 16112 KB Output is correct
60 Correct 42 ms 15968 KB Output is correct
61 Correct 36 ms 16112 KB Output is correct
62 Correct 49 ms 16116 KB Output is correct
63 Correct 46 ms 15976 KB Output is correct
64 Correct 32 ms 15860 KB Output is correct
65 Correct 42 ms 16108 KB Output is correct
66 Correct 45 ms 16112 KB Output is correct
67 Correct 56 ms 16112 KB Output is correct
68 Correct 44 ms 16296 KB Output is correct
69 Correct 46 ms 15964 KB Output is correct
70 Correct 42 ms 16112 KB Output is correct
71 Correct 303 ms 40428 KB Output is correct
72 Correct 319 ms 40548 KB Output is correct
73 Correct 355 ms 39348 KB Output is correct
74 Correct 587 ms 39412 KB Output is correct
75 Correct 344 ms 41460 KB Output is correct
76 Correct 538 ms 39668 KB Output is correct
77 Correct 565 ms 39552 KB Output is correct
78 Correct 221 ms 43664 KB Output is correct
79 Correct 321 ms 37364 KB Output is correct
80 Correct 316 ms 40568 KB Output is correct
81 Correct 428 ms 40312 KB Output is correct
82 Correct 577 ms 39792 KB Output is correct
83 Correct 305 ms 38492 KB Output is correct
84 Correct 457 ms 39564 KB Output is correct
85 Correct 538 ms 39648 KB Output is correct
86 Correct 219 ms 37212 KB Output is correct
87 Correct 310 ms 40288 KB Output is correct
88 Correct 307 ms 37072 KB Output is correct
89 Correct 440 ms 39012 KB Output is correct
90 Correct 397 ms 39356 KB Output is correct
91 Correct 307 ms 38504 KB Output is correct
92 Correct 555 ms 39776 KB Output is correct
93 Correct 501 ms 39536 KB Output is correct
94 Correct 247 ms 37220 KB Output is correct
95 Correct 492 ms 39568 KB Output is correct
96 Correct 452 ms 39372 KB Output is correct
97 Correct 527 ms 39692 KB Output is correct
98 Correct 508 ms 39428 KB Output is correct
99 Correct 448 ms 39468 KB Output is correct
100 Correct 430 ms 39408 KB Output is correct