#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define lb lower_bound
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
using namespace std;
const int mxN=1100005;
const int mxM=500004;
const int mxK=30;
const int MOD=1'000'000'007;
const ll INF=8e18;
int L, Q;
char B[mxN];
int A[mxN];
char qry[mxK];
int dp1[mxN], dp0[mxN];
int one[mxN], oi;
int zero[mxN], zi;
int oz[mxN], ozi;
int pc[mxN]; ///popcount
void input(){
cin >> L >> Q;
cin >> B;
for(int i=0;i<(1<<L);i++) A[i]=B[i]-'0';
}
void precalc(){
for(int i=0;i<(1<<L);i++) pc[i]=__builtin_popcount(i)%2;
for(int i=0;i<(1<<L);i++) dp1[i]=dp0[i]=A[i];
for(int i=0;i<L;i++){
for(int j=0;j<(1<<L);j++){
if(j&(1<<i)) dp1[j]+=dp1[j^(1<<i)];
else dp0[j]+=dp0[j^(1<<i)];
}
}
}
void solv_one(){
ll res=0;
int idx=0;
for(int i=0;i<L;i++) if(qry[i]=='?') idx+=(1<<i);
int M=(1<<oi)-1;
for(int i=0;i<(1<<oi);i++){
int ni=0;
for(int j=0;j<oi;j++){
if((1<<j)&i) ni+=(1<<one[j]);
}
//printf("idx=%d, ni=%d\n", idx, ni);
if(pc[i^M]==0) res+=dp1[idx+ni];
else res-=dp1[idx+ni];
}
cout << res << '\n';
}
void solv_zero(){
ll res=0;
int idx=0;
int M=(1<<zi)-1;
for(int i=0;i<L;i++) if(qry[i]=='?') idx+=(1<<i);
for(int i=0;i<(1<<zi);i++){
int ni=0;
for(int j=0;j<zi;j++){
if((1<<j)&i) ni+=(1<<zero[j]);
}
//printf("idx=%d, ni=%d\n", idx, ni);
if(pc[i^M]==0) res+=dp0[(1<<L)-1-idx-ni];
else res-=dp0[(1<<L)-1-idx-ni];
}
cout << res << '\n';
}
void solv_oz(){
ll res=0;
int idx=0;
for(int i=0;i<L;i++) if(qry[i]=='1') idx+=(1<<i);
for(int i=0;i<(1<<ozi);i++){
int ni=0;
for(int j=0;j<ozi;j++){
if((1<<j)&i) ni+=(1<<oz[j]);
}
res+=A[ni+idx];
}
cout << res << '\n';
}
int main()
{
gibon
input();
precalc();
while(Q--){
cin >> qry;
reverse(qry, qry+L);
oi=zi=ozi=0;
for(int i=0;i<L;i++){
if(qry[i]=='1') one[oi++]=i;
if(qry[i]=='0') zero[zi++]=i;
if(qry[i]=='?') oz[ozi++]=i;
}
if(oi<=zi && oi<=ozi) solv_one();
else if(zi<=oi && zi<=ozi) solv_zero();
else solv_oz();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14844 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
4 ms |
14820 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14844 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
4 ms |
14820 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
189 ms |
29344 KB |
Output is correct |
12 |
Correct |
222 ms |
29328 KB |
Output is correct |
13 |
Correct |
237 ms |
28500 KB |
Output is correct |
14 |
Correct |
266 ms |
28536 KB |
Output is correct |
15 |
Correct |
235 ms |
29524 KB |
Output is correct |
16 |
Correct |
269 ms |
28628 KB |
Output is correct |
17 |
Correct |
262 ms |
28736 KB |
Output is correct |
18 |
Correct |
150 ms |
30376 KB |
Output is correct |
19 |
Correct |
169 ms |
27476 KB |
Output is correct |
20 |
Correct |
220 ms |
29188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14844 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
4 ms |
14820 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
189 ms |
29344 KB |
Output is correct |
12 |
Correct |
222 ms |
29328 KB |
Output is correct |
13 |
Correct |
237 ms |
28500 KB |
Output is correct |
14 |
Correct |
266 ms |
28536 KB |
Output is correct |
15 |
Correct |
235 ms |
29524 KB |
Output is correct |
16 |
Correct |
269 ms |
28628 KB |
Output is correct |
17 |
Correct |
262 ms |
28736 KB |
Output is correct |
18 |
Correct |
150 ms |
30376 KB |
Output is correct |
19 |
Correct |
169 ms |
27476 KB |
Output is correct |
20 |
Correct |
220 ms |
29188 KB |
Output is correct |
21 |
Correct |
245 ms |
32432 KB |
Output is correct |
22 |
Correct |
263 ms |
32572 KB |
Output is correct |
23 |
Correct |
291 ms |
31572 KB |
Output is correct |
24 |
Correct |
320 ms |
31560 KB |
Output is correct |
25 |
Correct |
284 ms |
33320 KB |
Output is correct |
26 |
Correct |
337 ms |
31992 KB |
Output is correct |
27 |
Correct |
340 ms |
31556 KB |
Output is correct |
28 |
Correct |
179 ms |
34432 KB |
Output is correct |
29 |
Correct |
234 ms |
30580 KB |
Output is correct |
30 |
Correct |
267 ms |
32520 KB |
Output is correct |
31 |
Correct |
309 ms |
32340 KB |
Output is correct |
32 |
Correct |
335 ms |
32552 KB |
Output is correct |
33 |
Correct |
259 ms |
31444 KB |
Output is correct |
34 |
Correct |
328 ms |
31556 KB |
Output is correct |
35 |
Correct |
334 ms |
32124 KB |
Output is correct |
36 |
Correct |
150 ms |
30496 KB |
Output is correct |
37 |
Correct |
248 ms |
32404 KB |
Output is correct |
38 |
Correct |
249 ms |
30336 KB |
Output is correct |
39 |
Correct |
299 ms |
31568 KB |
Output is correct |
40 |
Correct |
339 ms |
31452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14844 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
4 ms |
14820 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
40 ms |
27724 KB |
Output is correct |
12 |
Correct |
49 ms |
27740 KB |
Output is correct |
13 |
Correct |
46 ms |
27472 KB |
Output is correct |
14 |
Correct |
71 ms |
27476 KB |
Output is correct |
15 |
Correct |
40 ms |
27728 KB |
Output is correct |
16 |
Correct |
65 ms |
27704 KB |
Output is correct |
17 |
Correct |
64 ms |
27652 KB |
Output is correct |
18 |
Correct |
32 ms |
27712 KB |
Output is correct |
19 |
Correct |
41 ms |
27604 KB |
Output is correct |
20 |
Correct |
47 ms |
27540 KB |
Output is correct |
21 |
Correct |
53 ms |
27728 KB |
Output is correct |
22 |
Correct |
70 ms |
27476 KB |
Output is correct |
23 |
Correct |
40 ms |
27632 KB |
Output is correct |
24 |
Correct |
67 ms |
27468 KB |
Output is correct |
25 |
Correct |
67 ms |
27700 KB |
Output is correct |
26 |
Correct |
31 ms |
27468 KB |
Output is correct |
27 |
Correct |
40 ms |
27728 KB |
Output is correct |
28 |
Correct |
40 ms |
27468 KB |
Output is correct |
29 |
Correct |
52 ms |
27532 KB |
Output is correct |
30 |
Correct |
65 ms |
27472 KB |
Output is correct |
31 |
Correct |
39 ms |
27472 KB |
Output is correct |
32 |
Correct |
62 ms |
27468 KB |
Output is correct |
33 |
Correct |
80 ms |
27740 KB |
Output is correct |
34 |
Correct |
31 ms |
27476 KB |
Output is correct |
35 |
Correct |
53 ms |
27472 KB |
Output is correct |
36 |
Correct |
65 ms |
27492 KB |
Output is correct |
37 |
Correct |
60 ms |
27688 KB |
Output is correct |
38 |
Correct |
53 ms |
27476 KB |
Output is correct |
39 |
Correct |
56 ms |
27640 KB |
Output is correct |
40 |
Correct |
58 ms |
27480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14844 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
4 ms |
14820 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
189 ms |
29344 KB |
Output is correct |
12 |
Correct |
222 ms |
29328 KB |
Output is correct |
13 |
Correct |
237 ms |
28500 KB |
Output is correct |
14 |
Correct |
266 ms |
28536 KB |
Output is correct |
15 |
Correct |
235 ms |
29524 KB |
Output is correct |
16 |
Correct |
269 ms |
28628 KB |
Output is correct |
17 |
Correct |
262 ms |
28736 KB |
Output is correct |
18 |
Correct |
150 ms |
30376 KB |
Output is correct |
19 |
Correct |
169 ms |
27476 KB |
Output is correct |
20 |
Correct |
220 ms |
29188 KB |
Output is correct |
21 |
Correct |
245 ms |
32432 KB |
Output is correct |
22 |
Correct |
263 ms |
32572 KB |
Output is correct |
23 |
Correct |
291 ms |
31572 KB |
Output is correct |
24 |
Correct |
320 ms |
31560 KB |
Output is correct |
25 |
Correct |
284 ms |
33320 KB |
Output is correct |
26 |
Correct |
337 ms |
31992 KB |
Output is correct |
27 |
Correct |
340 ms |
31556 KB |
Output is correct |
28 |
Correct |
179 ms |
34432 KB |
Output is correct |
29 |
Correct |
234 ms |
30580 KB |
Output is correct |
30 |
Correct |
267 ms |
32520 KB |
Output is correct |
31 |
Correct |
309 ms |
32340 KB |
Output is correct |
32 |
Correct |
335 ms |
32552 KB |
Output is correct |
33 |
Correct |
259 ms |
31444 KB |
Output is correct |
34 |
Correct |
328 ms |
31556 KB |
Output is correct |
35 |
Correct |
334 ms |
32124 KB |
Output is correct |
36 |
Correct |
150 ms |
30496 KB |
Output is correct |
37 |
Correct |
248 ms |
32404 KB |
Output is correct |
38 |
Correct |
249 ms |
30336 KB |
Output is correct |
39 |
Correct |
299 ms |
31568 KB |
Output is correct |
40 |
Correct |
339 ms |
31452 KB |
Output is correct |
41 |
Correct |
40 ms |
27724 KB |
Output is correct |
42 |
Correct |
49 ms |
27740 KB |
Output is correct |
43 |
Correct |
46 ms |
27472 KB |
Output is correct |
44 |
Correct |
71 ms |
27476 KB |
Output is correct |
45 |
Correct |
40 ms |
27728 KB |
Output is correct |
46 |
Correct |
65 ms |
27704 KB |
Output is correct |
47 |
Correct |
64 ms |
27652 KB |
Output is correct |
48 |
Correct |
32 ms |
27712 KB |
Output is correct |
49 |
Correct |
41 ms |
27604 KB |
Output is correct |
50 |
Correct |
47 ms |
27540 KB |
Output is correct |
51 |
Correct |
53 ms |
27728 KB |
Output is correct |
52 |
Correct |
70 ms |
27476 KB |
Output is correct |
53 |
Correct |
40 ms |
27632 KB |
Output is correct |
54 |
Correct |
67 ms |
27468 KB |
Output is correct |
55 |
Correct |
67 ms |
27700 KB |
Output is correct |
56 |
Correct |
31 ms |
27468 KB |
Output is correct |
57 |
Correct |
40 ms |
27728 KB |
Output is correct |
58 |
Correct |
40 ms |
27468 KB |
Output is correct |
59 |
Correct |
52 ms |
27532 KB |
Output is correct |
60 |
Correct |
65 ms |
27472 KB |
Output is correct |
61 |
Correct |
39 ms |
27472 KB |
Output is correct |
62 |
Correct |
62 ms |
27468 KB |
Output is correct |
63 |
Correct |
80 ms |
27740 KB |
Output is correct |
64 |
Correct |
31 ms |
27476 KB |
Output is correct |
65 |
Correct |
53 ms |
27472 KB |
Output is correct |
66 |
Correct |
65 ms |
27492 KB |
Output is correct |
67 |
Correct |
60 ms |
27688 KB |
Output is correct |
68 |
Correct |
53 ms |
27476 KB |
Output is correct |
69 |
Correct |
56 ms |
27640 KB |
Output is correct |
70 |
Correct |
58 ms |
27480 KB |
Output is correct |
71 |
Correct |
359 ms |
52052 KB |
Output is correct |
72 |
Correct |
385 ms |
52188 KB |
Output is correct |
73 |
Correct |
523 ms |
50516 KB |
Output is correct |
74 |
Correct |
910 ms |
51028 KB |
Output is correct |
75 |
Correct |
363 ms |
52816 KB |
Output is correct |
76 |
Correct |
900 ms |
51540 KB |
Output is correct |
77 |
Correct |
794 ms |
51024 KB |
Output is correct |
78 |
Correct |
206 ms |
54864 KB |
Output is correct |
79 |
Correct |
364 ms |
48980 KB |
Output is correct |
80 |
Correct |
393 ms |
52116 KB |
Output is correct |
81 |
Correct |
556 ms |
51952 KB |
Output is correct |
82 |
Correct |
943 ms |
51036 KB |
Output is correct |
83 |
Correct |
369 ms |
50260 KB |
Output is correct |
84 |
Correct |
928 ms |
51332 KB |
Output is correct |
85 |
Correct |
898 ms |
51080 KB |
Output is correct |
86 |
Correct |
195 ms |
48912 KB |
Output is correct |
87 |
Correct |
351 ms |
51848 KB |
Output is correct |
88 |
Correct |
354 ms |
49028 KB |
Output is correct |
89 |
Correct |
533 ms |
50976 KB |
Output is correct |
90 |
Correct |
893 ms |
51280 KB |
Output is correct |
91 |
Correct |
334 ms |
50004 KB |
Output is correct |
92 |
Correct |
882 ms |
51548 KB |
Output is correct |
93 |
Correct |
710 ms |
51272 KB |
Output is correct |
94 |
Correct |
191 ms |
48936 KB |
Output is correct |
95 |
Correct |
593 ms |
51024 KB |
Output is correct |
96 |
Correct |
709 ms |
51120 KB |
Output is correct |
97 |
Correct |
606 ms |
50904 KB |
Output is correct |
98 |
Correct |
700 ms |
51188 KB |
Output is correct |
99 |
Correct |
663 ms |
50908 KB |
Output is correct |
100 |
Correct |
631 ms |
51144 KB |
Output is correct |