// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int INF = 1e9+500;
const int N = 20;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,q;
array<int, (1 << N)> a, dp, dpr;
int RVSR;
inline void solve() {
cin>>n>>q;
RVSR = (1 << n) - 1;
for(int i = 0; i < (1 << n); i++) {
char tmp;
cin >> tmp;
a[i] = dp[i] = dpr[i ^ RVSR] = tmp - '0';
}
for(int k = 0; k < n; k++) {
for(int i = 0; i < (1 << n); i++) {
if(i & (1 << k)) {
dp[i] += dp[i ^ (1 << k)];
dpr[i] += dpr[i ^ (1 << k)];
}
}
}
// for(int i = 0; i < (1 << n); i++) {
// cout << "i:" << i <<" dp:" << dp[i] << " dpr:" << dpr[i] << "\n";
// }
REP(z, q) {
string s;
cin >> s;
reverse(all(s));
int cnt0 = 0, cnt1 = 0;
for(char c : s) {
if(c == '0') cnt0++;
else if(c == '1') cnt1++;
}
int ans = 0;
if(cnt1 + cnt0 >= 14) {
int x = 0;
vector<int> occ;
for(int i = 0; i < n; i++) {
if(s[i] == '?') occ.pb(i);
else if(s[i] == '1') x += (1 << i);
}
for(int mask = 0; mask < (1 << occ.size()); mask++) {
for(int i = 0; i < occ.size(); i++) {
if(mask & (1 << i)) {
x += (1 << occ[i]);
}
}
ans += a[x];
}
}
else if(cnt1 > cnt0) { // use reverse
int x = 0;
vector<int> occ;
for(int i = 0; i < n; i++) {
if(s[i] != '1') x += (1 << i);
if(s[i] == '0') {
occ.pb(i);
}
}
for(int mask = 0; mask < (1 << ((int)occ.size())); mask++) {
int cur = x;
for(int i = 0; i < occ.size(); i++) {
if(mask & (1 << i)) {
cur ^= 1 << (occ[i]);
}
}
if(__popcount(mask) & 1) {
ans -= dpr[cur];
}
else {
ans += dpr[cur];
}
}
}
else {
int x = 0;
vector<int> occ;
for(int i = 0; i < n; i++) {
if(s[i] != '0') x += (1 << i);
if(s[i] == '1') {
occ.pb(i);
}
}
for(int mask = 0; mask < (1 << ((int)occ.size())); mask++) {
int cur = x;
// cout << "mask:" << mask << " cur:" << cur << "\n";
for(int i = 0; i < occ.size(); i++) {
if(mask & (1 << i)) {
cur ^= 1 << (occ[i]);
}
}
if(__popcount(mask) & 1) {
ans -= dp[cur];
}
else {
ans += dp[cur];
}
}
}
cout << ans << endl;
}
}
signed main() {
fastio();
solve();
}
Compilation message
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:68:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i = 0; i < occ.size(); i++) {
| ~~^~~~~~~~~~~~
snake_escaping.cpp:87:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(int i = 0; i < occ.size(); i++) {
| ~~^~~~~~~~~~~~
snake_escaping.cpp:112:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int i = 0; i < occ.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1391 ms |
8528 KB |
Output is correct |
12 |
Correct |
1339 ms |
8252 KB |
Output is correct |
13 |
Correct |
1507 ms |
7572 KB |
Output is correct |
14 |
Correct |
1517 ms |
7384 KB |
Output is correct |
15 |
Correct |
1424 ms |
8548 KB |
Output is correct |
16 |
Correct |
1483 ms |
7656 KB |
Output is correct |
17 |
Correct |
1508 ms |
7772 KB |
Output is correct |
18 |
Correct |
1202 ms |
9536 KB |
Output is correct |
19 |
Correct |
1776 ms |
6564 KB |
Output is correct |
20 |
Correct |
1269 ms |
8004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1391 ms |
8528 KB |
Output is correct |
12 |
Correct |
1339 ms |
8252 KB |
Output is correct |
13 |
Correct |
1507 ms |
7572 KB |
Output is correct |
14 |
Correct |
1517 ms |
7384 KB |
Output is correct |
15 |
Correct |
1424 ms |
8548 KB |
Output is correct |
16 |
Correct |
1483 ms |
7656 KB |
Output is correct |
17 |
Correct |
1508 ms |
7772 KB |
Output is correct |
18 |
Correct |
1202 ms |
9536 KB |
Output is correct |
19 |
Correct |
1776 ms |
6564 KB |
Output is correct |
20 |
Correct |
1269 ms |
8004 KB |
Output is correct |
21 |
Correct |
1312 ms |
8564 KB |
Output is correct |
22 |
Correct |
1363 ms |
8768 KB |
Output is correct |
23 |
Correct |
1640 ms |
7768 KB |
Output is correct |
24 |
Correct |
1597 ms |
7912 KB |
Output is correct |
25 |
Correct |
1394 ms |
9360 KB |
Output is correct |
26 |
Correct |
1577 ms |
8176 KB |
Output is correct |
27 |
Correct |
1606 ms |
7892 KB |
Output is correct |
28 |
Correct |
1213 ms |
10380 KB |
Output is correct |
29 |
Execution timed out |
2056 ms |
6436 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
100 ms |
12760 KB |
Output is correct |
12 |
Runtime error |
43 ms |
25384 KB |
Execution killed with signal 11 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1391 ms |
8528 KB |
Output is correct |
12 |
Correct |
1339 ms |
8252 KB |
Output is correct |
13 |
Correct |
1507 ms |
7572 KB |
Output is correct |
14 |
Correct |
1517 ms |
7384 KB |
Output is correct |
15 |
Correct |
1424 ms |
8548 KB |
Output is correct |
16 |
Correct |
1483 ms |
7656 KB |
Output is correct |
17 |
Correct |
1508 ms |
7772 KB |
Output is correct |
18 |
Correct |
1202 ms |
9536 KB |
Output is correct |
19 |
Correct |
1776 ms |
6564 KB |
Output is correct |
20 |
Correct |
1269 ms |
8004 KB |
Output is correct |
21 |
Correct |
1312 ms |
8564 KB |
Output is correct |
22 |
Correct |
1363 ms |
8768 KB |
Output is correct |
23 |
Correct |
1640 ms |
7768 KB |
Output is correct |
24 |
Correct |
1597 ms |
7912 KB |
Output is correct |
25 |
Correct |
1394 ms |
9360 KB |
Output is correct |
26 |
Correct |
1577 ms |
8176 KB |
Output is correct |
27 |
Correct |
1606 ms |
7892 KB |
Output is correct |
28 |
Correct |
1213 ms |
10380 KB |
Output is correct |
29 |
Execution timed out |
2056 ms |
6436 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |