Submission #398559

# Submission time Handle Problem Language Result Execution time Memory
398559 2021-05-04T14:17:38 Z pure_mem Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1397 ms 46396 KB
#include <bits/stdc++.h>

#define X first
#define Y second
#define MP make_pair
#define ll long long

using namespace std;

const int N = 1e5 + 12, mod = 1e9 + 7;
const ll INF = 1e18;

int n, q, w[1 << 20], sub[1 << 20], sup[1 << 20], cnt[1 << 20];
                                   
int main () {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> q;
	int all = (1 << n) - 1;
	for(int i = 0;i < (1 << n);i++){
		char x; cin >> x, w[i] = x - '0', cnt[i] = __builtin_popcount(i);
		sup[i] = sub[all ^ i] = w[i];
	}
	for(int i = 0;i < n;i++){
		for(int j = 0;j < (1 << n);j++){
			if((j >> i) & 1)
				sup[j] += sup[j ^ (1 << i)];
		}
		for(int j = 0;j < (1 << n);j++){
			if((j >> i) & 1)
				sub[j] += sub[j ^ (1 << i)];
		}
	}
	for(int i = 0;i < q;i++){
		int A = 0, B = 0, C = 0;
		int cA = 0, cB = 0, cC = 0;
		for(int j = 1;j <= n;j++){
			char x;
			cin >> x;
			if(x == '0')
				cA += 1, A ^= (1 << (n - j));
			else if(x == '1')
				cB += 1, B ^= (1 << (n - j));
			else
				cC += 1, C ^= (1 << (n - j));
		}
		int ans = 0;
		if(cA <= cC && cA <= cB){
			for(int mask = A;mask >= 0;mask = (mask - 1) & A){
				if((cnt[A] - cnt[mask]) & 1)
					ans -= sub[mask | C];
				else
					ans += sub[mask | C];	
				if(mask == 0)
					break;
			}		
		}
		else if(cB <= cA && cB <= cC){
			for(int mask = B;mask >= 0;mask = (mask - 1) & B){
				if((cnt[B] - cnt[mask]) & 1) 
					ans -= sup[mask | C];
				else
					ans += sup[mask | C];
				if(mask == 0)
					break;
			}	
		}
		else{
			for(int mask = C;mask >= 0;mask = (mask - 1) & C){
				ans += w[B | mask];
				if(mask == 0)
					break;
			}
		}
		cout << ans << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 370 ms 15040 KB Output is correct
12 Correct 341 ms 14732 KB Output is correct
13 Correct 364 ms 14092 KB Output is correct
14 Correct 359 ms 14020 KB Output is correct
15 Correct 348 ms 15192 KB Output is correct
16 Correct 376 ms 14264 KB Output is correct
17 Correct 390 ms 14220 KB Output is correct
18 Correct 285 ms 16140 KB Output is correct
19 Correct 328 ms 13096 KB Output is correct
20 Correct 345 ms 14788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 370 ms 15040 KB Output is correct
12 Correct 341 ms 14732 KB Output is correct
13 Correct 364 ms 14092 KB Output is correct
14 Correct 359 ms 14020 KB Output is correct
15 Correct 348 ms 15192 KB Output is correct
16 Correct 376 ms 14264 KB Output is correct
17 Correct 390 ms 14220 KB Output is correct
18 Correct 285 ms 16140 KB Output is correct
19 Correct 328 ms 13096 KB Output is correct
20 Correct 345 ms 14788 KB Output is correct
21 Correct 414 ms 18148 KB Output is correct
22 Correct 412 ms 18328 KB Output is correct
23 Correct 485 ms 17320 KB Output is correct
24 Correct 496 ms 17364 KB Output is correct
25 Correct 448 ms 19148 KB Output is correct
26 Correct 470 ms 17612 KB Output is correct
27 Correct 493 ms 17620 KB Output is correct
28 Correct 303 ms 20096 KB Output is correct
29 Correct 406 ms 16252 KB Output is correct
30 Correct 421 ms 18304 KB Output is correct
31 Correct 434 ms 18152 KB Output is correct
32 Correct 509 ms 18140 KB Output is correct
33 Correct 403 ms 17092 KB Output is correct
34 Correct 475 ms 17088 KB Output is correct
35 Correct 478 ms 17620 KB Output is correct
36 Correct 324 ms 16068 KB Output is correct
37 Correct 402 ms 18244 KB Output is correct
38 Correct 400 ms 16096 KB Output is correct
39 Correct 487 ms 17352 KB Output is correct
40 Correct 494 ms 17188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 106 ms 18968 KB Output is correct
12 Correct 105 ms 19000 KB Output is correct
13 Correct 122 ms 18876 KB Output is correct
14 Correct 132 ms 18888 KB Output is correct
15 Correct 103 ms 19012 KB Output is correct
16 Correct 137 ms 18948 KB Output is correct
17 Correct 128 ms 18924 KB Output is correct
18 Correct 96 ms 19132 KB Output is correct
19 Correct 105 ms 18816 KB Output is correct
20 Correct 113 ms 19004 KB Output is correct
21 Correct 113 ms 19000 KB Output is correct
22 Correct 134 ms 19028 KB Output is correct
23 Correct 104 ms 18908 KB Output is correct
24 Correct 135 ms 18888 KB Output is correct
25 Correct 160 ms 18884 KB Output is correct
26 Correct 117 ms 18928 KB Output is correct
27 Correct 103 ms 18984 KB Output is correct
28 Correct 113 ms 18752 KB Output is correct
29 Correct 149 ms 18944 KB Output is correct
30 Correct 115 ms 18888 KB Output is correct
31 Correct 107 ms 18908 KB Output is correct
32 Correct 142 ms 18884 KB Output is correct
33 Correct 141 ms 18884 KB Output is correct
34 Correct 97 ms 18748 KB Output is correct
35 Correct 124 ms 18908 KB Output is correct
36 Correct 121 ms 18904 KB Output is correct
37 Correct 135 ms 18928 KB Output is correct
38 Correct 129 ms 18884 KB Output is correct
39 Correct 125 ms 18880 KB Output is correct
40 Correct 124 ms 18976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 370 ms 15040 KB Output is correct
12 Correct 341 ms 14732 KB Output is correct
13 Correct 364 ms 14092 KB Output is correct
14 Correct 359 ms 14020 KB Output is correct
15 Correct 348 ms 15192 KB Output is correct
16 Correct 376 ms 14264 KB Output is correct
17 Correct 390 ms 14220 KB Output is correct
18 Correct 285 ms 16140 KB Output is correct
19 Correct 328 ms 13096 KB Output is correct
20 Correct 345 ms 14788 KB Output is correct
21 Correct 414 ms 18148 KB Output is correct
22 Correct 412 ms 18328 KB Output is correct
23 Correct 485 ms 17320 KB Output is correct
24 Correct 496 ms 17364 KB Output is correct
25 Correct 448 ms 19148 KB Output is correct
26 Correct 470 ms 17612 KB Output is correct
27 Correct 493 ms 17620 KB Output is correct
28 Correct 303 ms 20096 KB Output is correct
29 Correct 406 ms 16252 KB Output is correct
30 Correct 421 ms 18304 KB Output is correct
31 Correct 434 ms 18152 KB Output is correct
32 Correct 509 ms 18140 KB Output is correct
33 Correct 403 ms 17092 KB Output is correct
34 Correct 475 ms 17088 KB Output is correct
35 Correct 478 ms 17620 KB Output is correct
36 Correct 324 ms 16068 KB Output is correct
37 Correct 402 ms 18244 KB Output is correct
38 Correct 400 ms 16096 KB Output is correct
39 Correct 487 ms 17352 KB Output is correct
40 Correct 494 ms 17188 KB Output is correct
41 Correct 106 ms 18968 KB Output is correct
42 Correct 105 ms 19000 KB Output is correct
43 Correct 122 ms 18876 KB Output is correct
44 Correct 132 ms 18888 KB Output is correct
45 Correct 103 ms 19012 KB Output is correct
46 Correct 137 ms 18948 KB Output is correct
47 Correct 128 ms 18924 KB Output is correct
48 Correct 96 ms 19132 KB Output is correct
49 Correct 105 ms 18816 KB Output is correct
50 Correct 113 ms 19004 KB Output is correct
51 Correct 113 ms 19000 KB Output is correct
52 Correct 134 ms 19028 KB Output is correct
53 Correct 104 ms 18908 KB Output is correct
54 Correct 135 ms 18888 KB Output is correct
55 Correct 160 ms 18884 KB Output is correct
56 Correct 117 ms 18928 KB Output is correct
57 Correct 103 ms 18984 KB Output is correct
58 Correct 113 ms 18752 KB Output is correct
59 Correct 149 ms 18944 KB Output is correct
60 Correct 115 ms 18888 KB Output is correct
61 Correct 107 ms 18908 KB Output is correct
62 Correct 142 ms 18884 KB Output is correct
63 Correct 141 ms 18884 KB Output is correct
64 Correct 97 ms 18748 KB Output is correct
65 Correct 124 ms 18908 KB Output is correct
66 Correct 121 ms 18904 KB Output is correct
67 Correct 135 ms 18928 KB Output is correct
68 Correct 129 ms 18884 KB Output is correct
69 Correct 125 ms 18880 KB Output is correct
70 Correct 124 ms 18976 KB Output is correct
71 Correct 783 ms 43320 KB Output is correct
72 Correct 725 ms 43420 KB Output is correct
73 Correct 936 ms 41900 KB Output is correct
74 Correct 1397 ms 42220 KB Output is correct
75 Correct 684 ms 44344 KB Output is correct
76 Correct 1297 ms 42544 KB Output is correct
77 Correct 1163 ms 42516 KB Output is correct
78 Correct 512 ms 46396 KB Output is correct
79 Correct 738 ms 40300 KB Output is correct
80 Correct 758 ms 43464 KB Output is correct
81 Correct 990 ms 43224 KB Output is correct
82 Correct 1257 ms 42204 KB Output is correct
83 Correct 697 ms 41476 KB Output is correct
84 Correct 1299 ms 42236 KB Output is correct
85 Correct 1170 ms 42404 KB Output is correct
86 Correct 527 ms 40424 KB Output is correct
87 Correct 704 ms 43232 KB Output is correct
88 Correct 737 ms 40192 KB Output is correct
89 Correct 845 ms 41892 KB Output is correct
90 Correct 1020 ms 42204 KB Output is correct
91 Correct 704 ms 41460 KB Output is correct
92 Correct 1348 ms 42652 KB Output is correct
93 Correct 1161 ms 42472 KB Output is correct
94 Correct 478 ms 40292 KB Output is correct
95 Correct 1031 ms 42408 KB Output is correct
96 Correct 999 ms 42312 KB Output is correct
97 Correct 1025 ms 42356 KB Output is correct
98 Correct 1002 ms 42392 KB Output is correct
99 Correct 1026 ms 42332 KB Output is correct
100 Correct 972 ms 42408 KB Output is correct