#include <bits/stdc++.h>
using namespace std;
const int Nmax = (1<<20);
int n, q;
string Val;
int val[Nmax], up[Nmax], down[Nmax], bits[Nmax];
void precalc()
{
int i, j;
for(i=0; i<(1<<n); ++i)
bits[i] = __builtin_popcount(i), up[i] = down[i] = val[i] = Val[i] - '0';
for(i=0; i<n; ++i)
for(j=0; j<(1<<n); ++j)
if(j & (1<<i))
up[j ^ (1<<i)] += up[j];
else down[j ^ (1<<i)] += down[j];
}
int solveQ(int mask, int que)
{
int i, ans = 0;
for(i=que; ; i = (i-1) & que)
{
ans += val[i ^ mask];
if(i == 0) return ans;
}
}
int solve0(int mask0, int mask1, int maskq)
{
int i, ans = 0;
for(i=mask0; ; i = (i-1) & mask0)
{
if(bits[i] & 1) ans -= up[mask1 ^ i];
else ans += up[mask1 ^ i];
if(i == 0) return ans;
}
}
int solve1(int mask0, int mask1, int maskq)
{
int i, ans = 0;
for(i=mask1; ; i = (i-1) & mask1)
{
if(bits[i] & 1) ans -= down[maskq ^ mask1 ^ i];
else ans += down[maskq ^ mask1 ^ i];
if(i == 0) return ans;
}
}
int query()
{
string S;
cin >> S;
reverse(S.begin(), S.end());
int i, mask0 = 0, mask1 = 0, maskq = 0;
for(i=0; i<n; ++i)
{
if(S[i] == '0') mask0 ^= (1<<i);
else if(S[i] == '1') mask1 ^= (1<<i);
else maskq ^= (1<<i);
}
if(bits[maskq] <= bits[mask0] && bits[maskq] <= bits[mask1]) return solveQ(mask1, maskq);
else if(bits[mask0] < bits[mask1]) return solve0(mask0, mask1, maskq);
else return solve1(mask0, mask1, maskq);
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false);
cin >> n >> q;
cin >> Val;
precalc();
while(q--)
cout << query() << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
1424 ms |
15176 KB |
Output is correct |
12 |
Correct |
1419 ms |
14976 KB |
Output is correct |
13 |
Correct |
1460 ms |
14220 KB |
Output is correct |
14 |
Correct |
1439 ms |
14328 KB |
Output is correct |
15 |
Correct |
1444 ms |
15224 KB |
Output is correct |
16 |
Correct |
1461 ms |
14328 KB |
Output is correct |
17 |
Correct |
1459 ms |
14444 KB |
Output is correct |
18 |
Correct |
1388 ms |
16248 KB |
Output is correct |
19 |
Correct |
1400 ms |
13432 KB |
Output is correct |
20 |
Correct |
1433 ms |
14784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
1424 ms |
15176 KB |
Output is correct |
12 |
Correct |
1419 ms |
14976 KB |
Output is correct |
13 |
Correct |
1460 ms |
14220 KB |
Output is correct |
14 |
Correct |
1439 ms |
14328 KB |
Output is correct |
15 |
Correct |
1444 ms |
15224 KB |
Output is correct |
16 |
Correct |
1461 ms |
14328 KB |
Output is correct |
17 |
Correct |
1459 ms |
14444 KB |
Output is correct |
18 |
Correct |
1388 ms |
16248 KB |
Output is correct |
19 |
Correct |
1400 ms |
13432 KB |
Output is correct |
20 |
Correct |
1433 ms |
14784 KB |
Output is correct |
21 |
Correct |
1447 ms |
18200 KB |
Output is correct |
22 |
Correct |
1471 ms |
18556 KB |
Output is correct |
23 |
Correct |
1476 ms |
17384 KB |
Output is correct |
24 |
Correct |
1484 ms |
17108 KB |
Output is correct |
25 |
Correct |
1484 ms |
19124 KB |
Output is correct |
26 |
Correct |
1514 ms |
17764 KB |
Output is correct |
27 |
Correct |
1519 ms |
17656 KB |
Output is correct |
28 |
Correct |
1440 ms |
20228 KB |
Output is correct |
29 |
Correct |
1466 ms |
16396 KB |
Output is correct |
30 |
Correct |
1501 ms |
18488 KB |
Output is correct |
31 |
Correct |
1476 ms |
18204 KB |
Output is correct |
32 |
Correct |
1495 ms |
18296 KB |
Output is correct |
33 |
Correct |
1455 ms |
17144 KB |
Output is correct |
34 |
Correct |
1521 ms |
17216 KB |
Output is correct |
35 |
Correct |
1547 ms |
17844 KB |
Output is correct |
36 |
Correct |
1381 ms |
16392 KB |
Output is correct |
37 |
Correct |
1440 ms |
18296 KB |
Output is correct |
38 |
Correct |
1448 ms |
16376 KB |
Output is correct |
39 |
Correct |
1525 ms |
17508 KB |
Output is correct |
40 |
Correct |
1490 ms |
17336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
135 ms |
20324 KB |
Output is correct |
12 |
Correct |
133 ms |
20440 KB |
Output is correct |
13 |
Correct |
139 ms |
20120 KB |
Output is correct |
14 |
Correct |
152 ms |
20152 KB |
Output is correct |
15 |
Correct |
129 ms |
20380 KB |
Output is correct |
16 |
Correct |
164 ms |
20152 KB |
Output is correct |
17 |
Correct |
154 ms |
20160 KB |
Output is correct |
18 |
Correct |
118 ms |
20392 KB |
Output is correct |
19 |
Correct |
131 ms |
20116 KB |
Output is correct |
20 |
Correct |
132 ms |
20248 KB |
Output is correct |
21 |
Correct |
144 ms |
20244 KB |
Output is correct |
22 |
Correct |
157 ms |
20248 KB |
Output is correct |
23 |
Correct |
138 ms |
20416 KB |
Output is correct |
24 |
Correct |
158 ms |
20216 KB |
Output is correct |
25 |
Correct |
151 ms |
20248 KB |
Output is correct |
26 |
Correct |
117 ms |
20120 KB |
Output is correct |
27 |
Correct |
131 ms |
20236 KB |
Output is correct |
28 |
Correct |
132 ms |
20124 KB |
Output is correct |
29 |
Correct |
140 ms |
20220 KB |
Output is correct |
30 |
Correct |
144 ms |
20168 KB |
Output is correct |
31 |
Correct |
134 ms |
20184 KB |
Output is correct |
32 |
Correct |
167 ms |
20236 KB |
Output is correct |
33 |
Correct |
153 ms |
20172 KB |
Output is correct |
34 |
Correct |
118 ms |
20012 KB |
Output is correct |
35 |
Correct |
148 ms |
20144 KB |
Output is correct |
36 |
Correct |
149 ms |
20248 KB |
Output is correct |
37 |
Correct |
150 ms |
20120 KB |
Output is correct |
38 |
Correct |
145 ms |
20244 KB |
Output is correct |
39 |
Correct |
147 ms |
20376 KB |
Output is correct |
40 |
Correct |
141 ms |
20172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
1424 ms |
15176 KB |
Output is correct |
12 |
Correct |
1419 ms |
14976 KB |
Output is correct |
13 |
Correct |
1460 ms |
14220 KB |
Output is correct |
14 |
Correct |
1439 ms |
14328 KB |
Output is correct |
15 |
Correct |
1444 ms |
15224 KB |
Output is correct |
16 |
Correct |
1461 ms |
14328 KB |
Output is correct |
17 |
Correct |
1459 ms |
14444 KB |
Output is correct |
18 |
Correct |
1388 ms |
16248 KB |
Output is correct |
19 |
Correct |
1400 ms |
13432 KB |
Output is correct |
20 |
Correct |
1433 ms |
14784 KB |
Output is correct |
21 |
Correct |
1447 ms |
18200 KB |
Output is correct |
22 |
Correct |
1471 ms |
18556 KB |
Output is correct |
23 |
Correct |
1476 ms |
17384 KB |
Output is correct |
24 |
Correct |
1484 ms |
17108 KB |
Output is correct |
25 |
Correct |
1484 ms |
19124 KB |
Output is correct |
26 |
Correct |
1514 ms |
17764 KB |
Output is correct |
27 |
Correct |
1519 ms |
17656 KB |
Output is correct |
28 |
Correct |
1440 ms |
20228 KB |
Output is correct |
29 |
Correct |
1466 ms |
16396 KB |
Output is correct |
30 |
Correct |
1501 ms |
18488 KB |
Output is correct |
31 |
Correct |
1476 ms |
18204 KB |
Output is correct |
32 |
Correct |
1495 ms |
18296 KB |
Output is correct |
33 |
Correct |
1455 ms |
17144 KB |
Output is correct |
34 |
Correct |
1521 ms |
17216 KB |
Output is correct |
35 |
Correct |
1547 ms |
17844 KB |
Output is correct |
36 |
Correct |
1381 ms |
16392 KB |
Output is correct |
37 |
Correct |
1440 ms |
18296 KB |
Output is correct |
38 |
Correct |
1448 ms |
16376 KB |
Output is correct |
39 |
Correct |
1525 ms |
17508 KB |
Output is correct |
40 |
Correct |
1490 ms |
17336 KB |
Output is correct |
41 |
Correct |
135 ms |
20324 KB |
Output is correct |
42 |
Correct |
133 ms |
20440 KB |
Output is correct |
43 |
Correct |
139 ms |
20120 KB |
Output is correct |
44 |
Correct |
152 ms |
20152 KB |
Output is correct |
45 |
Correct |
129 ms |
20380 KB |
Output is correct |
46 |
Correct |
164 ms |
20152 KB |
Output is correct |
47 |
Correct |
154 ms |
20160 KB |
Output is correct |
48 |
Correct |
118 ms |
20392 KB |
Output is correct |
49 |
Correct |
131 ms |
20116 KB |
Output is correct |
50 |
Correct |
132 ms |
20248 KB |
Output is correct |
51 |
Correct |
144 ms |
20244 KB |
Output is correct |
52 |
Correct |
157 ms |
20248 KB |
Output is correct |
53 |
Correct |
138 ms |
20416 KB |
Output is correct |
54 |
Correct |
158 ms |
20216 KB |
Output is correct |
55 |
Correct |
151 ms |
20248 KB |
Output is correct |
56 |
Correct |
117 ms |
20120 KB |
Output is correct |
57 |
Correct |
131 ms |
20236 KB |
Output is correct |
58 |
Correct |
132 ms |
20124 KB |
Output is correct |
59 |
Correct |
140 ms |
20220 KB |
Output is correct |
60 |
Correct |
144 ms |
20168 KB |
Output is correct |
61 |
Correct |
134 ms |
20184 KB |
Output is correct |
62 |
Correct |
167 ms |
20236 KB |
Output is correct |
63 |
Correct |
153 ms |
20172 KB |
Output is correct |
64 |
Correct |
118 ms |
20012 KB |
Output is correct |
65 |
Correct |
148 ms |
20144 KB |
Output is correct |
66 |
Correct |
149 ms |
20248 KB |
Output is correct |
67 |
Correct |
150 ms |
20120 KB |
Output is correct |
68 |
Correct |
145 ms |
20244 KB |
Output is correct |
69 |
Correct |
147 ms |
20376 KB |
Output is correct |
70 |
Correct |
141 ms |
20172 KB |
Output is correct |
71 |
Correct |
1721 ms |
44548 KB |
Output is correct |
72 |
Correct |
1769 ms |
44808 KB |
Output is correct |
73 |
Correct |
1961 ms |
43412 KB |
Output is correct |
74 |
Execution timed out |
2029 ms |
41316 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |