#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)];
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] = 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)];
///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
{
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];
}
cout<<ans<<endl;
}
return 0;
}
///precompute bit count for speed up
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
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 |
167 ms |
4340 KB |
Output is correct |
12 |
Correct |
240 ms |
4072 KB |
Output is correct |
13 |
Correct |
223 ms |
3236 KB |
Output is correct |
14 |
Correct |
185 ms |
3328 KB |
Output is correct |
15 |
Correct |
178 ms |
4300 KB |
Output is correct |
16 |
Correct |
195 ms |
3504 KB |
Output is correct |
17 |
Correct |
206 ms |
3388 KB |
Output is correct |
18 |
Correct |
130 ms |
5352 KB |
Output is correct |
19 |
Correct |
152 ms |
2368 KB |
Output is correct |
20 |
Correct |
180 ms |
3900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
167 ms |
4340 KB |
Output is correct |
12 |
Correct |
240 ms |
4072 KB |
Output is correct |
13 |
Correct |
223 ms |
3236 KB |
Output is correct |
14 |
Correct |
185 ms |
3328 KB |
Output is correct |
15 |
Correct |
178 ms |
4300 KB |
Output is correct |
16 |
Correct |
195 ms |
3504 KB |
Output is correct |
17 |
Correct |
206 ms |
3388 KB |
Output is correct |
18 |
Correct |
130 ms |
5352 KB |
Output is correct |
19 |
Correct |
152 ms |
2368 KB |
Output is correct |
20 |
Correct |
180 ms |
3900 KB |
Output is correct |
21 |
Correct |
189 ms |
4376 KB |
Output is correct |
22 |
Correct |
458 ms |
4496 KB |
Output is correct |
23 |
Correct |
267 ms |
3648 KB |
Output is correct |
24 |
Correct |
215 ms |
3348 KB |
Output is correct |
25 |
Correct |
208 ms |
5352 KB |
Output is correct |
26 |
Correct |
232 ms |
3928 KB |
Output is correct |
27 |
Correct |
243 ms |
3820 KB |
Output is correct |
28 |
Correct |
136 ms |
6348 KB |
Output is correct |
29 |
Correct |
171 ms |
2384 KB |
Output is correct |
30 |
Correct |
204 ms |
4508 KB |
Output is correct |
31 |
Correct |
232 ms |
4340 KB |
Output is correct |
32 |
Correct |
234 ms |
4572 KB |
Output is correct |
33 |
Correct |
188 ms |
3224 KB |
Output is correct |
34 |
Correct |
247 ms |
3356 KB |
Output is correct |
35 |
Correct |
243 ms |
3780 KB |
Output is correct |
36 |
Correct |
127 ms |
2400 KB |
Output is correct |
37 |
Correct |
353 ms |
4404 KB |
Output is correct |
38 |
Correct |
182 ms |
2380 KB |
Output is correct |
39 |
Correct |
208 ms |
3544 KB |
Output is correct |
40 |
Correct |
214 ms |
3380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
37 ms |
9940 KB |
Output is correct |
12 |
Correct |
313 ms |
10044 KB |
Output is correct |
13 |
Correct |
106 ms |
9812 KB |
Output is correct |
14 |
Correct |
50 ms |
9836 KB |
Output is correct |
15 |
Correct |
46 ms |
9964 KB |
Output is correct |
16 |
Correct |
43 ms |
9808 KB |
Output is correct |
17 |
Correct |
54 ms |
9844 KB |
Output is correct |
18 |
Correct |
39 ms |
9964 KB |
Output is correct |
19 |
Correct |
34 ms |
9700 KB |
Output is correct |
20 |
Correct |
37 ms |
9876 KB |
Output is correct |
21 |
Correct |
42 ms |
9824 KB |
Output is correct |
22 |
Correct |
46 ms |
9776 KB |
Output is correct |
23 |
Correct |
34 ms |
9840 KB |
Output is correct |
24 |
Correct |
66 ms |
9868 KB |
Output is correct |
25 |
Correct |
67 ms |
9884 KB |
Output is correct |
26 |
Correct |
29 ms |
9780 KB |
Output is correct |
27 |
Correct |
131 ms |
9928 KB |
Output is correct |
28 |
Correct |
34 ms |
9788 KB |
Output is correct |
29 |
Correct |
41 ms |
9780 KB |
Output is correct |
30 |
Correct |
39 ms |
9804 KB |
Output is correct |
31 |
Correct |
414 ms |
9816 KB |
Output is correct |
32 |
Correct |
48 ms |
9832 KB |
Output is correct |
33 |
Correct |
55 ms |
9824 KB |
Output is correct |
34 |
Correct |
34 ms |
9708 KB |
Output is correct |
35 |
Correct |
81 ms |
9816 KB |
Output is correct |
36 |
Correct |
79 ms |
9852 KB |
Output is correct |
37 |
Correct |
72 ms |
9796 KB |
Output is correct |
38 |
Correct |
85 ms |
9812 KB |
Output is correct |
39 |
Correct |
81 ms |
9868 KB |
Output is correct |
40 |
Correct |
75 ms |
9832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
167 ms |
4340 KB |
Output is correct |
12 |
Correct |
240 ms |
4072 KB |
Output is correct |
13 |
Correct |
223 ms |
3236 KB |
Output is correct |
14 |
Correct |
185 ms |
3328 KB |
Output is correct |
15 |
Correct |
178 ms |
4300 KB |
Output is correct |
16 |
Correct |
195 ms |
3504 KB |
Output is correct |
17 |
Correct |
206 ms |
3388 KB |
Output is correct |
18 |
Correct |
130 ms |
5352 KB |
Output is correct |
19 |
Correct |
152 ms |
2368 KB |
Output is correct |
20 |
Correct |
180 ms |
3900 KB |
Output is correct |
21 |
Correct |
189 ms |
4376 KB |
Output is correct |
22 |
Correct |
458 ms |
4496 KB |
Output is correct |
23 |
Correct |
267 ms |
3648 KB |
Output is correct |
24 |
Correct |
215 ms |
3348 KB |
Output is correct |
25 |
Correct |
208 ms |
5352 KB |
Output is correct |
26 |
Correct |
232 ms |
3928 KB |
Output is correct |
27 |
Correct |
243 ms |
3820 KB |
Output is correct |
28 |
Correct |
136 ms |
6348 KB |
Output is correct |
29 |
Correct |
171 ms |
2384 KB |
Output is correct |
30 |
Correct |
204 ms |
4508 KB |
Output is correct |
31 |
Correct |
232 ms |
4340 KB |
Output is correct |
32 |
Correct |
234 ms |
4572 KB |
Output is correct |
33 |
Correct |
188 ms |
3224 KB |
Output is correct |
34 |
Correct |
247 ms |
3356 KB |
Output is correct |
35 |
Correct |
243 ms |
3780 KB |
Output is correct |
36 |
Correct |
127 ms |
2400 KB |
Output is correct |
37 |
Correct |
353 ms |
4404 KB |
Output is correct |
38 |
Correct |
182 ms |
2380 KB |
Output is correct |
39 |
Correct |
208 ms |
3544 KB |
Output is correct |
40 |
Correct |
214 ms |
3380 KB |
Output is correct |
41 |
Correct |
37 ms |
9940 KB |
Output is correct |
42 |
Correct |
313 ms |
10044 KB |
Output is correct |
43 |
Correct |
106 ms |
9812 KB |
Output is correct |
44 |
Correct |
50 ms |
9836 KB |
Output is correct |
45 |
Correct |
46 ms |
9964 KB |
Output is correct |
46 |
Correct |
43 ms |
9808 KB |
Output is correct |
47 |
Correct |
54 ms |
9844 KB |
Output is correct |
48 |
Correct |
39 ms |
9964 KB |
Output is correct |
49 |
Correct |
34 ms |
9700 KB |
Output is correct |
50 |
Correct |
37 ms |
9876 KB |
Output is correct |
51 |
Correct |
42 ms |
9824 KB |
Output is correct |
52 |
Correct |
46 ms |
9776 KB |
Output is correct |
53 |
Correct |
34 ms |
9840 KB |
Output is correct |
54 |
Correct |
66 ms |
9868 KB |
Output is correct |
55 |
Correct |
67 ms |
9884 KB |
Output is correct |
56 |
Correct |
29 ms |
9780 KB |
Output is correct |
57 |
Correct |
131 ms |
9928 KB |
Output is correct |
58 |
Correct |
34 ms |
9788 KB |
Output is correct |
59 |
Correct |
41 ms |
9780 KB |
Output is correct |
60 |
Correct |
39 ms |
9804 KB |
Output is correct |
61 |
Correct |
414 ms |
9816 KB |
Output is correct |
62 |
Correct |
48 ms |
9832 KB |
Output is correct |
63 |
Correct |
55 ms |
9824 KB |
Output is correct |
64 |
Correct |
34 ms |
9708 KB |
Output is correct |
65 |
Correct |
81 ms |
9816 KB |
Output is correct |
66 |
Correct |
79 ms |
9852 KB |
Output is correct |
67 |
Correct |
72 ms |
9796 KB |
Output is correct |
68 |
Correct |
85 ms |
9812 KB |
Output is correct |
69 |
Correct |
81 ms |
9868 KB |
Output is correct |
70 |
Correct |
75 ms |
9832 KB |
Output is correct |
71 |
Correct |
274 ms |
14656 KB |
Output is correct |
72 |
Execution timed out |
2063 ms |
11476 KB |
Time limit exceeded |
73 |
Halted |
0 ms |
0 KB |
- |