Submission #334278

# Submission time Handle Problem Language Result Execution time Memory
334278 2020-12-08T22:55:21 Z ChrisT Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1865 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;
int val[1 << 20], sum1[1 << 20], sum2[1 << 20], nmask[1 << 6]; 
vector<int> pre[1 << 20];
char str[25];
int main() { //nut
	int n,q;
	scanf ("%d %d",&n,&q);
	for (int i = 0; i < (1 << n); i++) {
		char ch; scanf (" %c",&ch); 
		sum2[((1 << n) - 1) ^ i] = sum1[i] = val[i] = ch - '0';
	}
	for (int i = 0; i < n; i++) {
		for (int mask = 0; mask < (1 << n); mask++) if (mask >> i & 1) {
			sum1[mask] += sum1[mask ^ (1 << i)];
			sum2[mask] += sum2[mask ^ (1 << i)];
		}
	}
	for (int mask = 0; mask < (1 << n); mask++) if (__builtin_popcount(mask) <= n/3) {
		vector<int> pos;
		for (int i = 0; i < n; i++) if (mask >> i & 1) pos.push_back(i);
		for (int mask2 = 0; mask2 < (1 << (int)pos.size()); mask2++) {
			int nmask = 0;
			for (int i = 0; i < (int)pos.size(); i++) if (mask2 >> i & 1) nmask |= (1 << pos[i]);
			pre[mask].push_back(nmask);
		}
	}
	while (q--) {
		scanf ("%s",str);
		reverse(str,str+n);
		int cnt0 = 0, cnt1 = 0, cntq = 0;
		for (int i = 0; i < n; i++) {
			if (str[i] == '0') ++cnt0;
			else if (str[i] == '1') ++cnt1;
			else ++cntq;
		}
		if (cntq <= cnt0 && cntq <= cnt1) { //brute force ?s
			int mask = 0, ret = 0, mask2 = 0;
			for (int i = 0; i < n; i++) {
				if (str[i] == '?') mask2 |= (1<<i);
				mask |= (str[i] == '1') << i;
			}
			for (int xormask : pre[mask2]) {
				ret += val[mask ^ xormask];
			}
			printf ("%d\n",ret);
		} else if (cnt0 <= cnt1) { //PIE
			int mask = 0, ret = 0, mask2 = 0;
			for (int i = 0; i < n; i++) {
				if (str[i] == '0') mask2 |= (1 << i);
				mask |= (str[i] != '1') << i;
			}
			for (int xormask : pre[mask2]) {
				ret += (__builtin_popcount(xormask) & 1 ? -1 : 1) * sum2[mask ^ xormask];
			}
			printf ("%d\n",ret);
		} else { //'' ''
			int mask = 0, ret = 0, mask2 = 0;
			for (int i = 0; i < n; i++) {
				if (str[i] == '1') mask2 |= (1 << i);
				mask |= (str[i] != '0') << i;
			}
			for (int xormask : pre[mask2]) {
				ret += (__builtin_popcount(xormask) & 1 ? -1 : 1) * sum1[mask ^ xormask];
			}
			printf ("%d\n",ret);
		}
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:8:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    8 |  scanf ("%d %d",&n,&q);
      |  ~~~~~~^~~~~~~~~~~~~~~
snake_escaping.cpp:10:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   10 |   char ch; scanf (" %c",&ch);
      |            ~~~~~~^~~~~~~~~~~
snake_escaping.cpp:29:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   29 |   scanf ("%s",str);
      |   ~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24940 KB Output is correct
2 Correct 18 ms 24940 KB Output is correct
3 Correct 18 ms 24940 KB Output is correct
4 Correct 18 ms 24940 KB Output is correct
5 Correct 18 ms 24940 KB Output is correct
6 Correct 18 ms 24940 KB Output is correct
7 Correct 18 ms 24940 KB Output is correct
8 Correct 18 ms 24940 KB Output is correct
9 Correct 18 ms 24940 KB Output is correct
10 Correct 19 ms 24940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24940 KB Output is correct
2 Correct 18 ms 24940 KB Output is correct
3 Correct 18 ms 24940 KB Output is correct
4 Correct 18 ms 24940 KB Output is correct
5 Correct 18 ms 24940 KB Output is correct
6 Correct 18 ms 24940 KB Output is correct
7 Correct 18 ms 24940 KB Output is correct
8 Correct 18 ms 24940 KB Output is correct
9 Correct 18 ms 24940 KB Output is correct
10 Correct 19 ms 24940 KB Output is correct
11 Correct 332 ms 33648 KB Output is correct
12 Correct 334 ms 33044 KB Output is correct
13 Correct 363 ms 32280 KB Output is correct
14 Correct 339 ms 32236 KB Output is correct
15 Correct 346 ms 33348 KB Output is correct
16 Correct 368 ms 32492 KB Output is correct
17 Correct 375 ms 33040 KB Output is correct
18 Correct 278 ms 34540 KB Output is correct
19 Correct 307 ms 31724 KB Output is correct
20 Correct 350 ms 33260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24940 KB Output is correct
2 Correct 18 ms 24940 KB Output is correct
3 Correct 18 ms 24940 KB Output is correct
4 Correct 18 ms 24940 KB Output is correct
5 Correct 18 ms 24940 KB Output is correct
6 Correct 18 ms 24940 KB Output is correct
7 Correct 18 ms 24940 KB Output is correct
8 Correct 18 ms 24940 KB Output is correct
9 Correct 18 ms 24940 KB Output is correct
10 Correct 19 ms 24940 KB Output is correct
11 Correct 332 ms 33648 KB Output is correct
12 Correct 334 ms 33044 KB Output is correct
13 Correct 363 ms 32280 KB Output is correct
14 Correct 339 ms 32236 KB Output is correct
15 Correct 346 ms 33348 KB Output is correct
16 Correct 368 ms 32492 KB Output is correct
17 Correct 375 ms 33040 KB Output is correct
18 Correct 278 ms 34540 KB Output is correct
19 Correct 307 ms 31724 KB Output is correct
20 Correct 350 ms 33260 KB Output is correct
21 Correct 360 ms 36332 KB Output is correct
22 Correct 368 ms 36204 KB Output is correct
23 Correct 426 ms 35180 KB Output is correct
24 Correct 401 ms 35052 KB Output is correct
25 Correct 376 ms 37100 KB Output is correct
26 Correct 450 ms 35692 KB Output is correct
27 Correct 435 ms 35692 KB Output is correct
28 Correct 302 ms 38176 KB Output is correct
29 Correct 340 ms 33900 KB Output is correct
30 Correct 381 ms 35820 KB Output is correct
31 Correct 413 ms 35436 KB Output is correct
32 Correct 465 ms 35944 KB Output is correct
33 Correct 377 ms 34796 KB Output is correct
34 Correct 428 ms 34540 KB Output is correct
35 Correct 438 ms 35308 KB Output is correct
36 Correct 295 ms 33900 KB Output is correct
37 Correct 346 ms 35692 KB Output is correct
38 Correct 368 ms 33644 KB Output is correct
39 Correct 404 ms 35052 KB Output is correct
40 Correct 404 ms 34924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24940 KB Output is correct
2 Correct 18 ms 24940 KB Output is correct
3 Correct 18 ms 24940 KB Output is correct
4 Correct 18 ms 24940 KB Output is correct
5 Correct 18 ms 24940 KB Output is correct
6 Correct 18 ms 24940 KB Output is correct
7 Correct 18 ms 24940 KB Output is correct
8 Correct 18 ms 24940 KB Output is correct
9 Correct 18 ms 24940 KB Output is correct
10 Correct 19 ms 24940 KB Output is correct
11 Correct 215 ms 51780 KB Output is correct
12 Correct 224 ms 51820 KB Output is correct
13 Correct 239 ms 51820 KB Output is correct
14 Correct 273 ms 51820 KB Output is correct
15 Correct 220 ms 51948 KB Output is correct
16 Correct 280 ms 51820 KB Output is correct
17 Correct 262 ms 51948 KB Output is correct
18 Correct 209 ms 52076 KB Output is correct
19 Correct 216 ms 51940 KB Output is correct
20 Correct 217 ms 52076 KB Output is correct
21 Correct 243 ms 51948 KB Output is correct
22 Correct 271 ms 51972 KB Output is correct
23 Correct 222 ms 51948 KB Output is correct
24 Correct 261 ms 51948 KB Output is correct
25 Correct 265 ms 51948 KB Output is correct
26 Correct 207 ms 51948 KB Output is correct
27 Correct 220 ms 52204 KB Output is correct
28 Correct 217 ms 51820 KB Output is correct
29 Correct 241 ms 51948 KB Output is correct
30 Correct 254 ms 52076 KB Output is correct
31 Correct 227 ms 52304 KB Output is correct
32 Correct 274 ms 52076 KB Output is correct
33 Correct 262 ms 52204 KB Output is correct
34 Correct 206 ms 51948 KB Output is correct
35 Correct 251 ms 52112 KB Output is correct
36 Correct 247 ms 52076 KB Output is correct
37 Correct 249 ms 52076 KB Output is correct
38 Correct 250 ms 52204 KB Output is correct
39 Correct 245 ms 52052 KB Output is correct
40 Correct 245 ms 52076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24940 KB Output is correct
2 Correct 18 ms 24940 KB Output is correct
3 Correct 18 ms 24940 KB Output is correct
4 Correct 18 ms 24940 KB Output is correct
5 Correct 18 ms 24940 KB Output is correct
6 Correct 18 ms 24940 KB Output is correct
7 Correct 18 ms 24940 KB Output is correct
8 Correct 18 ms 24940 KB Output is correct
9 Correct 18 ms 24940 KB Output is correct
10 Correct 19 ms 24940 KB Output is correct
11 Correct 332 ms 33648 KB Output is correct
12 Correct 334 ms 33044 KB Output is correct
13 Correct 363 ms 32280 KB Output is correct
14 Correct 339 ms 32236 KB Output is correct
15 Correct 346 ms 33348 KB Output is correct
16 Correct 368 ms 32492 KB Output is correct
17 Correct 375 ms 33040 KB Output is correct
18 Correct 278 ms 34540 KB Output is correct
19 Correct 307 ms 31724 KB Output is correct
20 Correct 350 ms 33260 KB Output is correct
21 Correct 360 ms 36332 KB Output is correct
22 Correct 368 ms 36204 KB Output is correct
23 Correct 426 ms 35180 KB Output is correct
24 Correct 401 ms 35052 KB Output is correct
25 Correct 376 ms 37100 KB Output is correct
26 Correct 450 ms 35692 KB Output is correct
27 Correct 435 ms 35692 KB Output is correct
28 Correct 302 ms 38176 KB Output is correct
29 Correct 340 ms 33900 KB Output is correct
30 Correct 381 ms 35820 KB Output is correct
31 Correct 413 ms 35436 KB Output is correct
32 Correct 465 ms 35944 KB Output is correct
33 Correct 377 ms 34796 KB Output is correct
34 Correct 428 ms 34540 KB Output is correct
35 Correct 438 ms 35308 KB Output is correct
36 Correct 295 ms 33900 KB Output is correct
37 Correct 346 ms 35692 KB Output is correct
38 Correct 368 ms 33644 KB Output is correct
39 Correct 404 ms 35052 KB Output is correct
40 Correct 404 ms 34924 KB Output is correct
41 Correct 215 ms 51780 KB Output is correct
42 Correct 224 ms 51820 KB Output is correct
43 Correct 239 ms 51820 KB Output is correct
44 Correct 273 ms 51820 KB Output is correct
45 Correct 220 ms 51948 KB Output is correct
46 Correct 280 ms 51820 KB Output is correct
47 Correct 262 ms 51948 KB Output is correct
48 Correct 209 ms 52076 KB Output is correct
49 Correct 216 ms 51940 KB Output is correct
50 Correct 217 ms 52076 KB Output is correct
51 Correct 243 ms 51948 KB Output is correct
52 Correct 271 ms 51972 KB Output is correct
53 Correct 222 ms 51948 KB Output is correct
54 Correct 261 ms 51948 KB Output is correct
55 Correct 265 ms 51948 KB Output is correct
56 Correct 207 ms 51948 KB Output is correct
57 Correct 220 ms 52204 KB Output is correct
58 Correct 217 ms 51820 KB Output is correct
59 Correct 241 ms 51948 KB Output is correct
60 Correct 254 ms 52076 KB Output is correct
61 Correct 227 ms 52304 KB Output is correct
62 Correct 274 ms 52076 KB Output is correct
63 Correct 262 ms 52204 KB Output is correct
64 Correct 206 ms 51948 KB Output is correct
65 Correct 251 ms 52112 KB Output is correct
66 Correct 247 ms 52076 KB Output is correct
67 Correct 249 ms 52076 KB Output is correct
68 Correct 250 ms 52204 KB Output is correct
69 Correct 245 ms 52052 KB Output is correct
70 Correct 245 ms 52076 KB Output is correct
71 Correct 700 ms 55828 KB Output is correct
72 Correct 712 ms 56044 KB Output is correct
73 Correct 1153 ms 54764 KB Output is correct
74 Correct 1753 ms 55276 KB Output is correct
75 Correct 798 ms 56812 KB Output is correct
76 Correct 1865 ms 57436 KB Output is correct
77 Correct 1563 ms 54712 KB Output is correct
78 Correct 526 ms 58476 KB Output is correct
79 Correct 684 ms 52360 KB Output is correct
80 Correct 726 ms 55432 KB Output is correct
81 Correct 1249 ms 55388 KB Output is correct
82 Correct 1836 ms 54536 KB Output is correct
83 Correct 890 ms 53540 KB Output is correct
84 Correct 1556 ms 54508 KB Output is correct
85 Correct 1581 ms 54676 KB Output is correct
86 Correct 534 ms 52460 KB Output is correct
87 Correct 680 ms 55456 KB Output is correct
88 Correct 695 ms 52588 KB Output is correct
89 Correct 1111 ms 54156 KB Output is correct
90 Correct 1338 ms 54380 KB Output is correct
91 Correct 809 ms 53612 KB Output is correct
92 Correct 1861 ms 54764 KB Output is correct
93 Correct 1586 ms 65536 KB Output is correct
94 Correct 515 ms 65536 KB Output is correct
95 Correct 1295 ms 65536 KB Output is correct
96 Correct 1303 ms 65536 KB Output is correct
97 Correct 1295 ms 65536 KB Output is correct
98 Correct 1292 ms 65536 KB Output is correct
99 Correct 1279 ms 65536 KB Output is correct
100 Correct 1291 ms 65536 KB Output is correct