// #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++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4440 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4440 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1249 ms |
8552 KB |
Output is correct |
12 |
Correct |
1282 ms |
8324 KB |
Output is correct |
13 |
Correct |
1444 ms |
7624 KB |
Output is correct |
14 |
Correct |
1410 ms |
7760 KB |
Output is correct |
15 |
Correct |
1331 ms |
8464 KB |
Output is correct |
16 |
Correct |
1443 ms |
7672 KB |
Output is correct |
17 |
Correct |
1418 ms |
8132 KB |
Output is correct |
18 |
Correct |
1223 ms |
9552 KB |
Output is correct |
19 |
Correct |
1599 ms |
6944 KB |
Output is correct |
20 |
Correct |
1255 ms |
8028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4440 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1249 ms |
8552 KB |
Output is correct |
12 |
Correct |
1282 ms |
8324 KB |
Output is correct |
13 |
Correct |
1444 ms |
7624 KB |
Output is correct |
14 |
Correct |
1410 ms |
7760 KB |
Output is correct |
15 |
Correct |
1331 ms |
8464 KB |
Output is correct |
16 |
Correct |
1443 ms |
7672 KB |
Output is correct |
17 |
Correct |
1418 ms |
8132 KB |
Output is correct |
18 |
Correct |
1223 ms |
9552 KB |
Output is correct |
19 |
Correct |
1599 ms |
6944 KB |
Output is correct |
20 |
Correct |
1255 ms |
8028 KB |
Output is correct |
21 |
Correct |
1275 ms |
8480 KB |
Output is correct |
22 |
Correct |
1339 ms |
8740 KB |
Output is correct |
23 |
Correct |
1570 ms |
7904 KB |
Output is correct |
24 |
Correct |
1531 ms |
7636 KB |
Output is correct |
25 |
Correct |
1411 ms |
9604 KB |
Output is correct |
26 |
Correct |
1516 ms |
8120 KB |
Output is correct |
27 |
Correct |
1560 ms |
7940 KB |
Output is correct |
28 |
Correct |
1236 ms |
10784 KB |
Output is correct |
29 |
Execution timed out |
2035 ms |
6708 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4440 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
102 ms |
12920 KB |
Output is correct |
12 |
Runtime error |
44 ms |
25424 KB |
Execution killed with signal 11 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4440 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1249 ms |
8552 KB |
Output is correct |
12 |
Correct |
1282 ms |
8324 KB |
Output is correct |
13 |
Correct |
1444 ms |
7624 KB |
Output is correct |
14 |
Correct |
1410 ms |
7760 KB |
Output is correct |
15 |
Correct |
1331 ms |
8464 KB |
Output is correct |
16 |
Correct |
1443 ms |
7672 KB |
Output is correct |
17 |
Correct |
1418 ms |
8132 KB |
Output is correct |
18 |
Correct |
1223 ms |
9552 KB |
Output is correct |
19 |
Correct |
1599 ms |
6944 KB |
Output is correct |
20 |
Correct |
1255 ms |
8028 KB |
Output is correct |
21 |
Correct |
1275 ms |
8480 KB |
Output is correct |
22 |
Correct |
1339 ms |
8740 KB |
Output is correct |
23 |
Correct |
1570 ms |
7904 KB |
Output is correct |
24 |
Correct |
1531 ms |
7636 KB |
Output is correct |
25 |
Correct |
1411 ms |
9604 KB |
Output is correct |
26 |
Correct |
1516 ms |
8120 KB |
Output is correct |
27 |
Correct |
1560 ms |
7940 KB |
Output is correct |
28 |
Correct |
1236 ms |
10784 KB |
Output is correct |
29 |
Execution timed out |
2035 ms |
6708 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |