Submission #848699

# Submission time Handle Problem Language Result Execution time Memory
848699 2023-09-13T10:01:12 Z TranGiaHuy1508 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
871 ms 38680 KB
#include <bits/stdc++.h>
using namespace std;

void main_program();

signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	main_program();
}

const int maxn = 1 << 20;

int lg, n, q;
vector<int> v;

vector<int> subsetZeta(vector<int> V){
	for (int j = 0; j < lg; j++){
		for (int i = 0; i < n; i++){
			if (((i >> j) & 1)){
				V[i] += V[i ^ (1 << j)];
			}
		}
	}
	return V;
}

vector<int> supersetZeta(vector<int> V){
	for (int j = 0; j < lg; j++){
		for (int i = 0; i < n; i++){
			if (((i >> j) & 1) == 0){
				V[i] += V[i ^ (1 << j)];
			}
		}
	}
	return V;
}

void main_program(){
	cin >> lg >> q;
	n = 1 << lg;

	v.resize(n);
	string s; cin >> s;
	for (int i = 0; i < n; i++) v[i] = (s[i] - '0');

	vector<int> subset = subsetZeta(v), superset = supersetZeta(v);

	for (int _ = 0; _ < q; _++){
		string Q; cin >> Q;

		int mask0 = 0, mask1 = 0, mask2 = 0;
		for (int i = 0; i < lg; i++){
			if (Q[i] == '0') mask0 |= (1 << (lg-1 - i));
			if (Q[i] == '1') mask1 |= (1 << (lg-1 - i));
			if (Q[i] == '?') mask2 |= (1 << (lg-1 - i));
		}
		int popcnt0 = __builtin_popcount(mask0);
		int popcnt1 = __builtin_popcount(mask1);
		int popcnt2 = __builtin_popcount(mask2);
		int mn = min({popcnt0, popcnt1, popcnt2});

		if (popcnt0 == mn){
			int sum = 0;
			for (int submask = mask0; submask > 0; submask = (submask - 1) & mask0){
				int num = mask1 + submask;
				int sign = ((__builtin_popcount(submask) & 1)) ? -1 : 1;
				sum += superset[num] * sign;
			}
			{
				int submask = 0;
				int num = mask1 + submask;
				int sign = ((__builtin_popcount(submask) & 1)) ? -1 : 1;
				sum += superset[num] * sign;
			}
			cout << sum << "\n";
		}
		else if (popcnt1 == mn){
			int sum = 0;
			for (int submask = mask1; submask > 0; submask = (submask - 1) & mask1){
				int num = mask2 + submask;
				int sign = ((__builtin_popcount(submask) & 1) == (popcnt1 & 1)) ? 1 : -1;
				sum += subset[num] * sign;
			}
			{
				int submask = 0;
				int num = mask2 + submask;
				int sign = ((__builtin_popcount(submask) & 1) == (popcnt1 & 1)) ? 1 : -1;
				sum += subset[num] * sign;
			}
			cout << sum << "\n";
		}
		else{
			int sum = 0;
			for (int submask = mask2; submask > 0; submask = (submask - 1) & mask2){
				int num = mask1 + submask;
				sum += v[num];
			}
			{
				int submask = 0;
				int num = mask1 + submask;
				sum += v[num];
			}
			cout << sum << "\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 1 ms 468 KB Output is correct
11 Correct 183 ms 12920 KB Output is correct
12 Correct 179 ms 12572 KB Output is correct
13 Correct 203 ms 11624 KB Output is correct
14 Correct 243 ms 11896 KB Output is correct
15 Correct 188 ms 12884 KB Output is correct
16 Correct 237 ms 11844 KB Output is correct
17 Correct 223 ms 11840 KB Output is correct
18 Correct 143 ms 13768 KB Output is correct
19 Correct 160 ms 10796 KB Output is correct
20 Correct 209 ms 12368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 1 ms 468 KB Output is correct
11 Correct 183 ms 12920 KB Output is correct
12 Correct 179 ms 12572 KB Output is correct
13 Correct 203 ms 11624 KB Output is correct
14 Correct 243 ms 11896 KB Output is correct
15 Correct 188 ms 12884 KB Output is correct
16 Correct 237 ms 11844 KB Output is correct
17 Correct 223 ms 11840 KB Output is correct
18 Correct 143 ms 13768 KB Output is correct
19 Correct 160 ms 10796 KB Output is correct
20 Correct 209 ms 12368 KB Output is correct
21 Correct 199 ms 15092 KB Output is correct
22 Correct 203 ms 15272 KB Output is correct
23 Correct 239 ms 14420 KB Output is correct
24 Correct 265 ms 14188 KB Output is correct
25 Correct 211 ms 16240 KB Output is correct
26 Correct 258 ms 14812 KB Output is correct
27 Correct 251 ms 14668 KB Output is correct
28 Correct 154 ms 17132 KB Output is correct
29 Correct 199 ms 13240 KB Output is correct
30 Correct 215 ms 15444 KB Output is correct
31 Correct 246 ms 15288 KB Output is correct
32 Correct 267 ms 15196 KB Output is correct
33 Correct 204 ms 14160 KB Output is correct
34 Correct 257 ms 14148 KB Output is correct
35 Correct 256 ms 14764 KB Output is correct
36 Correct 148 ms 13088 KB Output is correct
37 Correct 188 ms 15260 KB Output is correct
38 Correct 196 ms 13312 KB Output is correct
39 Correct 246 ms 14420 KB Output is correct
40 Correct 261 ms 14300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 1 ms 468 KB Output is correct
11 Correct 51 ms 16012 KB Output is correct
12 Correct 55 ms 16184 KB Output is correct
13 Correct 62 ms 16124 KB Output is correct
14 Correct 71 ms 15972 KB Output is correct
15 Correct 57 ms 16212 KB Output is correct
16 Correct 63 ms 16180 KB Output is correct
17 Correct 70 ms 16160 KB Output is correct
18 Correct 48 ms 16116 KB Output is correct
19 Correct 53 ms 15940 KB Output is correct
20 Correct 51 ms 16104 KB Output is correct
21 Correct 61 ms 16212 KB Output is correct
22 Correct 72 ms 15916 KB Output is correct
23 Correct 54 ms 15944 KB Output is correct
24 Correct 101 ms 15964 KB Output is correct
25 Correct 62 ms 15968 KB Output is correct
26 Correct 52 ms 16116 KB Output is correct
27 Correct 54 ms 16192 KB Output is correct
28 Correct 53 ms 15976 KB Output is correct
29 Correct 54 ms 16120 KB Output is correct
30 Correct 56 ms 16120 KB Output is correct
31 Correct 62 ms 16372 KB Output is correct
32 Correct 64 ms 16120 KB Output is correct
33 Correct 61 ms 16008 KB Output is correct
34 Correct 46 ms 16116 KB Output is correct
35 Correct 59 ms 16116 KB Output is correct
36 Correct 59 ms 15960 KB Output is correct
37 Correct 65 ms 16120 KB Output is correct
38 Correct 68 ms 15952 KB Output is correct
39 Correct 58 ms 15960 KB Output is correct
40 Correct 57 ms 16172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 1 ms 468 KB Output is correct
11 Correct 183 ms 12920 KB Output is correct
12 Correct 179 ms 12572 KB Output is correct
13 Correct 203 ms 11624 KB Output is correct
14 Correct 243 ms 11896 KB Output is correct
15 Correct 188 ms 12884 KB Output is correct
16 Correct 237 ms 11844 KB Output is correct
17 Correct 223 ms 11840 KB Output is correct
18 Correct 143 ms 13768 KB Output is correct
19 Correct 160 ms 10796 KB Output is correct
20 Correct 209 ms 12368 KB Output is correct
21 Correct 199 ms 15092 KB Output is correct
22 Correct 203 ms 15272 KB Output is correct
23 Correct 239 ms 14420 KB Output is correct
24 Correct 265 ms 14188 KB Output is correct
25 Correct 211 ms 16240 KB Output is correct
26 Correct 258 ms 14812 KB Output is correct
27 Correct 251 ms 14668 KB Output is correct
28 Correct 154 ms 17132 KB Output is correct
29 Correct 199 ms 13240 KB Output is correct
30 Correct 215 ms 15444 KB Output is correct
31 Correct 246 ms 15288 KB Output is correct
32 Correct 267 ms 15196 KB Output is correct
33 Correct 204 ms 14160 KB Output is correct
34 Correct 257 ms 14148 KB Output is correct
35 Correct 256 ms 14764 KB Output is correct
36 Correct 148 ms 13088 KB Output is correct
37 Correct 188 ms 15260 KB Output is correct
38 Correct 196 ms 13312 KB Output is correct
39 Correct 246 ms 14420 KB Output is correct
40 Correct 261 ms 14300 KB Output is correct
41 Correct 51 ms 16012 KB Output is correct
42 Correct 55 ms 16184 KB Output is correct
43 Correct 62 ms 16124 KB Output is correct
44 Correct 71 ms 15972 KB Output is correct
45 Correct 57 ms 16212 KB Output is correct
46 Correct 63 ms 16180 KB Output is correct
47 Correct 70 ms 16160 KB Output is correct
48 Correct 48 ms 16116 KB Output is correct
49 Correct 53 ms 15940 KB Output is correct
50 Correct 51 ms 16104 KB Output is correct
51 Correct 61 ms 16212 KB Output is correct
52 Correct 72 ms 15916 KB Output is correct
53 Correct 54 ms 15944 KB Output is correct
54 Correct 101 ms 15964 KB Output is correct
55 Correct 62 ms 15968 KB Output is correct
56 Correct 52 ms 16116 KB Output is correct
57 Correct 54 ms 16192 KB Output is correct
58 Correct 53 ms 15976 KB Output is correct
59 Correct 54 ms 16120 KB Output is correct
60 Correct 56 ms 16120 KB Output is correct
61 Correct 62 ms 16372 KB Output is correct
62 Correct 64 ms 16120 KB Output is correct
63 Correct 61 ms 16008 KB Output is correct
64 Correct 46 ms 16116 KB Output is correct
65 Correct 59 ms 16116 KB Output is correct
66 Correct 59 ms 15960 KB Output is correct
67 Correct 65 ms 16120 KB Output is correct
68 Correct 68 ms 15952 KB Output is correct
69 Correct 58 ms 15960 KB Output is correct
70 Correct 57 ms 16172 KB Output is correct
71 Correct 336 ms 35392 KB Output is correct
72 Correct 337 ms 35684 KB Output is correct
73 Correct 418 ms 34108 KB Output is correct
74 Correct 654 ms 34416 KB Output is correct
75 Correct 365 ms 36380 KB Output is correct
76 Correct 624 ms 34656 KB Output is correct
77 Correct 871 ms 34608 KB Output is correct
78 Correct 243 ms 38680 KB Output is correct
79 Correct 352 ms 32560 KB Output is correct
80 Correct 361 ms 35620 KB Output is correct
81 Correct 441 ms 35568 KB Output is correct
82 Correct 679 ms 34480 KB Output is correct
83 Correct 350 ms 33688 KB Output is correct
84 Correct 790 ms 34432 KB Output is correct
85 Correct 648 ms 34560 KB Output is correct
86 Correct 219 ms 32356 KB Output is correct
87 Correct 318 ms 35428 KB Output is correct
88 Correct 309 ms 27128 KB Output is correct
89 Correct 434 ms 28856 KB Output is correct
90 Correct 436 ms 28868 KB Output is correct
91 Correct 372 ms 27908 KB Output is correct
92 Correct 644 ms 29140 KB Output is correct
93 Correct 594 ms 28940 KB Output is correct
94 Correct 215 ms 26740 KB Output is correct
95 Correct 503 ms 28916 KB Output is correct
96 Correct 508 ms 29048 KB Output is correct
97 Correct 461 ms 28792 KB Output is correct
98 Correct 515 ms 28968 KB Output is correct
99 Correct 564 ms 28888 KB Output is correct
100 Correct 498 ms 28968 KB Output is correct