#include<bits/stdc++.h>
#define endl '\n'
#pragma GCC optimize("Ofast")
using namespace std;
string s,val;
int i,j,p,q,n,m,k,l,Q,A,B,C;
int dp[(1<<21)],subdp[(1<<21)];
int bitcnt[(1<<21)],ans;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>l>>Q;
n = (1<<l);
cin>>val;
for(i=1; i<n; i++)
bitcnt[i] = bitcnt[i/2]+i%2;
for(i=0; i<n; i++)
dp[i] = subdp[i] = val[i]-'0';
for(i=0; i<l; i++)
{
for(j=0; j<n; j++)
{
if((j&(1<<i))==0)
dp[j]+=dp[j^(1<<i)];
else subdp[j]+=subdp[j^(1<<i)];
///1 za sub/ 0 za super
}
}
while(Q--)
{
cin>>s;
A = B = C = 0;
ans = 0;
for(i=0; i<l; i++)
{
if(s[i]=='0')
A |= (1<<(l-i-1));
else if(s[i]=='1')
B |= (1<<(l-i-1));
else
C |= (1<<(l-i-1));
}
///tva e mega dobro
if(bitcnt[C]<=bitcnt[A] && bitcnt[C]<=bitcnt[B])
{
///vsichki podmnojestva ot setnatite bitove na C
for(int m=C; m!=0; m=(m-1)&C)
{
ans+=val[m|B]-'0';
}
ans+=val[B]-'0';
}
///tva e mega dobro
else if(bitcnt[A]<=bitcnt[B])
{
for(int m=A;m!=0;m=(m-1)&A)
{
if(bitcnt[m]%2==0)
ans+=dp[m|B];
else
ans-=dp[m|B];
}
ans+=dp[B];
}
else
{
for(int m=B;m!=0;m=(m-1)&B)
{
if((bitcnt[B]-bitcnt[m])%2==0)
ans+=subdp[m|C];
else
ans-=subdp[m|C];
}
//ans+=subdp[C|];
if(bitcnt[B]%2==0)ans+=subdp[C];
else ans-=subdp[C];
}
cout<<ans<<endl;
}
return 0;
}
///precompute bit count for speed up
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
165 ms |
4288 KB |
Output is correct |
12 |
Correct |
164 ms |
3900 KB |
Output is correct |
13 |
Correct |
189 ms |
3288 KB |
Output is correct |
14 |
Correct |
207 ms |
3340 KB |
Output is correct |
15 |
Correct |
167 ms |
4344 KB |
Output is correct |
16 |
Correct |
205 ms |
3484 KB |
Output is correct |
17 |
Correct |
205 ms |
3416 KB |
Output is correct |
18 |
Correct |
141 ms |
5364 KB |
Output is correct |
19 |
Correct |
161 ms |
2344 KB |
Output is correct |
20 |
Correct |
167 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
165 ms |
4288 KB |
Output is correct |
12 |
Correct |
164 ms |
3900 KB |
Output is correct |
13 |
Correct |
189 ms |
3288 KB |
Output is correct |
14 |
Correct |
207 ms |
3340 KB |
Output is correct |
15 |
Correct |
167 ms |
4344 KB |
Output is correct |
16 |
Correct |
205 ms |
3484 KB |
Output is correct |
17 |
Correct |
205 ms |
3416 KB |
Output is correct |
18 |
Correct |
141 ms |
5364 KB |
Output is correct |
19 |
Correct |
161 ms |
2344 KB |
Output is correct |
20 |
Correct |
167 ms |
3948 KB |
Output is correct |
21 |
Correct |
196 ms |
4448 KB |
Output is correct |
22 |
Correct |
203 ms |
4616 KB |
Output is correct |
23 |
Correct |
259 ms |
3596 KB |
Output is correct |
24 |
Correct |
217 ms |
3448 KB |
Output is correct |
25 |
Correct |
195 ms |
5380 KB |
Output is correct |
26 |
Correct |
250 ms |
3884 KB |
Output is correct |
27 |
Correct |
227 ms |
3816 KB |
Output is correct |
28 |
Correct |
157 ms |
6376 KB |
Output is correct |
29 |
Correct |
180 ms |
2408 KB |
Output is correct |
30 |
Correct |
223 ms |
4548 KB |
Output is correct |
31 |
Correct |
215 ms |
4436 KB |
Output is correct |
32 |
Correct |
221 ms |
4348 KB |
Output is correct |
33 |
Correct |
192 ms |
3248 KB |
Output is correct |
34 |
Correct |
220 ms |
3396 KB |
Output is correct |
35 |
Correct |
231 ms |
3916 KB |
Output is correct |
36 |
Correct |
135 ms |
2424 KB |
Output is correct |
37 |
Correct |
173 ms |
4428 KB |
Output is correct |
38 |
Correct |
186 ms |
2364 KB |
Output is correct |
39 |
Correct |
235 ms |
3624 KB |
Output is correct |
40 |
Correct |
211 ms |
3348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
56 ms |
13928 KB |
Output is correct |
12 |
Correct |
61 ms |
13940 KB |
Output is correct |
13 |
Correct |
64 ms |
13908 KB |
Output is correct |
14 |
Correct |
60 ms |
13940 KB |
Output is correct |
15 |
Correct |
55 ms |
14056 KB |
Output is correct |
16 |
Correct |
69 ms |
13956 KB |
Output is correct |
17 |
Correct |
63 ms |
13940 KB |
Output is correct |
18 |
Correct |
46 ms |
14180 KB |
Output is correct |
19 |
Correct |
54 ms |
13848 KB |
Output is correct |
20 |
Correct |
54 ms |
14016 KB |
Output is correct |
21 |
Correct |
59 ms |
14004 KB |
Output is correct |
22 |
Correct |
67 ms |
13928 KB |
Output is correct |
23 |
Correct |
52 ms |
13940 KB |
Output is correct |
24 |
Correct |
61 ms |
13932 KB |
Output is correct |
25 |
Correct |
61 ms |
13940 KB |
Output is correct |
26 |
Correct |
50 ms |
13816 KB |
Output is correct |
27 |
Correct |
53 ms |
13980 KB |
Output is correct |
28 |
Correct |
52 ms |
13856 KB |
Output is correct |
29 |
Correct |
59 ms |
13924 KB |
Output is correct |
30 |
Correct |
57 ms |
13940 KB |
Output is correct |
31 |
Correct |
55 ms |
13932 KB |
Output is correct |
32 |
Correct |
70 ms |
13936 KB |
Output is correct |
33 |
Correct |
66 ms |
13940 KB |
Output is correct |
34 |
Correct |
46 ms |
13860 KB |
Output is correct |
35 |
Correct |
71 ms |
13924 KB |
Output is correct |
36 |
Correct |
61 ms |
13996 KB |
Output is correct |
37 |
Correct |
61 ms |
13944 KB |
Output is correct |
38 |
Correct |
75 ms |
13932 KB |
Output is correct |
39 |
Correct |
61 ms |
13936 KB |
Output is correct |
40 |
Correct |
59 ms |
13948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
165 ms |
4288 KB |
Output is correct |
12 |
Correct |
164 ms |
3900 KB |
Output is correct |
13 |
Correct |
189 ms |
3288 KB |
Output is correct |
14 |
Correct |
207 ms |
3340 KB |
Output is correct |
15 |
Correct |
167 ms |
4344 KB |
Output is correct |
16 |
Correct |
205 ms |
3484 KB |
Output is correct |
17 |
Correct |
205 ms |
3416 KB |
Output is correct |
18 |
Correct |
141 ms |
5364 KB |
Output is correct |
19 |
Correct |
161 ms |
2344 KB |
Output is correct |
20 |
Correct |
167 ms |
3948 KB |
Output is correct |
21 |
Correct |
196 ms |
4448 KB |
Output is correct |
22 |
Correct |
203 ms |
4616 KB |
Output is correct |
23 |
Correct |
259 ms |
3596 KB |
Output is correct |
24 |
Correct |
217 ms |
3448 KB |
Output is correct |
25 |
Correct |
195 ms |
5380 KB |
Output is correct |
26 |
Correct |
250 ms |
3884 KB |
Output is correct |
27 |
Correct |
227 ms |
3816 KB |
Output is correct |
28 |
Correct |
157 ms |
6376 KB |
Output is correct |
29 |
Correct |
180 ms |
2408 KB |
Output is correct |
30 |
Correct |
223 ms |
4548 KB |
Output is correct |
31 |
Correct |
215 ms |
4436 KB |
Output is correct |
32 |
Correct |
221 ms |
4348 KB |
Output is correct |
33 |
Correct |
192 ms |
3248 KB |
Output is correct |
34 |
Correct |
220 ms |
3396 KB |
Output is correct |
35 |
Correct |
231 ms |
3916 KB |
Output is correct |
36 |
Correct |
135 ms |
2424 KB |
Output is correct |
37 |
Correct |
173 ms |
4428 KB |
Output is correct |
38 |
Correct |
186 ms |
2364 KB |
Output is correct |
39 |
Correct |
235 ms |
3624 KB |
Output is correct |
40 |
Correct |
211 ms |
3348 KB |
Output is correct |
41 |
Correct |
56 ms |
13928 KB |
Output is correct |
42 |
Correct |
61 ms |
13940 KB |
Output is correct |
43 |
Correct |
64 ms |
13908 KB |
Output is correct |
44 |
Correct |
60 ms |
13940 KB |
Output is correct |
45 |
Correct |
55 ms |
14056 KB |
Output is correct |
46 |
Correct |
69 ms |
13956 KB |
Output is correct |
47 |
Correct |
63 ms |
13940 KB |
Output is correct |
48 |
Correct |
46 ms |
14180 KB |
Output is correct |
49 |
Correct |
54 ms |
13848 KB |
Output is correct |
50 |
Correct |
54 ms |
14016 KB |
Output is correct |
51 |
Correct |
59 ms |
14004 KB |
Output is correct |
52 |
Correct |
67 ms |
13928 KB |
Output is correct |
53 |
Correct |
52 ms |
13940 KB |
Output is correct |
54 |
Correct |
61 ms |
13932 KB |
Output is correct |
55 |
Correct |
61 ms |
13940 KB |
Output is correct |
56 |
Correct |
50 ms |
13816 KB |
Output is correct |
57 |
Correct |
53 ms |
13980 KB |
Output is correct |
58 |
Correct |
52 ms |
13856 KB |
Output is correct |
59 |
Correct |
59 ms |
13924 KB |
Output is correct |
60 |
Correct |
57 ms |
13940 KB |
Output is correct |
61 |
Correct |
55 ms |
13932 KB |
Output is correct |
62 |
Correct |
70 ms |
13936 KB |
Output is correct |
63 |
Correct |
66 ms |
13940 KB |
Output is correct |
64 |
Correct |
46 ms |
13860 KB |
Output is correct |
65 |
Correct |
71 ms |
13924 KB |
Output is correct |
66 |
Correct |
61 ms |
13996 KB |
Output is correct |
67 |
Correct |
61 ms |
13944 KB |
Output is correct |
68 |
Correct |
75 ms |
13932 KB |
Output is correct |
69 |
Correct |
61 ms |
13936 KB |
Output is correct |
70 |
Correct |
59 ms |
13948 KB |
Output is correct |
71 |
Correct |
304 ms |
18808 KB |
Output is correct |
72 |
Correct |
309 ms |
31464 KB |
Output is correct |
73 |
Correct |
422 ms |
39008 KB |
Output is correct |
74 |
Correct |
480 ms |
39380 KB |
Output is correct |
75 |
Correct |
316 ms |
41332 KB |
Output is correct |
76 |
Correct |
520 ms |
39648 KB |
Output is correct |
77 |
Correct |
486 ms |
39732 KB |
Output is correct |
78 |
Correct |
211 ms |
43356 KB |
Output is correct |
79 |
Correct |
273 ms |
37276 KB |
Output is correct |
80 |
Correct |
335 ms |
40428 KB |
Output is correct |
81 |
Correct |
417 ms |
40340 KB |
Output is correct |
82 |
Correct |
500 ms |
39276 KB |
Output is correct |
83 |
Correct |
306 ms |
38532 KB |
Output is correct |
84 |
Correct |
471 ms |
39324 KB |
Output is correct |
85 |
Correct |
551 ms |
39600 KB |
Output is correct |
86 |
Correct |
194 ms |
37404 KB |
Output is correct |
87 |
Correct |
343 ms |
40324 KB |
Output is correct |
88 |
Correct |
276 ms |
37292 KB |
Output is correct |
89 |
Correct |
359 ms |
38992 KB |
Output is correct |
90 |
Correct |
409 ms |
39304 KB |
Output is correct |
91 |
Correct |
326 ms |
38492 KB |
Output is correct |
92 |
Correct |
554 ms |
39712 KB |
Output is correct |
93 |
Correct |
517 ms |
39568 KB |
Output is correct |
94 |
Correct |
183 ms |
37360 KB |
Output is correct |
95 |
Correct |
437 ms |
39480 KB |
Output is correct |
96 |
Correct |
474 ms |
39388 KB |
Output is correct |
97 |
Correct |
406 ms |
39456 KB |
Output is correct |
98 |
Correct |
412 ms |
39424 KB |
Output is correct |
99 |
Correct |
443 ms |
39428 KB |
Output is correct |
100 |
Correct |
436 ms |
39404 KB |
Output is correct |