#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
#define debug(x) cerr<<#x<<" :"<<x<<"\n"
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define FAST ios_base::sync_with_stdio(false), cin.tie(), cout.tie();
//#define int long long
typedef long long ll;
typedef long double ld;
const int maxn = 20;
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const int mlog = 20;
const int SQ = 400;
int sos[1 << 20], rsos[1 << 20];
string s, k;
int l, q;
int a, b, c;
int main()
{
FAST;
cin >> l >> q >> s;
for(int i = 0; i < (1 << l); i++)
sos[i] = s[i] - '0', rsos[i] = s[(1 << l) - i - 1] - '0';
for(int i = 0; i < l; i++)
for(int mask = 0; mask < (1 << l); mask++)
if(mask & (1 << i))
sos[mask] += sos[mask ^ (1 << i)], rsos[mask] += rsos[mask ^ (1 << i)];
while(q--)
{
cin >> k;
reverse(all(k));
a = b = c = 0;
int ans = 0, base = 0, mask = 0;
for(int i = 0; i < l; i++)
{
if(k[i] == '?') a++;
if(k[i] == '0') b++;
if(k[i] == '1') c++;
}
if(a < 7)
{
for(int i = 0; i < l; i++)
{
if (k[i] == '?') mask += 1 << i;
else if (k[i] == '1') base += 1 << i;
}
for (int i = mask; i; i = mask & (i - 1)) ans += s[i | base] - '0';
ans += s[base] - '0';
}
else if(b < 7)
{
for(int i = 0; i < l; i++)
{
if (k[i] == '0') mask += 1 << i;
else if (k[i] == '?') base += 1 << i;
}
for (int i = mask; i; i = mask & (i - 1)) ans += ((b - __builtin_popcount(i)) & 1 ? -1 : 1) * rsos[i | base];
ans += (b & 1 ? -1 : 1) * rsos[base];
}
else
{
for(int i = 0; i < l; i++)
{
if (k[i] == '1') mask += 1 << i;
else if (k[i] == '?') base += 1 << i;
}
for (int i = mask; i; i = mask & (i - 1)) ans += ((c - __builtin_popcount(i)) & 1 ? -1 : 1) * sos[i | base];
ans += (c & 1 ? -1 : 1) * sos[base];
}
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
1921 ms |
4396 KB |
Output is correct |
12 |
Correct |
1993 ms |
4216 KB |
Output is correct |
13 |
Correct |
1837 ms |
3460 KB |
Output is correct |
14 |
Correct |
1896 ms |
3564 KB |
Output is correct |
15 |
Correct |
1928 ms |
4372 KB |
Output is correct |
16 |
Correct |
1910 ms |
3412 KB |
Output is correct |
17 |
Correct |
1919 ms |
3464 KB |
Output is correct |
18 |
Correct |
1798 ms |
5360 KB |
Output is correct |
19 |
Correct |
1801 ms |
2500 KB |
Output is correct |
20 |
Correct |
1862 ms |
3896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
1921 ms |
4396 KB |
Output is correct |
12 |
Correct |
1993 ms |
4216 KB |
Output is correct |
13 |
Correct |
1837 ms |
3460 KB |
Output is correct |
14 |
Correct |
1896 ms |
3564 KB |
Output is correct |
15 |
Correct |
1928 ms |
4372 KB |
Output is correct |
16 |
Correct |
1910 ms |
3412 KB |
Output is correct |
17 |
Correct |
1919 ms |
3464 KB |
Output is correct |
18 |
Correct |
1798 ms |
5360 KB |
Output is correct |
19 |
Correct |
1801 ms |
2500 KB |
Output is correct |
20 |
Correct |
1862 ms |
3896 KB |
Output is correct |
21 |
Correct |
1827 ms |
4444 KB |
Output is correct |
22 |
Execution timed out |
2039 ms |
4744 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
136 ms |
9948 KB |
Output is correct |
12 |
Correct |
136 ms |
10032 KB |
Output is correct |
13 |
Correct |
138 ms |
10056 KB |
Output is correct |
14 |
Correct |
163 ms |
9908 KB |
Output is correct |
15 |
Correct |
139 ms |
9952 KB |
Output is correct |
16 |
Correct |
158 ms |
9908 KB |
Output is correct |
17 |
Correct |
158 ms |
9908 KB |
Output is correct |
18 |
Correct |
127 ms |
10052 KB |
Output is correct |
19 |
Correct |
132 ms |
9768 KB |
Output is correct |
20 |
Correct |
133 ms |
9964 KB |
Output is correct |
21 |
Correct |
150 ms |
9904 KB |
Output is correct |
22 |
Correct |
162 ms |
9852 KB |
Output is correct |
23 |
Correct |
133 ms |
9832 KB |
Output is correct |
24 |
Correct |
142 ms |
9892 KB |
Output is correct |
25 |
Correct |
160 ms |
9956 KB |
Output is correct |
26 |
Correct |
129 ms |
9796 KB |
Output is correct |
27 |
Correct |
129 ms |
9904 KB |
Output is correct |
28 |
Correct |
128 ms |
9776 KB |
Output is correct |
29 |
Correct |
142 ms |
9888 KB |
Output is correct |
30 |
Correct |
142 ms |
10040 KB |
Output is correct |
31 |
Correct |
134 ms |
9908 KB |
Output is correct |
32 |
Correct |
168 ms |
9904 KB |
Output is correct |
33 |
Correct |
164 ms |
9956 KB |
Output is correct |
34 |
Correct |
125 ms |
9828 KB |
Output is correct |
35 |
Correct |
152 ms |
9916 KB |
Output is correct |
36 |
Correct |
150 ms |
9956 KB |
Output is correct |
37 |
Correct |
153 ms |
9956 KB |
Output is correct |
38 |
Correct |
151 ms |
10032 KB |
Output is correct |
39 |
Correct |
149 ms |
9904 KB |
Output is correct |
40 |
Correct |
153 ms |
9960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
1921 ms |
4396 KB |
Output is correct |
12 |
Correct |
1993 ms |
4216 KB |
Output is correct |
13 |
Correct |
1837 ms |
3460 KB |
Output is correct |
14 |
Correct |
1896 ms |
3564 KB |
Output is correct |
15 |
Correct |
1928 ms |
4372 KB |
Output is correct |
16 |
Correct |
1910 ms |
3412 KB |
Output is correct |
17 |
Correct |
1919 ms |
3464 KB |
Output is correct |
18 |
Correct |
1798 ms |
5360 KB |
Output is correct |
19 |
Correct |
1801 ms |
2500 KB |
Output is correct |
20 |
Correct |
1862 ms |
3896 KB |
Output is correct |
21 |
Correct |
1827 ms |
4444 KB |
Output is correct |
22 |
Execution timed out |
2039 ms |
4744 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |