#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
int f[1<<20],g[1<<20];
string s;
ll L;
ll q;
int n(const int &x)
{
ll ans=0;
for(int i=0;i<L;i++)
{
if((x>>i&1)==0)
{
ans+=1<<i;
}
}
return ans;
}
ll a[maxN];
void solve()
{
cin >> L >> q;
cin >> s;
for(int i=0;i<(1<<L);i++)
{
f[i]=s[i]-'0';
}
for(int i=0;i<(1<<L);i++)
{
g[i]=s[n(i)]-'0';
}
for(int i = 0;i < L; ++i) for(int mask = 0; mask < (1<<L); ++mask){
if((mask & (1<<i))==0)
f[mask] += f[mask^(1<<i)];
}
for(int i = 0;i < L; ++i) for(int mask = 0; mask < (1<<L); ++mask){
if((mask & (1<<i))==0)
g[mask] += g[mask^(1<<i)];
}
while(q--)
{
string t;
cin >> t;
ll cnt[3]={0};
for(int i=0;i<L;i++)
{
if(t[i]=='0') cnt[0]++;
else if(t[i]=='1') cnt[1]++;
else cnt[2]++;
}
ll mn=inf;
for(int i=0;i<3;i++) mn=min(mn,cnt[i]);
if(cnt[0]==mn)
{
ll ct=0,so=0;
for(int i=0;i<L;i++)
{
if(t[i]=='0')
{
a[ct]=L-i-1;
ct++;
}
else if(t[i]=='1')
{
so+=1<<(L-i-1);
}
}
ll ass=0,bs;
for(int i=0;i<(1<<ct);i++)
{
bs=0;
for(int j=0;j<ct;j++)
{
if(i>>j&1)
{
bs+=1<<(a[j]);
}
}
if(__builtin_popcount(i)&1) ass-=f[so+bs];
else ass+=f[so+bs];
}
cout << ass << '\n';
}
else if(cnt[1]==mn)
{
ll ct=0,so=0;
for(int i=0;i<L;i++)
{
if(t[i]=='1')
{
a[ct]=L-i-1;
ct++;
}
else if(t[i]=='0')
{
so+=1<<(L-i-1);
}
}
ll ans=0,bs;
for(int i=0;i<1<<ct;i++)
{
bs=0;
for(int j=0;j<ct;j++)
{
if(i>>j&1)
{
bs+=1<<(a[j]);
}
}
if(__builtin_popcount(i)&1) ans-=g[so+bs];
else ans+=g[so+bs];
}
cout << ans << '\n';
}
else
{
ll so=0,ct=0;
for(int i=0;i<L;i++)
{
if(t[i]=='1')
{
so+=1<<(L-i-1);
}
else if(t[i]=='?') a[ct]=L-i-1,ct++;
}
ll ans=0;
for(int i=0;i<1<<ct;i++)
{
ll bs=0;
for(int j=0;j<ct;j++)
{
if(i>>j&1)
{
bs+=1<<a[j];
}
}
ans+=s[bs+so]-'0';
}
cout << ans << '\n';
}
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
# |
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 |
344 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 |
344 KB |
Output is correct |
11 |
Correct |
187 ms |
15112 KB |
Output is correct |
12 |
Correct |
215 ms |
14744 KB |
Output is correct |
13 |
Correct |
284 ms |
14012 KB |
Output is correct |
14 |
Correct |
282 ms |
14028 KB |
Output is correct |
15 |
Correct |
238 ms |
15012 KB |
Output is correct |
16 |
Correct |
282 ms |
14224 KB |
Output is correct |
17 |
Correct |
277 ms |
14260 KB |
Output is correct |
18 |
Correct |
147 ms |
16020 KB |
Output is correct |
19 |
Correct |
188 ms |
13096 KB |
Output is correct |
20 |
Correct |
221 ms |
14704 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 |
344 KB |
Output is correct |
11 |
Correct |
187 ms |
15112 KB |
Output is correct |
12 |
Correct |
215 ms |
14744 KB |
Output is correct |
13 |
Correct |
284 ms |
14012 KB |
Output is correct |
14 |
Correct |
282 ms |
14028 KB |
Output is correct |
15 |
Correct |
238 ms |
15012 KB |
Output is correct |
16 |
Correct |
282 ms |
14224 KB |
Output is correct |
17 |
Correct |
277 ms |
14260 KB |
Output is correct |
18 |
Correct |
147 ms |
16020 KB |
Output is correct |
19 |
Correct |
188 ms |
13096 KB |
Output is correct |
20 |
Correct |
221 ms |
14704 KB |
Output is correct |
21 |
Correct |
248 ms |
18144 KB |
Output is correct |
22 |
Correct |
261 ms |
18252 KB |
Output is correct |
23 |
Correct |
337 ms |
17220 KB |
Output is correct |
24 |
Correct |
396 ms |
17108 KB |
Output is correct |
25 |
Correct |
277 ms |
19056 KB |
Output is correct |
26 |
Correct |
381 ms |
17624 KB |
Output is correct |
27 |
Correct |
362 ms |
17484 KB |
Output is correct |
28 |
Correct |
159 ms |
20084 KB |
Output is correct |
29 |
Correct |
268 ms |
16064 KB |
Output is correct |
30 |
Correct |
267 ms |
18252 KB |
Output is correct |
31 |
Correct |
323 ms |
18232 KB |
Output is correct |
32 |
Correct |
399 ms |
18040 KB |
Output is correct |
33 |
Correct |
269 ms |
16980 KB |
Output is correct |
34 |
Correct |
379 ms |
17080 KB |
Output is correct |
35 |
Correct |
347 ms |
17480 KB |
Output is correct |
36 |
Correct |
187 ms |
16088 KB |
Output is correct |
37 |
Correct |
256 ms |
18112 KB |
Output is correct |
38 |
Correct |
255 ms |
16076 KB |
Output is correct |
39 |
Correct |
316 ms |
17224 KB |
Output is correct |
40 |
Correct |
422 ms |
17140 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 |
344 KB |
Output is correct |
11 |
Correct |
127 ms |
11976 KB |
Output is correct |
12 |
Correct |
124 ms |
12012 KB |
Output is correct |
13 |
Correct |
135 ms |
11928 KB |
Output is correct |
14 |
Correct |
156 ms |
11884 KB |
Output is correct |
15 |
Correct |
122 ms |
12040 KB |
Output is correct |
16 |
Correct |
161 ms |
11876 KB |
Output is correct |
17 |
Correct |
137 ms |
11920 KB |
Output is correct |
18 |
Correct |
111 ms |
12020 KB |
Output is correct |
19 |
Correct |
121 ms |
11720 KB |
Output is correct |
20 |
Correct |
123 ms |
12012 KB |
Output is correct |
21 |
Correct |
127 ms |
11900 KB |
Output is correct |
22 |
Correct |
155 ms |
11912 KB |
Output is correct |
23 |
Correct |
133 ms |
11848 KB |
Output is correct |
24 |
Correct |
153 ms |
11864 KB |
Output is correct |
25 |
Correct |
136 ms |
11832 KB |
Output is correct |
26 |
Correct |
109 ms |
11756 KB |
Output is correct |
27 |
Correct |
139 ms |
12000 KB |
Output is correct |
28 |
Correct |
124 ms |
11836 KB |
Output is correct |
29 |
Correct |
131 ms |
11884 KB |
Output is correct |
30 |
Correct |
148 ms |
11836 KB |
Output is correct |
31 |
Correct |
120 ms |
11772 KB |
Output is correct |
32 |
Correct |
169 ms |
11960 KB |
Output is correct |
33 |
Correct |
147 ms |
11848 KB |
Output is correct |
34 |
Correct |
110 ms |
11732 KB |
Output is correct |
35 |
Correct |
137 ms |
11952 KB |
Output is correct |
36 |
Correct |
134 ms |
11916 KB |
Output is correct |
37 |
Correct |
160 ms |
11864 KB |
Output is correct |
38 |
Correct |
130 ms |
11824 KB |
Output is correct |
39 |
Correct |
129 ms |
11880 KB |
Output is correct |
40 |
Correct |
138 ms |
11908 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 |
344 KB |
Output is correct |
11 |
Correct |
187 ms |
15112 KB |
Output is correct |
12 |
Correct |
215 ms |
14744 KB |
Output is correct |
13 |
Correct |
284 ms |
14012 KB |
Output is correct |
14 |
Correct |
282 ms |
14028 KB |
Output is correct |
15 |
Correct |
238 ms |
15012 KB |
Output is correct |
16 |
Correct |
282 ms |
14224 KB |
Output is correct |
17 |
Correct |
277 ms |
14260 KB |
Output is correct |
18 |
Correct |
147 ms |
16020 KB |
Output is correct |
19 |
Correct |
188 ms |
13096 KB |
Output is correct |
20 |
Correct |
221 ms |
14704 KB |
Output is correct |
21 |
Correct |
248 ms |
18144 KB |
Output is correct |
22 |
Correct |
261 ms |
18252 KB |
Output is correct |
23 |
Correct |
337 ms |
17220 KB |
Output is correct |
24 |
Correct |
396 ms |
17108 KB |
Output is correct |
25 |
Correct |
277 ms |
19056 KB |
Output is correct |
26 |
Correct |
381 ms |
17624 KB |
Output is correct |
27 |
Correct |
362 ms |
17484 KB |
Output is correct |
28 |
Correct |
159 ms |
20084 KB |
Output is correct |
29 |
Correct |
268 ms |
16064 KB |
Output is correct |
30 |
Correct |
267 ms |
18252 KB |
Output is correct |
31 |
Correct |
323 ms |
18232 KB |
Output is correct |
32 |
Correct |
399 ms |
18040 KB |
Output is correct |
33 |
Correct |
269 ms |
16980 KB |
Output is correct |
34 |
Correct |
379 ms |
17080 KB |
Output is correct |
35 |
Correct |
347 ms |
17480 KB |
Output is correct |
36 |
Correct |
187 ms |
16088 KB |
Output is correct |
37 |
Correct |
256 ms |
18112 KB |
Output is correct |
38 |
Correct |
255 ms |
16076 KB |
Output is correct |
39 |
Correct |
316 ms |
17224 KB |
Output is correct |
40 |
Correct |
422 ms |
17140 KB |
Output is correct |
41 |
Correct |
127 ms |
11976 KB |
Output is correct |
42 |
Correct |
124 ms |
12012 KB |
Output is correct |
43 |
Correct |
135 ms |
11928 KB |
Output is correct |
44 |
Correct |
156 ms |
11884 KB |
Output is correct |
45 |
Correct |
122 ms |
12040 KB |
Output is correct |
46 |
Correct |
161 ms |
11876 KB |
Output is correct |
47 |
Correct |
137 ms |
11920 KB |
Output is correct |
48 |
Correct |
111 ms |
12020 KB |
Output is correct |
49 |
Correct |
121 ms |
11720 KB |
Output is correct |
50 |
Correct |
123 ms |
12012 KB |
Output is correct |
51 |
Correct |
127 ms |
11900 KB |
Output is correct |
52 |
Correct |
155 ms |
11912 KB |
Output is correct |
53 |
Correct |
133 ms |
11848 KB |
Output is correct |
54 |
Correct |
153 ms |
11864 KB |
Output is correct |
55 |
Correct |
136 ms |
11832 KB |
Output is correct |
56 |
Correct |
109 ms |
11756 KB |
Output is correct |
57 |
Correct |
139 ms |
12000 KB |
Output is correct |
58 |
Correct |
124 ms |
11836 KB |
Output is correct |
59 |
Correct |
131 ms |
11884 KB |
Output is correct |
60 |
Correct |
148 ms |
11836 KB |
Output is correct |
61 |
Correct |
120 ms |
11772 KB |
Output is correct |
62 |
Correct |
169 ms |
11960 KB |
Output is correct |
63 |
Correct |
147 ms |
11848 KB |
Output is correct |
64 |
Correct |
110 ms |
11732 KB |
Output is correct |
65 |
Correct |
137 ms |
11952 KB |
Output is correct |
66 |
Correct |
134 ms |
11916 KB |
Output is correct |
67 |
Correct |
160 ms |
11864 KB |
Output is correct |
68 |
Correct |
130 ms |
11824 KB |
Output is correct |
69 |
Correct |
129 ms |
11880 KB |
Output is correct |
70 |
Correct |
138 ms |
11908 KB |
Output is correct |
71 |
Correct |
484 ms |
36240 KB |
Output is correct |
72 |
Correct |
478 ms |
36432 KB |
Output is correct |
73 |
Correct |
657 ms |
34968 KB |
Output is correct |
74 |
Correct |
1250 ms |
35212 KB |
Output is correct |
75 |
Correct |
548 ms |
37268 KB |
Output is correct |
76 |
Correct |
1251 ms |
35516 KB |
Output is correct |
77 |
Correct |
1161 ms |
35400 KB |
Output is correct |
78 |
Correct |
358 ms |
39236 KB |
Output is correct |
79 |
Correct |
450 ms |
33164 KB |
Output is correct |
80 |
Correct |
566 ms |
36420 KB |
Output is correct |
81 |
Correct |
743 ms |
36256 KB |
Output is correct |
82 |
Correct |
1277 ms |
35236 KB |
Output is correct |
83 |
Correct |
527 ms |
34408 KB |
Output is correct |
84 |
Correct |
1032 ms |
35452 KB |
Output is correct |
85 |
Correct |
976 ms |
35440 KB |
Output is correct |
86 |
Correct |
319 ms |
33212 KB |
Output is correct |
87 |
Correct |
481 ms |
36392 KB |
Output is correct |
88 |
Correct |
471 ms |
33260 KB |
Output is correct |
89 |
Correct |
665 ms |
34872 KB |
Output is correct |
90 |
Correct |
1035 ms |
35172 KB |
Output is correct |
91 |
Correct |
488 ms |
34412 KB |
Output is correct |
92 |
Correct |
1064 ms |
35576 KB |
Output is correct |
93 |
Correct |
931 ms |
35472 KB |
Output is correct |
94 |
Correct |
322 ms |
33316 KB |
Output is correct |
95 |
Correct |
782 ms |
35304 KB |
Output is correct |
96 |
Correct |
791 ms |
35384 KB |
Output is correct |
97 |
Correct |
797 ms |
35484 KB |
Output is correct |
98 |
Correct |
796 ms |
35280 KB |
Output is correct |
99 |
Correct |
777 ms |
35296 KB |
Output is correct |
100 |
Correct |
842 ms |
35336 KB |
Output is correct |