#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int lgmx = 20;
const int maxn = (1<<lgmx);
int val[maxn];
int sub[maxn];
int sup[maxn];
inline void pvc(const vector<int>& mf)
{
for(int x: mf)
cerr << x << " ";
cerr << endl;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int L, q; cin >> L >> q;
const int n = (1<<L);
for(int i = 0; i < n; i++)
{
char c; cin >> c;
val[i] = c-'0';
sub[i] = val[i];
sup[i] = val[i];
}
for(int j = 0; j < L; j++)
for(int i = 0; i < n; i++)
if((i>>j) & 1)
sub[i] += sub[i^(1<<j)];
for(int j = L-1; j >= 0; j--)
for(int i = n-1; i >= 0; i--)
if((i>>j) & 1)
sup[i^(1<<j)] += sup[i];
//for(int i = 0; i < n; i++)
// cerr << (bitset<3>(i)) << ": " << sub[i] << " " << sup[i] << endl;
vector<int> vec[3];
const int l3 = L/3;
while(q--)
{
for(int i = 0; i < 3; i++) vec[i].clear();
string s; cin >> s;
reverse(s.begin(), s.end());
for(int i = 0; i < L; i++)
{
if(s[i] == '?')
vec[2].push_back(i);
else
vec[s[i]-'0'].push_back(i);
}
if((int)(vec[0].size()) <= l3)
{
int a = vec[0].size();
int k = 0;
for(int j: vec[1]) k ^= (1<<j);
int ans = 0;
for(int i = 0; i < (1<<a); i++)
{
int x = k;
for(int j = 0; j < a; j++)
if((i>>j) & 1)
x ^= (1<<vec[0][j]);
if(__builtin_popcount(i) & 1)
ans -= sup[x];
else
ans += sup[x];
}
cout << ans << endl;
}
else if((int)(vec[1].size()) <= l3)
{
int a = vec[1].size();
int k = n-1;
for(int j: vec[0]) k ^= (1<<j);
int ans = 0;
for(int i = 0; i < (1<<a); i++)
{
int x = k;
for(int j = 0; j < a; j++)
if(! ((i>>j) & 1) )
x ^= (1<<vec[1][j]);
if( (a-__builtin_popcount(i)) & 1)
ans -= sub[x];
else
ans += sub[x];
}
cout << ans << endl;
}
else
{
//cerr << "here" << endl;
int a = vec[2].size();
int k = 0;
for(int j: vec[1]) k ^= (1<<j);
int ans = 0;
//cerr << (bitset<4>(k)) << endl;
for(int i = 0; i < (1<<a); i++)
{
int x = k;
for(int j = 0; j < a; j++)
if((i>>j)&1)
x ^= (1<<vec[2][j]);
ans += val[x];
}
cout << ans << endl;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
316 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
316 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1513 ms |
15056 KB |
Output is correct |
12 |
Correct |
1587 ms |
14704 KB |
Output is correct |
13 |
Correct |
1530 ms |
14060 KB |
Output is correct |
14 |
Correct |
1616 ms |
13976 KB |
Output is correct |
15 |
Correct |
1463 ms |
15148 KB |
Output is correct |
16 |
Correct |
1559 ms |
14260 KB |
Output is correct |
17 |
Correct |
1582 ms |
14328 KB |
Output is correct |
18 |
Correct |
1384 ms |
15992 KB |
Output is correct |
19 |
Correct |
1523 ms |
13084 KB |
Output is correct |
20 |
Correct |
1445 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
316 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1513 ms |
15056 KB |
Output is correct |
12 |
Correct |
1587 ms |
14704 KB |
Output is correct |
13 |
Correct |
1530 ms |
14060 KB |
Output is correct |
14 |
Correct |
1616 ms |
13976 KB |
Output is correct |
15 |
Correct |
1463 ms |
15148 KB |
Output is correct |
16 |
Correct |
1559 ms |
14260 KB |
Output is correct |
17 |
Correct |
1582 ms |
14328 KB |
Output is correct |
18 |
Correct |
1384 ms |
15992 KB |
Output is correct |
19 |
Correct |
1523 ms |
13084 KB |
Output is correct |
20 |
Correct |
1445 ms |
14684 KB |
Output is correct |
21 |
Correct |
1648 ms |
18116 KB |
Output is correct |
22 |
Correct |
1576 ms |
18264 KB |
Output is correct |
23 |
Correct |
1571 ms |
17292 KB |
Output is correct |
24 |
Correct |
1643 ms |
17160 KB |
Output is correct |
25 |
Correct |
1613 ms |
19132 KB |
Output is correct |
26 |
Correct |
1720 ms |
17564 KB |
Output is correct |
27 |
Correct |
1682 ms |
17484 KB |
Output is correct |
28 |
Correct |
1405 ms |
20124 KB |
Output is correct |
29 |
Correct |
1437 ms |
16052 KB |
Output is correct |
30 |
Correct |
1550 ms |
18332 KB |
Output is correct |
31 |
Correct |
1604 ms |
18004 KB |
Output is correct |
32 |
Correct |
1697 ms |
18048 KB |
Output is correct |
33 |
Correct |
1583 ms |
17024 KB |
Output is correct |
34 |
Correct |
1689 ms |
17236 KB |
Output is correct |
35 |
Correct |
1621 ms |
17616 KB |
Output is correct |
36 |
Correct |
1454 ms |
16108 KB |
Output is correct |
37 |
Correct |
1485 ms |
18008 KB |
Output is correct |
38 |
Correct |
1463 ms |
16024 KB |
Output is correct |
39 |
Correct |
1526 ms |
17244 KB |
Output is correct |
40 |
Correct |
1601 ms |
17128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
316 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
137 ms |
14788 KB |
Output is correct |
12 |
Correct |
142 ms |
14956 KB |
Output is correct |
13 |
Correct |
164 ms |
14760 KB |
Output is correct |
14 |
Correct |
174 ms |
14808 KB |
Output is correct |
15 |
Correct |
150 ms |
14932 KB |
Output is correct |
16 |
Correct |
185 ms |
14872 KB |
Output is correct |
17 |
Correct |
189 ms |
14788 KB |
Output is correct |
18 |
Correct |
135 ms |
15112 KB |
Output is correct |
19 |
Correct |
140 ms |
14660 KB |
Output is correct |
20 |
Correct |
156 ms |
14880 KB |
Output is correct |
21 |
Correct |
162 ms |
14896 KB |
Output is correct |
22 |
Correct |
193 ms |
14868 KB |
Output is correct |
23 |
Correct |
152 ms |
14812 KB |
Output is correct |
24 |
Correct |
200 ms |
14892 KB |
Output is correct |
25 |
Correct |
181 ms |
14812 KB |
Output is correct |
26 |
Correct |
152 ms |
14696 KB |
Output is correct |
27 |
Correct |
138 ms |
14812 KB |
Output is correct |
28 |
Correct |
187 ms |
14744 KB |
Output is correct |
29 |
Correct |
162 ms |
14864 KB |
Output is correct |
30 |
Correct |
218 ms |
14752 KB |
Output is correct |
31 |
Correct |
142 ms |
14788 KB |
Output is correct |
32 |
Correct |
192 ms |
14772 KB |
Output is correct |
33 |
Correct |
190 ms |
14740 KB |
Output is correct |
34 |
Correct |
132 ms |
14632 KB |
Output is correct |
35 |
Correct |
173 ms |
14788 KB |
Output is correct |
36 |
Correct |
187 ms |
14792 KB |
Output is correct |
37 |
Correct |
170 ms |
14836 KB |
Output is correct |
38 |
Correct |
175 ms |
14916 KB |
Output is correct |
39 |
Correct |
169 ms |
14800 KB |
Output is correct |
40 |
Correct |
175 ms |
14852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
316 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1513 ms |
15056 KB |
Output is correct |
12 |
Correct |
1587 ms |
14704 KB |
Output is correct |
13 |
Correct |
1530 ms |
14060 KB |
Output is correct |
14 |
Correct |
1616 ms |
13976 KB |
Output is correct |
15 |
Correct |
1463 ms |
15148 KB |
Output is correct |
16 |
Correct |
1559 ms |
14260 KB |
Output is correct |
17 |
Correct |
1582 ms |
14328 KB |
Output is correct |
18 |
Correct |
1384 ms |
15992 KB |
Output is correct |
19 |
Correct |
1523 ms |
13084 KB |
Output is correct |
20 |
Correct |
1445 ms |
14684 KB |
Output is correct |
21 |
Correct |
1648 ms |
18116 KB |
Output is correct |
22 |
Correct |
1576 ms |
18264 KB |
Output is correct |
23 |
Correct |
1571 ms |
17292 KB |
Output is correct |
24 |
Correct |
1643 ms |
17160 KB |
Output is correct |
25 |
Correct |
1613 ms |
19132 KB |
Output is correct |
26 |
Correct |
1720 ms |
17564 KB |
Output is correct |
27 |
Correct |
1682 ms |
17484 KB |
Output is correct |
28 |
Correct |
1405 ms |
20124 KB |
Output is correct |
29 |
Correct |
1437 ms |
16052 KB |
Output is correct |
30 |
Correct |
1550 ms |
18332 KB |
Output is correct |
31 |
Correct |
1604 ms |
18004 KB |
Output is correct |
32 |
Correct |
1697 ms |
18048 KB |
Output is correct |
33 |
Correct |
1583 ms |
17024 KB |
Output is correct |
34 |
Correct |
1689 ms |
17236 KB |
Output is correct |
35 |
Correct |
1621 ms |
17616 KB |
Output is correct |
36 |
Correct |
1454 ms |
16108 KB |
Output is correct |
37 |
Correct |
1485 ms |
18008 KB |
Output is correct |
38 |
Correct |
1463 ms |
16024 KB |
Output is correct |
39 |
Correct |
1526 ms |
17244 KB |
Output is correct |
40 |
Correct |
1601 ms |
17128 KB |
Output is correct |
41 |
Correct |
137 ms |
14788 KB |
Output is correct |
42 |
Correct |
142 ms |
14956 KB |
Output is correct |
43 |
Correct |
164 ms |
14760 KB |
Output is correct |
44 |
Correct |
174 ms |
14808 KB |
Output is correct |
45 |
Correct |
150 ms |
14932 KB |
Output is correct |
46 |
Correct |
185 ms |
14872 KB |
Output is correct |
47 |
Correct |
189 ms |
14788 KB |
Output is correct |
48 |
Correct |
135 ms |
15112 KB |
Output is correct |
49 |
Correct |
140 ms |
14660 KB |
Output is correct |
50 |
Correct |
156 ms |
14880 KB |
Output is correct |
51 |
Correct |
162 ms |
14896 KB |
Output is correct |
52 |
Correct |
193 ms |
14868 KB |
Output is correct |
53 |
Correct |
152 ms |
14812 KB |
Output is correct |
54 |
Correct |
200 ms |
14892 KB |
Output is correct |
55 |
Correct |
181 ms |
14812 KB |
Output is correct |
56 |
Correct |
152 ms |
14696 KB |
Output is correct |
57 |
Correct |
138 ms |
14812 KB |
Output is correct |
58 |
Correct |
187 ms |
14744 KB |
Output is correct |
59 |
Correct |
162 ms |
14864 KB |
Output is correct |
60 |
Correct |
218 ms |
14752 KB |
Output is correct |
61 |
Correct |
142 ms |
14788 KB |
Output is correct |
62 |
Correct |
192 ms |
14772 KB |
Output is correct |
63 |
Correct |
190 ms |
14740 KB |
Output is correct |
64 |
Correct |
132 ms |
14632 KB |
Output is correct |
65 |
Correct |
173 ms |
14788 KB |
Output is correct |
66 |
Correct |
187 ms |
14792 KB |
Output is correct |
67 |
Correct |
170 ms |
14836 KB |
Output is correct |
68 |
Correct |
175 ms |
14916 KB |
Output is correct |
69 |
Correct |
169 ms |
14800 KB |
Output is correct |
70 |
Correct |
175 ms |
14852 KB |
Output is correct |
71 |
Correct |
1689 ms |
39336 KB |
Output is correct |
72 |
Correct |
1767 ms |
39272 KB |
Output is correct |
73 |
Execution timed out |
2069 ms |
37908 KB |
Time limit exceeded |
74 |
Halted |
0 ms |
0 KB |
- |