Submission #703109

# Submission time Handle Problem Language Result Execution time Memory
703109 2023-02-26T06:52:12 Z AmirAli_H1 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
826 ms 26340 KB
// In the name of Allah

#include <bits/stdc++.h>
using namespace std;

typedef long long int	ll;
typedef long double	ld;
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pll;

#define all(x)		(x).begin(),(x).end()
#define len(x)		((ll) (x).size())
#define F		first
#define S		second
#define pb		push_back
#define sep             ' '
#define endl            '\n'
#define Mp		make_pair
#define debug(x)	cerr << #x << ": " <<  x << endl;
#define kill(x)		cout << x << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define file_io(x,y)	freopen(x, "r", stdin); freopen(y, "w", stdout);

int n, k, q;
const int maxk = 20;
const int maxn = (1 << 20) + 3;
string s;
int a[maxn];
int dp1[maxn], dp2[maxn];

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	cin >> k >> q;
	n = (1 << k);
	cin >> s;
	for (int i = 0; i < n; i++) {
		a[i] = s[i] - '0';
	}
	
	for (int i = 0; i <= k; i++) {
		for (int mask = n - 1; mask >= 0; mask--) {
			if (i == 0) {
				dp1[mask] = a[mask];
				continue;
			}
			
			if (mask & (1 << (i - 1))) dp1[mask] += dp1[mask ^ (1 << (i - 1))];
		}
	}
	
	for (int i = 0; i <= k; i++) {
		for (int mask = 0; mask < n; mask++) {
			if (i == 0) {
				dp2[mask] = a[mask];
				continue;
			}
			
			if (!(mask & (1 << (i - 1)))) dp2[mask] += dp2[mask ^ (1 << (i - 1))];
		}
	}
	
	while (q--) {
		cin >> s;
		int t0 = 0, t1 = 0, t2 = 0;
		int val0 = 0, val1 = 0, val2 = 0;
		for (int i = 0; i < k; i++) {
			if (s[i] == '0') {
				t0++;
				val0 += (1 << (k - i - 1));
			}
			else if (s[i] == '1') {
				t1++;
				val1 += (1 << (k - i - 1));
			}
			else {
				t2++;
				val2 += (1 << (k - i - 1));
			}
		}
		
		int tm = min(min(t0, t1), t2);
		ll output = 0;
		if (tm == t0) {
			for (int mask = val0; mask >= 0; mask = (mask - 1) & val0) {
				int t = __builtin_popcount(mask);
				if (t % 2 == 0) output += dp2[val1 ^ mask];
				else output -= dp2[val1 ^ mask];
				
				if (mask == 0) break;
			}
		}
		else if (tm == t1) {
			for (int mask = val1; mask >= 0; mask = (mask - 1) & val1) {
				int t = __builtin_popcount(mask);
				if (t % 2 == 0) output += dp1[val2 ^ (val1 ^ mask)];
				else output -= dp1[val2 ^ (val1 ^ mask)];
				
				if (mask == 0) break;
			}
		}
		else {
			for (int mask = val2; mask >= 0; mask = (mask - 1) & val2) {
				output += a[val1 ^ mask];
				if (mask == 0) break;
			}
		}
		cout << output << endl;
	}
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 201 ms 15060 KB Output is correct
12 Correct 227 ms 14704 KB Output is correct
13 Correct 247 ms 13896 KB Output is correct
14 Correct 253 ms 14052 KB Output is correct
15 Correct 221 ms 15000 KB Output is correct
16 Correct 247 ms 14164 KB Output is correct
17 Correct 249 ms 14220 KB Output is correct
18 Correct 156 ms 16120 KB Output is correct
19 Correct 187 ms 13000 KB Output is correct
20 Correct 210 ms 14656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 201 ms 15060 KB Output is correct
12 Correct 227 ms 14704 KB Output is correct
13 Correct 247 ms 13896 KB Output is correct
14 Correct 253 ms 14052 KB Output is correct
15 Correct 221 ms 15000 KB Output is correct
16 Correct 247 ms 14164 KB Output is correct
17 Correct 249 ms 14220 KB Output is correct
18 Correct 156 ms 16120 KB Output is correct
19 Correct 187 ms 13000 KB Output is correct
20 Correct 210 ms 14656 KB Output is correct
21 Correct 224 ms 18060 KB Output is correct
22 Correct 223 ms 18292 KB Output is correct
23 Correct 271 ms 17208 KB Output is correct
24 Correct 297 ms 17136 KB Output is correct
25 Correct 230 ms 19104 KB Output is correct
26 Correct 279 ms 17604 KB Output is correct
27 Correct 284 ms 17652 KB Output is correct
28 Correct 166 ms 20032 KB Output is correct
29 Correct 214 ms 16124 KB Output is correct
30 Correct 221 ms 18268 KB Output is correct
31 Correct 282 ms 18168 KB Output is correct
32 Correct 292 ms 18128 KB Output is correct
33 Correct 240 ms 17076 KB Output is correct
34 Correct 297 ms 17068 KB Output is correct
35 Correct 283 ms 17488 KB Output is correct
36 Correct 184 ms 16116 KB Output is correct
37 Correct 217 ms 18036 KB Output is correct
38 Correct 216 ms 16116 KB Output is correct
39 Correct 261 ms 17316 KB Output is correct
40 Correct 311 ms 17028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 66 ms 15980 KB Output is correct
12 Correct 63 ms 16064 KB Output is correct
13 Correct 74 ms 15888 KB Output is correct
14 Correct 88 ms 15980 KB Output is correct
15 Correct 65 ms 16060 KB Output is correct
16 Correct 101 ms 15956 KB Output is correct
17 Correct 78 ms 16016 KB Output is correct
18 Correct 56 ms 16136 KB Output is correct
19 Correct 62 ms 15808 KB Output is correct
20 Correct 60 ms 16060 KB Output is correct
21 Correct 75 ms 16060 KB Output is correct
22 Correct 71 ms 15940 KB Output is correct
23 Correct 60 ms 15936 KB Output is correct
24 Correct 91 ms 15932 KB Output is correct
25 Correct 78 ms 15944 KB Output is correct
26 Correct 54 ms 15880 KB Output is correct
27 Correct 60 ms 16064 KB Output is correct
28 Correct 62 ms 15820 KB Output is correct
29 Correct 65 ms 15912 KB Output is correct
30 Correct 67 ms 16032 KB Output is correct
31 Correct 63 ms 15940 KB Output is correct
32 Correct 93 ms 15936 KB Output is correct
33 Correct 100 ms 16036 KB Output is correct
34 Correct 56 ms 15812 KB Output is correct
35 Correct 76 ms 15932 KB Output is correct
36 Correct 75 ms 15948 KB Output is correct
37 Correct 78 ms 15964 KB Output is correct
38 Correct 78 ms 16112 KB Output is correct
39 Correct 79 ms 15952 KB Output is correct
40 Correct 84 ms 15940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 201 ms 15060 KB Output is correct
12 Correct 227 ms 14704 KB Output is correct
13 Correct 247 ms 13896 KB Output is correct
14 Correct 253 ms 14052 KB Output is correct
15 Correct 221 ms 15000 KB Output is correct
16 Correct 247 ms 14164 KB Output is correct
17 Correct 249 ms 14220 KB Output is correct
18 Correct 156 ms 16120 KB Output is correct
19 Correct 187 ms 13000 KB Output is correct
20 Correct 210 ms 14656 KB Output is correct
21 Correct 224 ms 18060 KB Output is correct
22 Correct 223 ms 18292 KB Output is correct
23 Correct 271 ms 17208 KB Output is correct
24 Correct 297 ms 17136 KB Output is correct
25 Correct 230 ms 19104 KB Output is correct
26 Correct 279 ms 17604 KB Output is correct
27 Correct 284 ms 17652 KB Output is correct
28 Correct 166 ms 20032 KB Output is correct
29 Correct 214 ms 16124 KB Output is correct
30 Correct 221 ms 18268 KB Output is correct
31 Correct 282 ms 18168 KB Output is correct
32 Correct 292 ms 18128 KB Output is correct
33 Correct 240 ms 17076 KB Output is correct
34 Correct 297 ms 17068 KB Output is correct
35 Correct 283 ms 17488 KB Output is correct
36 Correct 184 ms 16116 KB Output is correct
37 Correct 217 ms 18036 KB Output is correct
38 Correct 216 ms 16116 KB Output is correct
39 Correct 261 ms 17316 KB Output is correct
40 Correct 311 ms 17028 KB Output is correct
41 Correct 66 ms 15980 KB Output is correct
42 Correct 63 ms 16064 KB Output is correct
43 Correct 74 ms 15888 KB Output is correct
44 Correct 88 ms 15980 KB Output is correct
45 Correct 65 ms 16060 KB Output is correct
46 Correct 101 ms 15956 KB Output is correct
47 Correct 78 ms 16016 KB Output is correct
48 Correct 56 ms 16136 KB Output is correct
49 Correct 62 ms 15808 KB Output is correct
50 Correct 60 ms 16060 KB Output is correct
51 Correct 75 ms 16060 KB Output is correct
52 Correct 71 ms 15940 KB Output is correct
53 Correct 60 ms 15936 KB Output is correct
54 Correct 91 ms 15932 KB Output is correct
55 Correct 78 ms 15944 KB Output is correct
56 Correct 54 ms 15880 KB Output is correct
57 Correct 60 ms 16064 KB Output is correct
58 Correct 62 ms 15820 KB Output is correct
59 Correct 65 ms 15912 KB Output is correct
60 Correct 67 ms 16032 KB Output is correct
61 Correct 63 ms 15940 KB Output is correct
62 Correct 93 ms 15936 KB Output is correct
63 Correct 100 ms 16036 KB Output is correct
64 Correct 56 ms 15812 KB Output is correct
65 Correct 76 ms 15932 KB Output is correct
66 Correct 75 ms 15948 KB Output is correct
67 Correct 78 ms 15964 KB Output is correct
68 Correct 78 ms 16112 KB Output is correct
69 Correct 79 ms 15952 KB Output is correct
70 Correct 84 ms 15940 KB Output is correct
71 Correct 394 ms 23344 KB Output is correct
72 Correct 373 ms 23592 KB Output is correct
73 Correct 568 ms 22020 KB Output is correct
74 Correct 676 ms 22368 KB Output is correct
75 Correct 373 ms 24356 KB Output is correct
76 Correct 778 ms 22644 KB Output is correct
77 Correct 817 ms 22788 KB Output is correct
78 Correct 235 ms 26340 KB Output is correct
79 Correct 361 ms 20316 KB Output is correct
80 Correct 375 ms 23504 KB Output is correct
81 Correct 552 ms 23436 KB Output is correct
82 Correct 743 ms 22416 KB Output is correct
83 Correct 383 ms 21632 KB Output is correct
84 Correct 809 ms 22396 KB Output is correct
85 Correct 713 ms 22512 KB Output is correct
86 Correct 250 ms 20316 KB Output is correct
87 Correct 317 ms 23408 KB Output is correct
88 Correct 398 ms 20300 KB Output is correct
89 Correct 535 ms 21928 KB Output is correct
90 Correct 496 ms 22432 KB Output is correct
91 Correct 354 ms 21480 KB Output is correct
92 Correct 826 ms 22564 KB Output is correct
93 Correct 702 ms 22496 KB Output is correct
94 Correct 225 ms 20336 KB Output is correct
95 Correct 595 ms 22432 KB Output is correct
96 Correct 603 ms 22588 KB Output is correct
97 Correct 641 ms 22508 KB Output is correct
98 Correct 589 ms 22564 KB Output is correct
99 Correct 634 ms 22540 KB Output is correct
100 Correct 615 ms 22376 KB Output is correct