Submission #334264

# Submission time Handle Problem Language Result Execution time Memory
334264 2020-12-08T21:45:22 Z ChrisT Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 42476 KB
#include <bits/stdc++.h>
using namespace std;
int val[1 << 20], sum1[1 << 20], sum2[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)];
		}
	}
	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
			vector<int> pos; pos.reserve(cntq);
			int mask = 0, ret = 0;
			for (int i = 0; i < n; i++) {
				if (str[i] == '?') pos.push_back(i);
				mask |= (str[i] == '1') << i;
			}
			vector<int> nmask(1 << cntq); nmask[0] = mask;
			for (int qmask = 0; qmask < (1 << cntq); qmask++) {
				if (qmask) {
					int bit = __builtin_ctz(qmask);
					nmask[qmask] = nmask[qmask ^ (1 << bit)] | (1 << pos[bit]);
				}
				ret += val[nmask[qmask]];
			}
			printf ("%d\n",ret);
		} else if (cnt0 <= cnt1) { //PIE
			vector<int> pos; pos.reserve(cnt0);
			int mask = 0, ret = 0;
			for (int i = 0; i < n; i++) {
				if (str[i] == '0') pos.push_back(i);
				mask |= (str[i] != '1') << i;
			}
			vector<int> nmask(1 << cnt0); nmask[0] = mask;
			for (int mask0 = 0; mask0 < (1 << cnt0); mask0++) {
				if (mask0) {
					int bit = __builtin_ctz(mask0);
					nmask[mask0] = nmask[mask0 ^ (1 << bit)] ^ (1 << pos[bit]);
				}
				ret += (__builtin_popcount(mask0) & 1 ? -1 : 1) * sum2[nmask[mask0]];
			}
			printf ("%d\n",ret);
		} else { //'' ''
			vector<int> pos; pos.reserve(cnt1);
			int mask = 0, ret = 0;
			for (int i = 0; i < n; i++) {
				if (str[i] == '1') pos.push_back(i);
				mask |= (str[i] != '0') << i;
			}
			vector<int> nmask(1 << cnt1); nmask[0] = mask;
			for (int mask1 = 0; mask1 < (1 << cnt1); mask1++) {
				if (mask1) {
					int bit = __builtin_ctz(mask1);
					nmask[mask1] = nmask[mask1 ^ (1 << bit)] ^ (1 << pos[bit]);
				}
				ret += (__builtin_popcount(mask1) & 1 ? -1 : 1) * sum1[nmask[mask1]];
			}
			printf ("%d\n",ret);
		}
	}
    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:7:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    7 |  scanf ("%d %d",&n,&q);
      |  ~~~~~~^~~~~~~~~~~~~~~
snake_escaping.cpp:9:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    9 |   char ch; scanf (" %c",&ch);
      |            ~~~~~~^~~~~~~~~~~
snake_escaping.cpp:19:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   19 |   scanf ("%s",str);
      |   ~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 380 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 380 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 335 ms 6380 KB Output is correct
12 Correct 345 ms 5868 KB Output is correct
13 Correct 400 ms 4972 KB Output is correct
14 Correct 424 ms 5320 KB Output is correct
15 Correct 384 ms 6252 KB Output is correct
16 Correct 454 ms 5228 KB Output is correct
17 Correct 457 ms 5248 KB Output is correct
18 Correct 300 ms 7148 KB Output is correct
19 Correct 317 ms 4288 KB Output is correct
20 Correct 357 ms 5740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 380 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 335 ms 6380 KB Output is correct
12 Correct 345 ms 5868 KB Output is correct
13 Correct 400 ms 4972 KB Output is correct
14 Correct 424 ms 5320 KB Output is correct
15 Correct 384 ms 6252 KB Output is correct
16 Correct 454 ms 5228 KB Output is correct
17 Correct 457 ms 5248 KB Output is correct
18 Correct 300 ms 7148 KB Output is correct
19 Correct 317 ms 4288 KB Output is correct
20 Correct 357 ms 5740 KB Output is correct
21 Correct 370 ms 7660 KB Output is correct
22 Correct 385 ms 7916 KB Output is correct
23 Correct 499 ms 7020 KB Output is correct
24 Correct 509 ms 6904 KB Output is correct
25 Correct 434 ms 8812 KB Output is correct
26 Correct 538 ms 6892 KB Output is correct
27 Correct 524 ms 6892 KB Output is correct
28 Correct 318 ms 9452 KB Output is correct
29 Correct 370 ms 5228 KB Output is correct
30 Correct 399 ms 7404 KB Output is correct
31 Correct 494 ms 7148 KB Output is correct
32 Correct 580 ms 7404 KB Output is correct
33 Correct 428 ms 5868 KB Output is correct
34 Correct 527 ms 6124 KB Output is correct
35 Correct 523 ms 6508 KB Output is correct
36 Correct 337 ms 4716 KB Output is correct
37 Correct 362 ms 6636 KB Output is correct
38 Correct 379 ms 4716 KB Output is correct
39 Correct 492 ms 5868 KB Output is correct
40 Correct 505 ms 5612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 380 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 131 ms 14188 KB Output is correct
12 Correct 129 ms 14060 KB Output is correct
13 Correct 152 ms 13932 KB Output is correct
14 Correct 180 ms 13932 KB Output is correct
15 Correct 134 ms 14060 KB Output is correct
16 Correct 191 ms 14188 KB Output is correct
17 Correct 178 ms 13932 KB Output is correct
18 Correct 122 ms 14188 KB Output is correct
19 Correct 129 ms 13804 KB Output is correct
20 Correct 129 ms 13932 KB Output is correct
21 Correct 154 ms 13932 KB Output is correct
22 Correct 174 ms 13804 KB Output is correct
23 Correct 134 ms 13804 KB Output is correct
24 Correct 176 ms 13804 KB Output is correct
25 Correct 176 ms 13932 KB Output is correct
26 Correct 124 ms 13804 KB Output is correct
27 Correct 129 ms 13932 KB Output is correct
28 Correct 134 ms 13804 KB Output is correct
29 Correct 146 ms 13804 KB Output is correct
30 Correct 154 ms 13932 KB Output is correct
31 Correct 136 ms 13676 KB Output is correct
32 Correct 194 ms 13676 KB Output is correct
33 Correct 177 ms 13676 KB Output is correct
34 Correct 118 ms 13736 KB Output is correct
35 Correct 165 ms 13804 KB Output is correct
36 Correct 158 ms 13676 KB Output is correct
37 Correct 161 ms 13676 KB Output is correct
38 Correct 160 ms 13676 KB Output is correct
39 Correct 162 ms 13676 KB Output is correct
40 Correct 164 ms 13760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 380 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 335 ms 6380 KB Output is correct
12 Correct 345 ms 5868 KB Output is correct
13 Correct 400 ms 4972 KB Output is correct
14 Correct 424 ms 5320 KB Output is correct
15 Correct 384 ms 6252 KB Output is correct
16 Correct 454 ms 5228 KB Output is correct
17 Correct 457 ms 5248 KB Output is correct
18 Correct 300 ms 7148 KB Output is correct
19 Correct 317 ms 4288 KB Output is correct
20 Correct 357 ms 5740 KB Output is correct
21 Correct 370 ms 7660 KB Output is correct
22 Correct 385 ms 7916 KB Output is correct
23 Correct 499 ms 7020 KB Output is correct
24 Correct 509 ms 6904 KB Output is correct
25 Correct 434 ms 8812 KB Output is correct
26 Correct 538 ms 6892 KB Output is correct
27 Correct 524 ms 6892 KB Output is correct
28 Correct 318 ms 9452 KB Output is correct
29 Correct 370 ms 5228 KB Output is correct
30 Correct 399 ms 7404 KB Output is correct
31 Correct 494 ms 7148 KB Output is correct
32 Correct 580 ms 7404 KB Output is correct
33 Correct 428 ms 5868 KB Output is correct
34 Correct 527 ms 6124 KB Output is correct
35 Correct 523 ms 6508 KB Output is correct
36 Correct 337 ms 4716 KB Output is correct
37 Correct 362 ms 6636 KB Output is correct
38 Correct 379 ms 4716 KB Output is correct
39 Correct 492 ms 5868 KB Output is correct
40 Correct 505 ms 5612 KB Output is correct
41 Correct 131 ms 14188 KB Output is correct
42 Correct 129 ms 14060 KB Output is correct
43 Correct 152 ms 13932 KB Output is correct
44 Correct 180 ms 13932 KB Output is correct
45 Correct 134 ms 14060 KB Output is correct
46 Correct 191 ms 14188 KB Output is correct
47 Correct 178 ms 13932 KB Output is correct
48 Correct 122 ms 14188 KB Output is correct
49 Correct 129 ms 13804 KB Output is correct
50 Correct 129 ms 13932 KB Output is correct
51 Correct 154 ms 13932 KB Output is correct
52 Correct 174 ms 13804 KB Output is correct
53 Correct 134 ms 13804 KB Output is correct
54 Correct 176 ms 13804 KB Output is correct
55 Correct 176 ms 13932 KB Output is correct
56 Correct 124 ms 13804 KB Output is correct
57 Correct 129 ms 13932 KB Output is correct
58 Correct 134 ms 13804 KB Output is correct
59 Correct 146 ms 13804 KB Output is correct
60 Correct 154 ms 13932 KB Output is correct
61 Correct 136 ms 13676 KB Output is correct
62 Correct 194 ms 13676 KB Output is correct
63 Correct 177 ms 13676 KB Output is correct
64 Correct 118 ms 13736 KB Output is correct
65 Correct 165 ms 13804 KB Output is correct
66 Correct 158 ms 13676 KB Output is correct
67 Correct 161 ms 13676 KB Output is correct
68 Correct 160 ms 13676 KB Output is correct
69 Correct 162 ms 13676 KB Output is correct
70 Correct 164 ms 13760 KB Output is correct
71 Correct 677 ms 17988 KB Output is correct
72 Correct 667 ms 18028 KB Output is correct
73 Correct 1095 ms 16620 KB Output is correct
74 Correct 1639 ms 16704 KB Output is correct
75 Correct 813 ms 18804 KB Output is correct
76 Correct 1907 ms 19268 KB Output is correct
77 Correct 1646 ms 38764 KB Output is correct
78 Correct 518 ms 42476 KB Output is correct
79 Correct 661 ms 36332 KB Output is correct
80 Correct 704 ms 39660 KB Output is correct
81 Correct 1156 ms 39532 KB Output is correct
82 Correct 1641 ms 38460 KB Output is correct
83 Correct 767 ms 37484 KB Output is correct
84 Correct 1638 ms 38376 KB Output is correct
85 Correct 1660 ms 38764 KB Output is correct
86 Correct 546 ms 36332 KB Output is correct
87 Correct 707 ms 39276 KB Output is correct
88 Correct 726 ms 36460 KB Output is correct
89 Correct 1086 ms 38248 KB Output is correct
90 Correct 1237 ms 38252 KB Output is correct
91 Correct 807 ms 37484 KB Output is correct
92 Execution timed out 2033 ms 38880 KB Time limit exceeded
93 Halted 0 ms 0 KB -