#include<bits/stdc++.h>
#define endl '\n'
#pragma GCC optimize("O3,unroll-loops")
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
# |
결과 |
실행 시간 |
메모리 |
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 |
332 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 |
336 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 |
332 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 |
336 KB |
Output is correct |
11 |
Correct |
168 ms |
15068 KB |
Output is correct |
12 |
Correct |
251 ms |
14732 KB |
Output is correct |
13 |
Correct |
210 ms |
13956 KB |
Output is correct |
14 |
Correct |
189 ms |
14028 KB |
Output is correct |
15 |
Correct |
187 ms |
15032 KB |
Output is correct |
16 |
Correct |
197 ms |
14256 KB |
Output is correct |
17 |
Correct |
216 ms |
14160 KB |
Output is correct |
18 |
Correct |
136 ms |
16124 KB |
Output is correct |
19 |
Correct |
163 ms |
13100 KB |
Output is correct |
20 |
Correct |
181 ms |
14708 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 |
332 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 |
336 KB |
Output is correct |
11 |
Correct |
168 ms |
15068 KB |
Output is correct |
12 |
Correct |
251 ms |
14732 KB |
Output is correct |
13 |
Correct |
210 ms |
13956 KB |
Output is correct |
14 |
Correct |
189 ms |
14028 KB |
Output is correct |
15 |
Correct |
187 ms |
15032 KB |
Output is correct |
16 |
Correct |
197 ms |
14256 KB |
Output is correct |
17 |
Correct |
216 ms |
14160 KB |
Output is correct |
18 |
Correct |
136 ms |
16124 KB |
Output is correct |
19 |
Correct |
163 ms |
13100 KB |
Output is correct |
20 |
Correct |
181 ms |
14708 KB |
Output is correct |
21 |
Correct |
228 ms |
18056 KB |
Output is correct |
22 |
Correct |
469 ms |
18236 KB |
Output is correct |
23 |
Correct |
288 ms |
17272 KB |
Output is correct |
24 |
Correct |
236 ms |
17072 KB |
Output is correct |
25 |
Correct |
202 ms |
19140 KB |
Output is correct |
26 |
Correct |
229 ms |
17584 KB |
Output is correct |
27 |
Correct |
255 ms |
17504 KB |
Output is correct |
28 |
Correct |
143 ms |
20148 KB |
Output is correct |
29 |
Correct |
208 ms |
16036 KB |
Output is correct |
30 |
Correct |
208 ms |
18344 KB |
Output is correct |
31 |
Correct |
232 ms |
18032 KB |
Output is correct |
32 |
Correct |
225 ms |
18184 KB |
Output is correct |
33 |
Correct |
199 ms |
16948 KB |
Output is correct |
34 |
Correct |
259 ms |
17156 KB |
Output is correct |
35 |
Correct |
255 ms |
17524 KB |
Output is correct |
36 |
Correct |
138 ms |
16064 KB |
Output is correct |
37 |
Correct |
355 ms |
18076 KB |
Output is correct |
38 |
Correct |
204 ms |
16060 KB |
Output is correct |
39 |
Correct |
237 ms |
17308 KB |
Output is correct |
40 |
Correct |
226 ms |
17076 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 |
332 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 |
336 KB |
Output is correct |
11 |
Correct |
27 ms |
12016 KB |
Output is correct |
12 |
Correct |
310 ms |
11980 KB |
Output is correct |
13 |
Correct |
69 ms |
11880 KB |
Output is correct |
14 |
Correct |
41 ms |
11868 KB |
Output is correct |
15 |
Correct |
37 ms |
11964 KB |
Output is correct |
16 |
Correct |
35 ms |
11864 KB |
Output is correct |
17 |
Correct |
46 ms |
12016 KB |
Output is correct |
18 |
Correct |
23 ms |
12120 KB |
Output is correct |
19 |
Correct |
30 ms |
11784 KB |
Output is correct |
20 |
Correct |
29 ms |
11956 KB |
Output is correct |
21 |
Correct |
48 ms |
11988 KB |
Output is correct |
22 |
Correct |
36 ms |
11888 KB |
Output is correct |
23 |
Correct |
33 ms |
11884 KB |
Output is correct |
24 |
Correct |
54 ms |
11876 KB |
Output is correct |
25 |
Correct |
50 ms |
11880 KB |
Output is correct |
26 |
Correct |
23 ms |
11748 KB |
Output is correct |
27 |
Correct |
121 ms |
11992 KB |
Output is correct |
28 |
Correct |
29 ms |
11728 KB |
Output is correct |
29 |
Correct |
30 ms |
11864 KB |
Output is correct |
30 |
Correct |
33 ms |
11952 KB |
Output is correct |
31 |
Correct |
383 ms |
11864 KB |
Output is correct |
32 |
Correct |
35 ms |
11952 KB |
Output is correct |
33 |
Correct |
46 ms |
11900 KB |
Output is correct |
34 |
Correct |
23 ms |
11748 KB |
Output is correct |
35 |
Correct |
69 ms |
11860 KB |
Output is correct |
36 |
Correct |
64 ms |
11884 KB |
Output is correct |
37 |
Correct |
83 ms |
12012 KB |
Output is correct |
38 |
Correct |
67 ms |
11868 KB |
Output is correct |
39 |
Correct |
65 ms |
11888 KB |
Output is correct |
40 |
Correct |
67 ms |
11948 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 |
332 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 |
336 KB |
Output is correct |
11 |
Correct |
168 ms |
15068 KB |
Output is correct |
12 |
Correct |
251 ms |
14732 KB |
Output is correct |
13 |
Correct |
210 ms |
13956 KB |
Output is correct |
14 |
Correct |
189 ms |
14028 KB |
Output is correct |
15 |
Correct |
187 ms |
15032 KB |
Output is correct |
16 |
Correct |
197 ms |
14256 KB |
Output is correct |
17 |
Correct |
216 ms |
14160 KB |
Output is correct |
18 |
Correct |
136 ms |
16124 KB |
Output is correct |
19 |
Correct |
163 ms |
13100 KB |
Output is correct |
20 |
Correct |
181 ms |
14708 KB |
Output is correct |
21 |
Correct |
228 ms |
18056 KB |
Output is correct |
22 |
Correct |
469 ms |
18236 KB |
Output is correct |
23 |
Correct |
288 ms |
17272 KB |
Output is correct |
24 |
Correct |
236 ms |
17072 KB |
Output is correct |
25 |
Correct |
202 ms |
19140 KB |
Output is correct |
26 |
Correct |
229 ms |
17584 KB |
Output is correct |
27 |
Correct |
255 ms |
17504 KB |
Output is correct |
28 |
Correct |
143 ms |
20148 KB |
Output is correct |
29 |
Correct |
208 ms |
16036 KB |
Output is correct |
30 |
Correct |
208 ms |
18344 KB |
Output is correct |
31 |
Correct |
232 ms |
18032 KB |
Output is correct |
32 |
Correct |
225 ms |
18184 KB |
Output is correct |
33 |
Correct |
199 ms |
16948 KB |
Output is correct |
34 |
Correct |
259 ms |
17156 KB |
Output is correct |
35 |
Correct |
255 ms |
17524 KB |
Output is correct |
36 |
Correct |
138 ms |
16064 KB |
Output is correct |
37 |
Correct |
355 ms |
18076 KB |
Output is correct |
38 |
Correct |
204 ms |
16060 KB |
Output is correct |
39 |
Correct |
237 ms |
17308 KB |
Output is correct |
40 |
Correct |
226 ms |
17076 KB |
Output is correct |
41 |
Correct |
27 ms |
12016 KB |
Output is correct |
42 |
Correct |
310 ms |
11980 KB |
Output is correct |
43 |
Correct |
69 ms |
11880 KB |
Output is correct |
44 |
Correct |
41 ms |
11868 KB |
Output is correct |
45 |
Correct |
37 ms |
11964 KB |
Output is correct |
46 |
Correct |
35 ms |
11864 KB |
Output is correct |
47 |
Correct |
46 ms |
12016 KB |
Output is correct |
48 |
Correct |
23 ms |
12120 KB |
Output is correct |
49 |
Correct |
30 ms |
11784 KB |
Output is correct |
50 |
Correct |
29 ms |
11956 KB |
Output is correct |
51 |
Correct |
48 ms |
11988 KB |
Output is correct |
52 |
Correct |
36 ms |
11888 KB |
Output is correct |
53 |
Correct |
33 ms |
11884 KB |
Output is correct |
54 |
Correct |
54 ms |
11876 KB |
Output is correct |
55 |
Correct |
50 ms |
11880 KB |
Output is correct |
56 |
Correct |
23 ms |
11748 KB |
Output is correct |
57 |
Correct |
121 ms |
11992 KB |
Output is correct |
58 |
Correct |
29 ms |
11728 KB |
Output is correct |
59 |
Correct |
30 ms |
11864 KB |
Output is correct |
60 |
Correct |
33 ms |
11952 KB |
Output is correct |
61 |
Correct |
383 ms |
11864 KB |
Output is correct |
62 |
Correct |
35 ms |
11952 KB |
Output is correct |
63 |
Correct |
46 ms |
11900 KB |
Output is correct |
64 |
Correct |
23 ms |
11748 KB |
Output is correct |
65 |
Correct |
69 ms |
11860 KB |
Output is correct |
66 |
Correct |
64 ms |
11884 KB |
Output is correct |
67 |
Correct |
83 ms |
12012 KB |
Output is correct |
68 |
Correct |
67 ms |
11868 KB |
Output is correct |
69 |
Correct |
65 ms |
11888 KB |
Output is correct |
70 |
Correct |
67 ms |
11948 KB |
Output is correct |
71 |
Correct |
307 ms |
36236 KB |
Output is correct |
72 |
Execution timed out |
2059 ms |
20556 KB |
Time limit exceeded |
73 |
Halted |
0 ms |
0 KB |
- |