#include "bits/stdc++.h"
#define F first
#define S second
#define ll long long
#define pii pair<ll,int>
const int mxN = (1 << 20) + 5;
const int mod = 1e9 + 7;
using namespace std;
int sos[mxN][2][2];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q;
cin >>n>>q;
string s;
cin>>s;
for(int i = 0;i < n;i++){
for(int mask = 0;mask < (1 << n);mask++){
if(!i){
sos[mask][0][0] = s[mask] - '0';
if((mask &1)) sos[mask][0][0] += s[mask - 1] - '0';
sos[mask][0][1] = s[mask] - '0';
if((mask &1) == 0) sos[mask][0][1] += s[mask + 1] - '0';
continue;
}
sos[mask][i &1][0] = sos[mask][(i - 1)&1][0];
sos[mask][i &1][1] = sos[mask][(i - 1)&1][1];
if(mask >> i & 1) sos[mask][(i)&1][0] += sos[mask - (1 << i)][(i - 1)&1][0];
else sos[mask][i &1][1] += sos[mask + (1 << i)][(i - 1)&1][1];
}
}
while(q--){
string a;
cin >>a;
reverse(a.begin(),a.end());
int Qcnt = 0, Ocnt = 0, Icnt = 0;
for(auto x : a){
if(x == '?') Qcnt++;
else if(x == '1') Icnt++;
else Ocnt++;
}
int ans = 0;
if(min({Qcnt,Ocnt,Icnt}) == Qcnt){
// printf("? small\n");
int m1 = 0,m2 = 0,m3 = 0;
int i = 0;
for(auto x : a){
if(x == '1') m1 += (1 << i);
if(x == '?') m2 += (1 << i);
i++;
}
m3 = m2;
while(m3 > 0){
ans += s[m3 + m1] - '0';
m3 = (m3 - 1) & m2;
}
ans += s[m1] - '0';
}else if(min({Qcnt,Ocnt,Icnt}) == Icnt){
// printf("1 small\n");
int m1 = 0,m2 = 0,m3 = 0;
int i = 0;
for(auto x : a){
if(x == '?') m1 += (1 << i);
if(x == '1') m2 += (1 << i);
i++;
}
m3 = m2;
while(m3 > 0){
if(((Icnt + Qcnt) &1) == (__builtin_popcount(m3 + m1) & 1)) ans += sos[m3 + m1][(n - 1) & 1][0];
else ans -= sos[m3 + m1][(n - 1) & 1][0];
m3 = (m3 - 1) & m2;
}
if(((Icnt + Qcnt) & 1) == (__builtin_popcount(m1) & 1)) ans += sos[m1][(n - 1) & 1][0];
else ans -= sos[m1][(n - 1) & 1][0];
}else{
// printf("0 small\n");
int m1 = 0,m2 = 0,m3 = 0;
int i = 0;
for(auto x : a){
if(x == '1') m1 += (1 << i);
if(x == '0') m2 += (1 << i);
i++;
}
m3 = m2;
while(m3 > 0){
if((Icnt & 1) == (__builtin_popcount(m3 + m1) &1)) ans += sos[m3 + m1][(n - 1) & 1][1];
else ans -= sos[m3 + m1][(n - 1) & 1][1];
m3 = (m3 - 1) & m2;
}
if((Icnt & 1) == (__builtin_popcount(m1) &1)) ans += sos[m1][(n - 1) & 1][1];
else ans -= sos[m1][(n - 1) & 1][1];
}
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
209 ms |
4424 KB |
Output is correct |
12 |
Correct |
176 ms |
3988 KB |
Output is correct |
13 |
Correct |
215 ms |
3300 KB |
Output is correct |
14 |
Correct |
194 ms |
3400 KB |
Output is correct |
15 |
Correct |
210 ms |
4332 KB |
Output is correct |
16 |
Correct |
243 ms |
3452 KB |
Output is correct |
17 |
Correct |
274 ms |
3540 KB |
Output is correct |
18 |
Correct |
139 ms |
5988 KB |
Output is correct |
19 |
Correct |
163 ms |
2448 KB |
Output is correct |
20 |
Correct |
169 ms |
3912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
209 ms |
4424 KB |
Output is correct |
12 |
Correct |
176 ms |
3988 KB |
Output is correct |
13 |
Correct |
215 ms |
3300 KB |
Output is correct |
14 |
Correct |
194 ms |
3400 KB |
Output is correct |
15 |
Correct |
210 ms |
4332 KB |
Output is correct |
16 |
Correct |
243 ms |
3452 KB |
Output is correct |
17 |
Correct |
274 ms |
3540 KB |
Output is correct |
18 |
Correct |
139 ms |
5988 KB |
Output is correct |
19 |
Correct |
163 ms |
2448 KB |
Output is correct |
20 |
Correct |
169 ms |
3912 KB |
Output is correct |
21 |
Correct |
188 ms |
4424 KB |
Output is correct |
22 |
Correct |
233 ms |
4680 KB |
Output is correct |
23 |
Correct |
268 ms |
3592 KB |
Output is correct |
24 |
Correct |
256 ms |
6384 KB |
Output is correct |
25 |
Correct |
281 ms |
7848 KB |
Output is correct |
26 |
Correct |
315 ms |
5752 KB |
Output is correct |
27 |
Correct |
296 ms |
3908 KB |
Output is correct |
28 |
Correct |
139 ms |
7240 KB |
Output is correct |
29 |
Correct |
208 ms |
2512 KB |
Output is correct |
30 |
Correct |
249 ms |
5960 KB |
Output is correct |
31 |
Correct |
275 ms |
4424 KB |
Output is correct |
32 |
Correct |
282 ms |
4424 KB |
Output is correct |
33 |
Correct |
255 ms |
3400 KB |
Output is correct |
34 |
Correct |
316 ms |
6768 KB |
Output is correct |
35 |
Correct |
296 ms |
3912 KB |
Output is correct |
36 |
Correct |
129 ms |
3400 KB |
Output is correct |
37 |
Correct |
204 ms |
5812 KB |
Output is correct |
38 |
Correct |
274 ms |
2376 KB |
Output is correct |
39 |
Correct |
299 ms |
3784 KB |
Output is correct |
40 |
Correct |
277 ms |
3396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
61 ms |
20352 KB |
Output is correct |
12 |
Correct |
75 ms |
19380 KB |
Output is correct |
13 |
Correct |
68 ms |
19428 KB |
Output is correct |
14 |
Correct |
78 ms |
18152 KB |
Output is correct |
15 |
Correct |
61 ms |
20464 KB |
Output is correct |
16 |
Correct |
76 ms |
20712 KB |
Output is correct |
17 |
Correct |
73 ms |
19364 KB |
Output is correct |
18 |
Correct |
66 ms |
20708 KB |
Output is correct |
19 |
Correct |
65 ms |
19700 KB |
Output is correct |
20 |
Correct |
57 ms |
18152 KB |
Output is correct |
21 |
Correct |
57 ms |
20456 KB |
Output is correct |
22 |
Correct |
97 ms |
19184 KB |
Output is correct |
23 |
Correct |
61 ms |
19376 KB |
Output is correct |
24 |
Correct |
77 ms |
18024 KB |
Output is correct |
25 |
Correct |
75 ms |
18920 KB |
Output is correct |
26 |
Correct |
56 ms |
20396 KB |
Output is correct |
27 |
Correct |
56 ms |
19204 KB |
Output is correct |
28 |
Correct |
57 ms |
19040 KB |
Output is correct |
29 |
Correct |
73 ms |
19616 KB |
Output is correct |
30 |
Correct |
70 ms |
19440 KB |
Output is correct |
31 |
Correct |
63 ms |
18152 KB |
Output is correct |
32 |
Correct |
70 ms |
19692 KB |
Output is correct |
33 |
Correct |
94 ms |
19708 KB |
Output is correct |
34 |
Correct |
53 ms |
18084 KB |
Output is correct |
35 |
Correct |
77 ms |
19444 KB |
Output is correct |
36 |
Correct |
75 ms |
20456 KB |
Output is correct |
37 |
Correct |
70 ms |
19324 KB |
Output is correct |
38 |
Correct |
91 ms |
19432 KB |
Output is correct |
39 |
Correct |
79 ms |
19348 KB |
Output is correct |
40 |
Correct |
71 ms |
19300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
209 ms |
4424 KB |
Output is correct |
12 |
Correct |
176 ms |
3988 KB |
Output is correct |
13 |
Correct |
215 ms |
3300 KB |
Output is correct |
14 |
Correct |
194 ms |
3400 KB |
Output is correct |
15 |
Correct |
210 ms |
4332 KB |
Output is correct |
16 |
Correct |
243 ms |
3452 KB |
Output is correct |
17 |
Correct |
274 ms |
3540 KB |
Output is correct |
18 |
Correct |
139 ms |
5988 KB |
Output is correct |
19 |
Correct |
163 ms |
2448 KB |
Output is correct |
20 |
Correct |
169 ms |
3912 KB |
Output is correct |
21 |
Correct |
188 ms |
4424 KB |
Output is correct |
22 |
Correct |
233 ms |
4680 KB |
Output is correct |
23 |
Correct |
268 ms |
3592 KB |
Output is correct |
24 |
Correct |
256 ms |
6384 KB |
Output is correct |
25 |
Correct |
281 ms |
7848 KB |
Output is correct |
26 |
Correct |
315 ms |
5752 KB |
Output is correct |
27 |
Correct |
296 ms |
3908 KB |
Output is correct |
28 |
Correct |
139 ms |
7240 KB |
Output is correct |
29 |
Correct |
208 ms |
2512 KB |
Output is correct |
30 |
Correct |
249 ms |
5960 KB |
Output is correct |
31 |
Correct |
275 ms |
4424 KB |
Output is correct |
32 |
Correct |
282 ms |
4424 KB |
Output is correct |
33 |
Correct |
255 ms |
3400 KB |
Output is correct |
34 |
Correct |
316 ms |
6768 KB |
Output is correct |
35 |
Correct |
296 ms |
3912 KB |
Output is correct |
36 |
Correct |
129 ms |
3400 KB |
Output is correct |
37 |
Correct |
204 ms |
5812 KB |
Output is correct |
38 |
Correct |
274 ms |
2376 KB |
Output is correct |
39 |
Correct |
299 ms |
3784 KB |
Output is correct |
40 |
Correct |
277 ms |
3396 KB |
Output is correct |
41 |
Correct |
61 ms |
20352 KB |
Output is correct |
42 |
Correct |
75 ms |
19380 KB |
Output is correct |
43 |
Correct |
68 ms |
19428 KB |
Output is correct |
44 |
Correct |
78 ms |
18152 KB |
Output is correct |
45 |
Correct |
61 ms |
20464 KB |
Output is correct |
46 |
Correct |
76 ms |
20712 KB |
Output is correct |
47 |
Correct |
73 ms |
19364 KB |
Output is correct |
48 |
Correct |
66 ms |
20708 KB |
Output is correct |
49 |
Correct |
65 ms |
19700 KB |
Output is correct |
50 |
Correct |
57 ms |
18152 KB |
Output is correct |
51 |
Correct |
57 ms |
20456 KB |
Output is correct |
52 |
Correct |
97 ms |
19184 KB |
Output is correct |
53 |
Correct |
61 ms |
19376 KB |
Output is correct |
54 |
Correct |
77 ms |
18024 KB |
Output is correct |
55 |
Correct |
75 ms |
18920 KB |
Output is correct |
56 |
Correct |
56 ms |
20396 KB |
Output is correct |
57 |
Correct |
56 ms |
19204 KB |
Output is correct |
58 |
Correct |
57 ms |
19040 KB |
Output is correct |
59 |
Correct |
73 ms |
19616 KB |
Output is correct |
60 |
Correct |
70 ms |
19440 KB |
Output is correct |
61 |
Correct |
63 ms |
18152 KB |
Output is correct |
62 |
Correct |
70 ms |
19692 KB |
Output is correct |
63 |
Correct |
94 ms |
19708 KB |
Output is correct |
64 |
Correct |
53 ms |
18084 KB |
Output is correct |
65 |
Correct |
77 ms |
19444 KB |
Output is correct |
66 |
Correct |
75 ms |
20456 KB |
Output is correct |
67 |
Correct |
70 ms |
19324 KB |
Output is correct |
68 |
Correct |
91 ms |
19432 KB |
Output is correct |
69 |
Correct |
79 ms |
19348 KB |
Output is correct |
70 |
Correct |
71 ms |
19300 KB |
Output is correct |
71 |
Correct |
496 ms |
35012 KB |
Output is correct |
72 |
Correct |
493 ms |
29752 KB |
Output is correct |
73 |
Correct |
545 ms |
28764 KB |
Output is correct |
74 |
Correct |
1107 ms |
34476 KB |
Output is correct |
75 |
Correct |
460 ms |
24936 KB |
Output is correct |
76 |
Correct |
866 ms |
34280 KB |
Output is correct |
77 |
Correct |
769 ms |
34012 KB |
Output is correct |
78 |
Correct |
203 ms |
26856 KB |
Output is correct |
79 |
Correct |
413 ms |
37608 KB |
Output is correct |
80 |
Correct |
541 ms |
42124 KB |
Output is correct |
81 |
Correct |
705 ms |
33876 KB |
Output is correct |
82 |
Correct |
924 ms |
22968 KB |
Output is correct |
83 |
Correct |
393 ms |
38636 KB |
Output is correct |
84 |
Correct |
648 ms |
22892 KB |
Output is correct |
85 |
Correct |
716 ms |
23272 KB |
Output is correct |
86 |
Correct |
213 ms |
37004 KB |
Output is correct |
87 |
Correct |
441 ms |
43500 KB |
Output is correct |
88 |
Correct |
376 ms |
20968 KB |
Output is correct |
89 |
Correct |
485 ms |
33512 KB |
Output is correct |
90 |
Correct |
492 ms |
33868 KB |
Output is correct |
91 |
Correct |
369 ms |
22112 KB |
Output is correct |
92 |
Correct |
741 ms |
23204 KB |
Output is correct |
93 |
Correct |
601 ms |
23784 KB |
Output is correct |
94 |
Correct |
202 ms |
31984 KB |
Output is correct |
95 |
Correct |
484 ms |
34024 KB |
Output is correct |
96 |
Correct |
565 ms |
23016 KB |
Output is correct |
97 |
Correct |
540 ms |
34144 KB |
Output is correct |
98 |
Correct |
589 ms |
34024 KB |
Output is correct |
99 |
Correct |
532 ms |
23012 KB |
Output is correct |
100 |
Correct |
513 ms |
34024 KB |
Output is correct |