#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define endl '\n'
using namespace std;
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int L, Q;
cin >> L >> Q;
string s;
cin >> s;
// in bm0, 1 = ?; in bm1, 0 = ?
int prv = 0, cur = 1;
vector<vector<int>> bm0(1 << L, vector<int>(2)), bm1(1 << L, vector<int>(2));
vector<int> val(1 << L, 0);
for (int i = 0; i < s.size(); i++)
{
val[i] = s[i] - '0';
bm0[i][prv] = bm1[i][prv] = val[i];
}
for (int j = 1; j <= L; j++)
{
for (int i = 0; i < (1 << L); i++)
{
if (i & (1 << (j - 1)))
{
bm0[i][cur] = bm0[i][prv] + bm0[i ^ (1 << (j - 1))][prv];
bm1[i][cur] = bm1[i][prv];
}
else
{
bm0[i][cur] = bm0[i][prv];
bm1[i][cur] = bm1[i][prv] + bm1[i ^ (1 << (j - 1))][prv];
}
}
swap(cur, prv);
}
swap(cur, prv);
while (Q--)
{
string t;
cin >> t;
int sz = t.size() - 1;
int cnt_0 = 0, cnt_1 = 0, cnt_q = 0;
vector<int> p0, p1, pq;
for (int i = 0; i < t.size(); i++)
if (t[i] == '0')
{
cnt_0++;
p0.push_back(i);
}
else if (t[i] == '1')
{
cnt_1++;
p1.push_back(i);
}
else
{
cnt_q++;
pq.push_back(i);
}
int mn = min({cnt_0, cnt_1, cnt_q});
if (mn == cnt_q)
{
int bit = 0;
for (int j = 0; j < t.size(); j++)
{
if (t[j] == '1')
bit |= (1 << (sz - j));
}
int res = 0;
for (int add = 0; add < (1 << cnt_q); add++)
{
int bit2 = bit;
for (int j = 0; j < cnt_q; j++)
{
if (add & (1 << j))
bit2 |= (1 << (sz - pq[j]));
}
res += val[bit2];
}
cout << res << endl;
}
else if (mn == cnt_0)
{
// we do PIE
int bit = 0;
for (int j = 0; j < t.size(); j++)
{
if (t[j] == '1')
bit |= (1 << (sz - j));
}
int res = 0;
for (int add = 0; add < (1 << cnt_0); add++)
{
int bit2 = bit;
int cnt = __builtin_popcount(add);
for (int j = 0; j < cnt_0; j++)
{
if (add & (1 << j))
bit2 |= (1 << (sz - p0[j]));
}
res += (cnt % 2 == 1 ? -1 : 1) * bm1[bit2][cur];
}
cout << res << endl;
}
else
{
// we do PIE too
int bit = 0;
for (int j = 0; j < t.size(); j++)
{
if (t[j] == '1' || t[j] == '?')
bit |= (1 << (sz - j));
}
int res = 0;
for (int add = 0; add < (1 << cnt_1); add++)
{
int bit2 = bit;
int cnt = __builtin_popcount(add);
for (int j = 0; j < cnt_1; j++)
{
if (add & (1 << j))
bit2 ^= (1 << (sz - p1[j]));
}
res += (cnt % 2 == 1 ? -1 : 1) * bm0[bit2][cur];
}
cout << res << endl;
}
}
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:23:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
snake_escaping.cpp:57:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i = 0; i < t.size(); i++)
| ~~^~~~~~~~~~
snake_escaping.cpp:78:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int j = 0; j < t.size(); j++)
| ~~^~~~~~~~~~
snake_escaping.cpp:103:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int j = 0; j < t.size(); j++)
| ~~^~~~~~~~~~
snake_escaping.cpp:129:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for (int j = 0; j < t.size(); j++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
335 ms |
4584 KB |
Output is correct |
12 |
Correct |
338 ms |
4168 KB |
Output is correct |
13 |
Correct |
387 ms |
3480 KB |
Output is correct |
14 |
Correct |
380 ms |
3400 KB |
Output is correct |
15 |
Correct |
345 ms |
4424 KB |
Output is correct |
16 |
Correct |
422 ms |
3564 KB |
Output is correct |
17 |
Correct |
436 ms |
3656 KB |
Output is correct |
18 |
Correct |
226 ms |
5448 KB |
Output is correct |
19 |
Correct |
287 ms |
2376 KB |
Output is correct |
20 |
Correct |
322 ms |
4096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
335 ms |
4584 KB |
Output is correct |
12 |
Correct |
338 ms |
4168 KB |
Output is correct |
13 |
Correct |
387 ms |
3480 KB |
Output is correct |
14 |
Correct |
380 ms |
3400 KB |
Output is correct |
15 |
Correct |
345 ms |
4424 KB |
Output is correct |
16 |
Correct |
422 ms |
3564 KB |
Output is correct |
17 |
Correct |
436 ms |
3656 KB |
Output is correct |
18 |
Correct |
226 ms |
5448 KB |
Output is correct |
19 |
Correct |
287 ms |
2376 KB |
Output is correct |
20 |
Correct |
322 ms |
4096 KB |
Output is correct |
21 |
Correct |
364 ms |
5192 KB |
Output is correct |
22 |
Correct |
396 ms |
5448 KB |
Output is correct |
23 |
Correct |
500 ms |
4416 KB |
Output is correct |
24 |
Correct |
483 ms |
4424 KB |
Output is correct |
25 |
Correct |
427 ms |
6216 KB |
Output is correct |
26 |
Correct |
555 ms |
4704 KB |
Output is correct |
27 |
Correct |
560 ms |
4788 KB |
Output is correct |
28 |
Correct |
232 ms |
7248 KB |
Output is correct |
29 |
Correct |
353 ms |
3364 KB |
Output is correct |
30 |
Correct |
436 ms |
5380 KB |
Output is correct |
31 |
Correct |
520 ms |
5204 KB |
Output is correct |
32 |
Correct |
560 ms |
5192 KB |
Output is correct |
33 |
Correct |
413 ms |
4168 KB |
Output is correct |
34 |
Correct |
520 ms |
4472 KB |
Output is correct |
35 |
Correct |
548 ms |
4648 KB |
Output is correct |
36 |
Correct |
198 ms |
3400 KB |
Output is correct |
37 |
Correct |
380 ms |
5192 KB |
Output is correct |
38 |
Correct |
382 ms |
3400 KB |
Output is correct |
39 |
Correct |
497 ms |
4452 KB |
Output is correct |
40 |
Correct |
485 ms |
4420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Runtime error |
48 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
335 ms |
4584 KB |
Output is correct |
12 |
Correct |
338 ms |
4168 KB |
Output is correct |
13 |
Correct |
387 ms |
3480 KB |
Output is correct |
14 |
Correct |
380 ms |
3400 KB |
Output is correct |
15 |
Correct |
345 ms |
4424 KB |
Output is correct |
16 |
Correct |
422 ms |
3564 KB |
Output is correct |
17 |
Correct |
436 ms |
3656 KB |
Output is correct |
18 |
Correct |
226 ms |
5448 KB |
Output is correct |
19 |
Correct |
287 ms |
2376 KB |
Output is correct |
20 |
Correct |
322 ms |
4096 KB |
Output is correct |
21 |
Correct |
364 ms |
5192 KB |
Output is correct |
22 |
Correct |
396 ms |
5448 KB |
Output is correct |
23 |
Correct |
500 ms |
4416 KB |
Output is correct |
24 |
Correct |
483 ms |
4424 KB |
Output is correct |
25 |
Correct |
427 ms |
6216 KB |
Output is correct |
26 |
Correct |
555 ms |
4704 KB |
Output is correct |
27 |
Correct |
560 ms |
4788 KB |
Output is correct |
28 |
Correct |
232 ms |
7248 KB |
Output is correct |
29 |
Correct |
353 ms |
3364 KB |
Output is correct |
30 |
Correct |
436 ms |
5380 KB |
Output is correct |
31 |
Correct |
520 ms |
5204 KB |
Output is correct |
32 |
Correct |
560 ms |
5192 KB |
Output is correct |
33 |
Correct |
413 ms |
4168 KB |
Output is correct |
34 |
Correct |
520 ms |
4472 KB |
Output is correct |
35 |
Correct |
548 ms |
4648 KB |
Output is correct |
36 |
Correct |
198 ms |
3400 KB |
Output is correct |
37 |
Correct |
380 ms |
5192 KB |
Output is correct |
38 |
Correct |
382 ms |
3400 KB |
Output is correct |
39 |
Correct |
497 ms |
4452 KB |
Output is correct |
40 |
Correct |
485 ms |
4420 KB |
Output is correct |
41 |
Runtime error |
48 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |