#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
int dp[2][1 << 20];
char str[1 << 20];
int bit[20], sz;
int ans;
void bktQ(int p, int cnf) {
if(p == sz) {
ans += str[cnf] - '0';
}
else {
bktQ(p + 1, cnf);
bktQ(p + 1, cnf + (1 << bit[p]));
}
}
bool type;
void bkt(int p, int cnf, int cnt) {
if(p == sz) {
if(cnt & 1) {
ans -= dp[type][cnf];
}
else {
ans += dp[type][cnf];
}
}
else {
bkt(p + 1, cnf, cnt);
bkt(p + 1, cnf ^ (1 << bit[p]), cnt + 1);
}
}
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, l, q;
//ios::sync_with_stdio(false);
//cin.tie(0), cout.tie(0);
scanf("%d%d%s" ,&l,&q,str);
for(int conf = 0; conf < (1 << l); conf++) {
dp[0][conf ^ ((1 << l) - 1)] = str[conf] - '0';
dp[1][conf] = str[conf] - '0';
}
for(i = 0; i < 2; i++) {
for(int bit = 0; bit < l; bit++) {
for(int conf = 0; conf < (1 << (l - 1)); conf++) {
int cur = (conf & ((1 << bit) - 1)) + (1 << bit) + ((conf >> bit) << (bit + 1));
dp[i][cur] += dp[i][cur ^ (1 << bit)];
}
}
}
while(q > 0) {
q--;
static char cur[20];
scanf("%s" ,cur);
reverse(cur, cur + l);
int numq = 0, num0 = 0, num1 = 0;
for(i = 0; i < l; i++) {
if(cur[i] == '0') {
num0++;
}
else if(cur[i] == '1') {
num1++;
}
else {
numq++;
}
}
int mn = min(numq, min(num0, num1));
int cnf = 0;
ans = sz = 0;
if(numq == mn) {
for(i = 0; i < l; i++) {
if(cur[i] == '?') {
bit[sz++] = i;
}
else {
cnf += (1 << i) * (cur[i] == '1');
}
}
bktQ(0, cnf);
}
else if(num0 == mn) {
for(i = 0; i < l; i++) {
if(cur[i] == '0') {
bit[sz++] = i;
}
if(cur[i] != '1') {
cnf += (1 << i);
}
}
type = 0;
bkt(0, cnf, 0);
}
else if(num1 == mn) {
for(i = 0; i < l; i++) {
if(cur[i] == '1') {
bit[sz++] = i;
}
if(cur[i] != '0') {
cnf += (1 << i);
}
}
type = 1;
bkt(0, cnf, 0);
}
printf("%d\n" ,ans);
}
//cin.close();
//cout.close();
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%s" ,&l,&q,str);
~~~~~^~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s" ,cur);
~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
300 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
428 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
300 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
428 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
292 ms |
6428 KB |
Output is correct |
12 |
Correct |
346 ms |
6168 KB |
Output is correct |
13 |
Correct |
339 ms |
5660 KB |
Output is correct |
14 |
Correct |
348 ms |
5580 KB |
Output is correct |
15 |
Correct |
356 ms |
6520 KB |
Output is correct |
16 |
Correct |
414 ms |
5624 KB |
Output is correct |
17 |
Correct |
417 ms |
5752 KB |
Output is correct |
18 |
Correct |
261 ms |
7416 KB |
Output is correct |
19 |
Correct |
250 ms |
4472 KB |
Output is correct |
20 |
Correct |
331 ms |
6136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
300 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
428 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
292 ms |
6428 KB |
Output is correct |
12 |
Correct |
346 ms |
6168 KB |
Output is correct |
13 |
Correct |
339 ms |
5660 KB |
Output is correct |
14 |
Correct |
348 ms |
5580 KB |
Output is correct |
15 |
Correct |
356 ms |
6520 KB |
Output is correct |
16 |
Correct |
414 ms |
5624 KB |
Output is correct |
17 |
Correct |
417 ms |
5752 KB |
Output is correct |
18 |
Correct |
261 ms |
7416 KB |
Output is correct |
19 |
Correct |
250 ms |
4472 KB |
Output is correct |
20 |
Correct |
331 ms |
6136 KB |
Output is correct |
21 |
Correct |
468 ms |
6684 KB |
Output is correct |
22 |
Correct |
364 ms |
6796 KB |
Output is correct |
23 |
Correct |
409 ms |
5752 KB |
Output is correct |
24 |
Correct |
439 ms |
5744 KB |
Output is correct |
25 |
Correct |
377 ms |
7416 KB |
Output is correct |
26 |
Correct |
536 ms |
5880 KB |
Output is correct |
27 |
Correct |
435 ms |
5756 KB |
Output is correct |
28 |
Correct |
277 ms |
8312 KB |
Output is correct |
29 |
Correct |
380 ms |
4268 KB |
Output is correct |
30 |
Correct |
403 ms |
6360 KB |
Output is correct |
31 |
Correct |
489 ms |
6412 KB |
Output is correct |
32 |
Correct |
552 ms |
6404 KB |
Output is correct |
33 |
Correct |
429 ms |
5148 KB |
Output is correct |
34 |
Correct |
563 ms |
5368 KB |
Output is correct |
35 |
Correct |
516 ms |
5720 KB |
Output is correct |
36 |
Correct |
307 ms |
4308 KB |
Output is correct |
37 |
Correct |
414 ms |
6392 KB |
Output is correct |
38 |
Correct |
347 ms |
4292 KB |
Output is correct |
39 |
Correct |
439 ms |
5544 KB |
Output is correct |
40 |
Correct |
458 ms |
5372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
300 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
428 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
114 ms |
11116 KB |
Output is correct |
12 |
Correct |
113 ms |
11164 KB |
Output is correct |
13 |
Correct |
113 ms |
11188 KB |
Output is correct |
14 |
Correct |
172 ms |
11000 KB |
Output is correct |
15 |
Correct |
130 ms |
11128 KB |
Output is correct |
16 |
Correct |
163 ms |
11128 KB |
Output is correct |
17 |
Correct |
140 ms |
11040 KB |
Output is correct |
18 |
Correct |
82 ms |
11256 KB |
Output is correct |
19 |
Correct |
106 ms |
10920 KB |
Output is correct |
20 |
Correct |
105 ms |
11128 KB |
Output is correct |
21 |
Correct |
133 ms |
11128 KB |
Output is correct |
22 |
Correct |
172 ms |
11076 KB |
Output is correct |
23 |
Correct |
136 ms |
11128 KB |
Output is correct |
24 |
Correct |
174 ms |
11128 KB |
Output is correct |
25 |
Correct |
138 ms |
11132 KB |
Output is correct |
26 |
Correct |
86 ms |
10908 KB |
Output is correct |
27 |
Correct |
96 ms |
11124 KB |
Output is correct |
28 |
Correct |
98 ms |
10908 KB |
Output is correct |
29 |
Correct |
137 ms |
11000 KB |
Output is correct |
30 |
Correct |
118 ms |
10992 KB |
Output is correct |
31 |
Correct |
100 ms |
11040 KB |
Output is correct |
32 |
Correct |
198 ms |
11128 KB |
Output is correct |
33 |
Correct |
130 ms |
11128 KB |
Output is correct |
34 |
Correct |
82 ms |
10904 KB |
Output is correct |
35 |
Correct |
106 ms |
11000 KB |
Output is correct |
36 |
Correct |
138 ms |
11116 KB |
Output is correct |
37 |
Correct |
143 ms |
11092 KB |
Output is correct |
38 |
Correct |
132 ms |
11000 KB |
Output is correct |
39 |
Correct |
135 ms |
11128 KB |
Output is correct |
40 |
Correct |
125 ms |
11032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
300 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
428 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
292 ms |
6428 KB |
Output is correct |
12 |
Correct |
346 ms |
6168 KB |
Output is correct |
13 |
Correct |
339 ms |
5660 KB |
Output is correct |
14 |
Correct |
348 ms |
5580 KB |
Output is correct |
15 |
Correct |
356 ms |
6520 KB |
Output is correct |
16 |
Correct |
414 ms |
5624 KB |
Output is correct |
17 |
Correct |
417 ms |
5752 KB |
Output is correct |
18 |
Correct |
261 ms |
7416 KB |
Output is correct |
19 |
Correct |
250 ms |
4472 KB |
Output is correct |
20 |
Correct |
331 ms |
6136 KB |
Output is correct |
21 |
Correct |
468 ms |
6684 KB |
Output is correct |
22 |
Correct |
364 ms |
6796 KB |
Output is correct |
23 |
Correct |
409 ms |
5752 KB |
Output is correct |
24 |
Correct |
439 ms |
5744 KB |
Output is correct |
25 |
Correct |
377 ms |
7416 KB |
Output is correct |
26 |
Correct |
536 ms |
5880 KB |
Output is correct |
27 |
Correct |
435 ms |
5756 KB |
Output is correct |
28 |
Correct |
277 ms |
8312 KB |
Output is correct |
29 |
Correct |
380 ms |
4268 KB |
Output is correct |
30 |
Correct |
403 ms |
6360 KB |
Output is correct |
31 |
Correct |
489 ms |
6412 KB |
Output is correct |
32 |
Correct |
552 ms |
6404 KB |
Output is correct |
33 |
Correct |
429 ms |
5148 KB |
Output is correct |
34 |
Correct |
563 ms |
5368 KB |
Output is correct |
35 |
Correct |
516 ms |
5720 KB |
Output is correct |
36 |
Correct |
307 ms |
4308 KB |
Output is correct |
37 |
Correct |
414 ms |
6392 KB |
Output is correct |
38 |
Correct |
347 ms |
4292 KB |
Output is correct |
39 |
Correct |
439 ms |
5544 KB |
Output is correct |
40 |
Correct |
458 ms |
5372 KB |
Output is correct |
41 |
Correct |
114 ms |
11116 KB |
Output is correct |
42 |
Correct |
113 ms |
11164 KB |
Output is correct |
43 |
Correct |
113 ms |
11188 KB |
Output is correct |
44 |
Correct |
172 ms |
11000 KB |
Output is correct |
45 |
Correct |
130 ms |
11128 KB |
Output is correct |
46 |
Correct |
163 ms |
11128 KB |
Output is correct |
47 |
Correct |
140 ms |
11040 KB |
Output is correct |
48 |
Correct |
82 ms |
11256 KB |
Output is correct |
49 |
Correct |
106 ms |
10920 KB |
Output is correct |
50 |
Correct |
105 ms |
11128 KB |
Output is correct |
51 |
Correct |
133 ms |
11128 KB |
Output is correct |
52 |
Correct |
172 ms |
11076 KB |
Output is correct |
53 |
Correct |
136 ms |
11128 KB |
Output is correct |
54 |
Correct |
174 ms |
11128 KB |
Output is correct |
55 |
Correct |
138 ms |
11132 KB |
Output is correct |
56 |
Correct |
86 ms |
10908 KB |
Output is correct |
57 |
Correct |
96 ms |
11124 KB |
Output is correct |
58 |
Correct |
98 ms |
10908 KB |
Output is correct |
59 |
Correct |
137 ms |
11000 KB |
Output is correct |
60 |
Correct |
118 ms |
10992 KB |
Output is correct |
61 |
Correct |
100 ms |
11040 KB |
Output is correct |
62 |
Correct |
198 ms |
11128 KB |
Output is correct |
63 |
Correct |
130 ms |
11128 KB |
Output is correct |
64 |
Correct |
82 ms |
10904 KB |
Output is correct |
65 |
Correct |
106 ms |
11000 KB |
Output is correct |
66 |
Correct |
138 ms |
11116 KB |
Output is correct |
67 |
Correct |
143 ms |
11092 KB |
Output is correct |
68 |
Correct |
132 ms |
11000 KB |
Output is correct |
69 |
Correct |
135 ms |
11128 KB |
Output is correct |
70 |
Correct |
125 ms |
11032 KB |
Output is correct |
71 |
Correct |
713 ms |
14656 KB |
Output is correct |
72 |
Correct |
659 ms |
14712 KB |
Output is correct |
73 |
Correct |
1000 ms |
13432 KB |
Output is correct |
74 |
Execution timed out |
2041 ms |
16096 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |