#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using vi = vector<int>;
#define sz(x) int(x.size())
int main()
{
int L, Q;
cin >> L >> Q;
vi A((1<<L));
for(int i = 0; i < (1<<L); i++)
{
char c;
cin >> c;
A[i] = c - '0';
}
vi lowersum = A;
vi uppersum = A;
reverse(uppersum.begin(), uppersum.end());
for(int b = 0; b < L; b++)
{
for(int i = 0; i < (1<<L); i++)
{
if(i & (1<<b))
{
lowersum[i] += lowersum[i ^ (1<<b)];
uppersum[i] += uppersum[i ^ (1<<b)];
}
}
}
vi getbit((1<<L));
for(int i = 0; i < L; i++)
getbit[(1<<i)] = i;
for(int j = 0; j < Q; j++)
{
vi p0, p1, pq;
int onemask = 0;
int qmask = 0;
int zeromask = 0;
for(int b = L-1; b >= 0; b--)
{
char c;
cin >> c;
if(c == '0')
{
p0.push_back(b);
zeromask += (1<<b);
}
else if(c == '1')
{
p1.push_back(b);
onemask += (1<<b);
}
else
{
pq.push_back(b);
qmask += (1<<b);
}
}
int res = 0;
if(sz(p1) <= -1)
{
// cerr << "p1 size = " << sz(p1) << '\n';
vi ssum((1 << sz(p1)), 0);
vi sign((1 << sz(p1)), +1);
if(sz(p1) % 2 == 0)
sign[0] = +1;
else
sign[0] = -1;
for(int m = 1; m < (1<<sz(p1)); m++)
{
ssum[m] = ssum[m - (m&-m)] + (1 << p1[getbit[m&-m]]);
sign[m] = -1 * sign[m - (m&-m)];
}
for(int m = 0; m < (1<<sz(p1)); m++)
{
// cerr << "m = " << m << '\n';
// cerr << "sign = " << sign[m] << '\n';
res += lowersum[qmask | ssum[m]] * sign[m];
}
}
else if(sz(p0) <= 10)
{
// cerr << "p0 size = " << sz(p0) << '\n';
vi ssum((1 << sz(p0)), 0);
vi sign((1 << sz(p0)), +1);
if(sz(p0) % 2 == 0)
sign[0] = +1;
else
sign[0] = -1;
for(int m = 1; m < (1<<sz(p0)); m++)
{
ssum[m] = ssum[m - (m&-m)] + (1 << p0[getbit[m&-m]]);
sign[m] = -1 * sign[m - (m&-m)];
}
// cerr << "qmask = " << qmask << '\n';
for(int m = 0; m < (1<<sz(p0)); m++)
{
// cerr << m << " -> " << ssum[m] << '\n';
res += uppersum[qmask | ssum[m]] * sign[m];
}
}
else// if(sz(pq) <= 10)
{
vi ssum((1 << sz(pq)), 0);
for(int m = 1; m < (1<<sz(pq)); m++)
ssum[m] = ssum[m - (m&-m)] + (1 << pq[getbit[m&-m]]);
for(int m = 0; m < (1<<sz(pq)); m++)
res += A[onemask | ssum[m]];
}
// cerr << test << ' ' << mask << '\n';
cout << res << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
212 KB |
Output is correct |
8 |
Correct |
3 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
212 KB |
Output is correct |
8 |
Correct |
3 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
212 KB |
Output is correct |
11 |
Execution timed out |
2074 ms |
5048 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
212 KB |
Output is correct |
8 |
Correct |
3 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
212 KB |
Output is correct |
11 |
Execution timed out |
2074 ms |
5048 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
212 KB |
Output is correct |
8 |
Correct |
3 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
212 KB |
Output is correct |
11 |
Correct |
264 ms |
16908 KB |
Output is correct |
12 |
Correct |
341 ms |
17016 KB |
Output is correct |
13 |
Correct |
517 ms |
16844 KB |
Output is correct |
14 |
Correct |
294 ms |
16844 KB |
Output is correct |
15 |
Correct |
239 ms |
16944 KB |
Output is correct |
16 |
Correct |
279 ms |
16936 KB |
Output is correct |
17 |
Correct |
298 ms |
16880 KB |
Output is correct |
18 |
Correct |
225 ms |
17108 KB |
Output is correct |
19 |
Correct |
493 ms |
16920 KB |
Output is correct |
20 |
Correct |
255 ms |
16940 KB |
Output is correct |
21 |
Correct |
248 ms |
16956 KB |
Output is correct |
22 |
Correct |
259 ms |
16876 KB |
Output is correct |
23 |
Correct |
251 ms |
16820 KB |
Output is correct |
24 |
Correct |
314 ms |
16984 KB |
Output is correct |
25 |
Correct |
290 ms |
16940 KB |
Output is correct |
26 |
Correct |
236 ms |
16808 KB |
Output is correct |
27 |
Correct |
530 ms |
16964 KB |
Output is correct |
28 |
Correct |
347 ms |
16836 KB |
Output is correct |
29 |
Correct |
260 ms |
16816 KB |
Output is correct |
30 |
Correct |
280 ms |
16888 KB |
Output is correct |
31 |
Correct |
272 ms |
16808 KB |
Output is correct |
32 |
Correct |
330 ms |
16940 KB |
Output is correct |
33 |
Correct |
300 ms |
16916 KB |
Output is correct |
34 |
Correct |
235 ms |
16824 KB |
Output is correct |
35 |
Correct |
302 ms |
16960 KB |
Output is correct |
36 |
Correct |
347 ms |
16936 KB |
Output is correct |
37 |
Correct |
291 ms |
16856 KB |
Output is correct |
38 |
Correct |
316 ms |
16956 KB |
Output is correct |
39 |
Correct |
321 ms |
16920 KB |
Output is correct |
40 |
Correct |
300 ms |
16824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
212 KB |
Output is correct |
8 |
Correct |
3 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
212 KB |
Output is correct |
11 |
Execution timed out |
2074 ms |
5048 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |