// #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 double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,q;
vector<int> dp, dpr, a;
int RVSR;
inline void solve() {
cin>>n>>q;
dp.assign((1 << n), 0);
dpr.assign((1 << n), 0);
a.assign((1 << n), 0);
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);
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();
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
Compilation message
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:72:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i < occ.size(); i++) {
| ~~^~~~~~~~~~~~
snake_escaping.cpp:91:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 0; i < occ.size(); i++) {
| ~~^~~~~~~~~~~~
snake_escaping.cpp:116:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for(int i = 0; i < occ.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1260 ms |
10272 KB |
Output is correct |
12 |
Correct |
1292 ms |
9848 KB |
Output is correct |
13 |
Correct |
1480 ms |
9152 KB |
Output is correct |
14 |
Correct |
1473 ms |
8900 KB |
Output is correct |
15 |
Correct |
1335 ms |
10068 KB |
Output is correct |
16 |
Correct |
1440 ms |
9252 KB |
Output is correct |
17 |
Correct |
1439 ms |
9220 KB |
Output is correct |
18 |
Correct |
1212 ms |
5528 KB |
Output is correct |
19 |
Correct |
1608 ms |
2700 KB |
Output is correct |
20 |
Correct |
1257 ms |
4084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1260 ms |
10272 KB |
Output is correct |
12 |
Correct |
1292 ms |
9848 KB |
Output is correct |
13 |
Correct |
1480 ms |
9152 KB |
Output is correct |
14 |
Correct |
1473 ms |
8900 KB |
Output is correct |
15 |
Correct |
1335 ms |
10068 KB |
Output is correct |
16 |
Correct |
1440 ms |
9252 KB |
Output is correct |
17 |
Correct |
1439 ms |
9220 KB |
Output is correct |
18 |
Correct |
1212 ms |
5528 KB |
Output is correct |
19 |
Correct |
1608 ms |
2700 KB |
Output is correct |
20 |
Correct |
1257 ms |
4084 KB |
Output is correct |
21 |
Correct |
1278 ms |
4400 KB |
Output is correct |
22 |
Correct |
1347 ms |
4712 KB |
Output is correct |
23 |
Correct |
1566 ms |
3692 KB |
Output is correct |
24 |
Correct |
1523 ms |
3684 KB |
Output is correct |
25 |
Correct |
1373 ms |
5544 KB |
Output is correct |
26 |
Correct |
1509 ms |
3904 KB |
Output is correct |
27 |
Correct |
1569 ms |
4004 KB |
Output is correct |
28 |
Correct |
1233 ms |
6392 KB |
Output is correct |
29 |
Execution timed out |
2024 ms |
2552 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
109 ms |
12876 KB |
Output is correct |
12 |
Runtime error |
42 ms |
25424 KB |
Execution killed with signal 11 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1260 ms |
10272 KB |
Output is correct |
12 |
Correct |
1292 ms |
9848 KB |
Output is correct |
13 |
Correct |
1480 ms |
9152 KB |
Output is correct |
14 |
Correct |
1473 ms |
8900 KB |
Output is correct |
15 |
Correct |
1335 ms |
10068 KB |
Output is correct |
16 |
Correct |
1440 ms |
9252 KB |
Output is correct |
17 |
Correct |
1439 ms |
9220 KB |
Output is correct |
18 |
Correct |
1212 ms |
5528 KB |
Output is correct |
19 |
Correct |
1608 ms |
2700 KB |
Output is correct |
20 |
Correct |
1257 ms |
4084 KB |
Output is correct |
21 |
Correct |
1278 ms |
4400 KB |
Output is correct |
22 |
Correct |
1347 ms |
4712 KB |
Output is correct |
23 |
Correct |
1566 ms |
3692 KB |
Output is correct |
24 |
Correct |
1523 ms |
3684 KB |
Output is correct |
25 |
Correct |
1373 ms |
5544 KB |
Output is correct |
26 |
Correct |
1509 ms |
3904 KB |
Output is correct |
27 |
Correct |
1569 ms |
4004 KB |
Output is correct |
28 |
Correct |
1233 ms |
6392 KB |
Output is correct |
29 |
Execution timed out |
2024 ms |
2552 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |