Submission #125624

# Submission time Handle Problem Language Result Execution time Memory
125624 2019-07-06T05:34:04 Z gs14004 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1169 ms 43256 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<lint, lint> pi;
const int MAXN = (1<<20) + 5;

int n, q;
int cnt1[MAXN];
int cnt2[MAXN];
int pcnt[MAXN];
char str[MAXN];

char buf[25];

int main(){
	scanf("%d %d",&n,&q);
	scanf("%s", str);
	for(int i=0; i<(1<<n); i++){
		if(i) pcnt[i] = pcnt[i-(i&-i)] + 1;
		cnt1[i ^ ((1<<n) - 1)] += str[i] - '0';
		cnt2[i] += str[i] - '0';
	}
	for(int i=0; i<n; i++){
		for(int j=0; j<(1<<n); j++){
			if((j >> i) % 2 == 0){
				cnt1[j] += cnt1[j | (1<<i)];
				cnt2[j] += cnt2[j | (1<<i)];
			}
		}
	}
	for(int i=0; i<q; i++){
		scanf("%s", buf);
		reverse(buf, buf + n);
		int cnt[3] = {};
		for(int j=0; buf[j]; j++){
			if(buf[j] == '?') cnt[2]++;
			else cnt[buf[j] - '0']++;
		}
		int ans = 0;
		if(cnt[0] <= n/3){
			int msk = 0, tmsk = 0;
			for(int j=0; buf[j]; j++){
				if(buf[j] == '0') tmsk |= 1<<j;
				if(buf[j] == '1') msk |= 1<<j;
			}
			ans += cnt2[msk];
			for(int i=tmsk; i>0; i=(i-1)&tmsk){
				ans += cnt2[i | msk] * (pcnt[i] % 2 ? -1 : 1);
			}
		}
		else if(cnt[1] <= n/3){
			int msk = 0, tmsk = 0;
			for(int j=0; buf[j]; j++){
				if(buf[j] == '0') msk |= 1<<j;
				if(buf[j] == '1') tmsk |= 1<<j;
			}
			ans += cnt1[msk];
			for(int i=tmsk; i>0; i=(i-1)&tmsk){
				ans += cnt1[i | msk] * (pcnt[i] % 2 ? -1 : 1);
			}
		}
		else if(cnt[2] <= n/3){
			int msk = 0, tmsk = 0;
			for(int j=0; buf[j]; j++){
				if(buf[j] == '1') msk |= 1<<j;
				if(buf[j] == '?') tmsk |= 1<<j;
			}
			ans += str[msk] - '0';
			for(int i=tmsk; i>0; i=(i-1)&tmsk){
				ans += str[i | msk] - '0';
			}
		}
		printf("%d\n", ans);
	}
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&q);
  ~~~~~^~~~~~~~~~~~~~~
snake_escaping.cpp:17:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", str);
  ~~~~~^~~~~~~~~~~
snake_escaping.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", buf);
   ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 424 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 424 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 288 ms 15116 KB Output is correct
12 Correct 363 ms 14752 KB Output is correct
13 Correct 352 ms 14124 KB Output is correct
14 Correct 330 ms 14120 KB Output is correct
15 Correct 327 ms 15148 KB Output is correct
16 Correct 352 ms 14448 KB Output is correct
17 Correct 373 ms 14300 KB Output is correct
18 Correct 243 ms 16072 KB Output is correct
19 Correct 298 ms 13252 KB Output is correct
20 Correct 308 ms 14804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 424 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 288 ms 15116 KB Output is correct
12 Correct 363 ms 14752 KB Output is correct
13 Correct 352 ms 14124 KB Output is correct
14 Correct 330 ms 14120 KB Output is correct
15 Correct 327 ms 15148 KB Output is correct
16 Correct 352 ms 14448 KB Output is correct
17 Correct 373 ms 14300 KB Output is correct
18 Correct 243 ms 16072 KB Output is correct
19 Correct 298 ms 13252 KB Output is correct
20 Correct 308 ms 14804 KB Output is correct
21 Correct 376 ms 18208 KB Output is correct
22 Correct 450 ms 18296 KB Output is correct
23 Correct 452 ms 17320 KB Output is correct
24 Correct 451 ms 17236 KB Output is correct
25 Correct 402 ms 19276 KB Output is correct
26 Correct 464 ms 17688 KB Output is correct
27 Correct 463 ms 17656 KB Output is correct
28 Correct 268 ms 20232 KB Output is correct
29 Correct 367 ms 16120 KB Output is correct
30 Correct 392 ms 18424 KB Output is correct
31 Correct 446 ms 18296 KB Output is correct
32 Correct 453 ms 18168 KB Output is correct
33 Correct 399 ms 17060 KB Output is correct
34 Correct 473 ms 17272 KB Output is correct
35 Correct 463 ms 17620 KB Output is correct
36 Correct 279 ms 16248 KB Output is correct
37 Correct 426 ms 18100 KB Output is correct
38 Correct 389 ms 16120 KB Output is correct
39 Correct 435 ms 17400 KB Output is correct
40 Correct 441 ms 17400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 424 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 73 ms 15992 KB Output is correct
12 Correct 80 ms 15992 KB Output is correct
13 Correct 83 ms 15864 KB Output is correct
14 Correct 89 ms 15888 KB Output is correct
15 Correct 76 ms 15992 KB Output is correct
16 Correct 89 ms 15932 KB Output is correct
17 Correct 103 ms 16052 KB Output is correct
18 Correct 71 ms 16112 KB Output is correct
19 Correct 79 ms 15992 KB Output is correct
20 Correct 73 ms 15996 KB Output is correct
21 Correct 81 ms 15992 KB Output is correct
22 Correct 87 ms 15992 KB Output is correct
23 Correct 77 ms 15848 KB Output is correct
24 Correct 89 ms 15864 KB Output is correct
25 Correct 93 ms 15852 KB Output is correct
26 Correct 68 ms 15740 KB Output is correct
27 Correct 76 ms 15992 KB Output is correct
28 Correct 77 ms 15744 KB Output is correct
29 Correct 89 ms 15864 KB Output is correct
30 Correct 80 ms 15864 KB Output is correct
31 Correct 76 ms 15856 KB Output is correct
32 Correct 91 ms 15992 KB Output is correct
33 Correct 90 ms 15964 KB Output is correct
34 Correct 64 ms 15864 KB Output is correct
35 Correct 87 ms 15992 KB Output is correct
36 Correct 86 ms 15992 KB Output is correct
37 Correct 84 ms 15992 KB Output is correct
38 Correct 87 ms 15992 KB Output is correct
39 Correct 87 ms 15992 KB Output is correct
40 Correct 85 ms 15864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 424 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 288 ms 15116 KB Output is correct
12 Correct 363 ms 14752 KB Output is correct
13 Correct 352 ms 14124 KB Output is correct
14 Correct 330 ms 14120 KB Output is correct
15 Correct 327 ms 15148 KB Output is correct
16 Correct 352 ms 14448 KB Output is correct
17 Correct 373 ms 14300 KB Output is correct
18 Correct 243 ms 16072 KB Output is correct
19 Correct 298 ms 13252 KB Output is correct
20 Correct 308 ms 14804 KB Output is correct
21 Correct 376 ms 18208 KB Output is correct
22 Correct 450 ms 18296 KB Output is correct
23 Correct 452 ms 17320 KB Output is correct
24 Correct 451 ms 17236 KB Output is correct
25 Correct 402 ms 19276 KB Output is correct
26 Correct 464 ms 17688 KB Output is correct
27 Correct 463 ms 17656 KB Output is correct
28 Correct 268 ms 20232 KB Output is correct
29 Correct 367 ms 16120 KB Output is correct
30 Correct 392 ms 18424 KB Output is correct
31 Correct 446 ms 18296 KB Output is correct
32 Correct 453 ms 18168 KB Output is correct
33 Correct 399 ms 17060 KB Output is correct
34 Correct 473 ms 17272 KB Output is correct
35 Correct 463 ms 17620 KB Output is correct
36 Correct 279 ms 16248 KB Output is correct
37 Correct 426 ms 18100 KB Output is correct
38 Correct 389 ms 16120 KB Output is correct
39 Correct 435 ms 17400 KB Output is correct
40 Correct 441 ms 17400 KB Output is correct
41 Correct 73 ms 15992 KB Output is correct
42 Correct 80 ms 15992 KB Output is correct
43 Correct 83 ms 15864 KB Output is correct
44 Correct 89 ms 15888 KB Output is correct
45 Correct 76 ms 15992 KB Output is correct
46 Correct 89 ms 15932 KB Output is correct
47 Correct 103 ms 16052 KB Output is correct
48 Correct 71 ms 16112 KB Output is correct
49 Correct 79 ms 15992 KB Output is correct
50 Correct 73 ms 15996 KB Output is correct
51 Correct 81 ms 15992 KB Output is correct
52 Correct 87 ms 15992 KB Output is correct
53 Correct 77 ms 15848 KB Output is correct
54 Correct 89 ms 15864 KB Output is correct
55 Correct 93 ms 15852 KB Output is correct
56 Correct 68 ms 15740 KB Output is correct
57 Correct 76 ms 15992 KB Output is correct
58 Correct 77 ms 15744 KB Output is correct
59 Correct 89 ms 15864 KB Output is correct
60 Correct 80 ms 15864 KB Output is correct
61 Correct 76 ms 15856 KB Output is correct
62 Correct 91 ms 15992 KB Output is correct
63 Correct 90 ms 15964 KB Output is correct
64 Correct 64 ms 15864 KB Output is correct
65 Correct 87 ms 15992 KB Output is correct
66 Correct 86 ms 15992 KB Output is correct
67 Correct 84 ms 15992 KB Output is correct
68 Correct 87 ms 15992 KB Output is correct
69 Correct 87 ms 15992 KB Output is correct
70 Correct 85 ms 15864 KB Output is correct
71 Correct 584 ms 40172 KB Output is correct
72 Correct 719 ms 40540 KB Output is correct
73 Correct 778 ms 39032 KB Output is correct
74 Correct 892 ms 39304 KB Output is correct
75 Correct 641 ms 41244 KB Output is correct
76 Correct 920 ms 39668 KB Output is correct
77 Correct 963 ms 39580 KB Output is correct
78 Correct 405 ms 43256 KB Output is correct
79 Correct 557 ms 37112 KB Output is correct
80 Correct 603 ms 40308 KB Output is correct
81 Correct 767 ms 40408 KB Output is correct
82 Correct 933 ms 39412 KB Output is correct
83 Correct 619 ms 38456 KB Output is correct
84 Correct 1012 ms 39332 KB Output is correct
85 Correct 1020 ms 39508 KB Output is correct
86 Correct 481 ms 37356 KB Output is correct
87 Correct 779 ms 40240 KB Output is correct
88 Correct 757 ms 37272 KB Output is correct
89 Correct 881 ms 39012 KB Output is correct
90 Correct 804 ms 39320 KB Output is correct
91 Correct 767 ms 38420 KB Output is correct
92 Correct 1169 ms 39612 KB Output is correct
93 Correct 1071 ms 39512 KB Output is correct
94 Correct 476 ms 37244 KB Output is correct
95 Correct 986 ms 39400 KB Output is correct
96 Correct 981 ms 39480 KB Output is correct
97 Correct 1006 ms 39416 KB Output is correct
98 Correct 992 ms 39336 KB Output is correct
99 Correct 992 ms 39516 KB Output is correct
100 Correct 1072 ms 39440 KB Output is correct