This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#include <bits/debug.hpp>
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Snake Escaping"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e6 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
int N, q;
int a[maxn], f[2][maxn];
int CASE(int type, int P, int L) {
int ans = 0;
for(int x = P; ; x = (x - 1) & P)
{
int w = (cntbit(P) - cntbit(x)) & 1 ? -1 : 1;
ans += w * f[type][x | L];
if(x == 0) break;
}
return ans;
}
void solve()
{
cin >> N >> q;
fi(i, 0, (1 << N) - 1) {
char x; cin >> x;
a[i] = x - '0';
}
fi(i, 0, (1 << N) - 1) {
f[0][(1 << N) - 1 - i] = a[i];
f[1][i] = a[i];
}
fi(j, 0, N - 1) fi(x, 0, (1 << N) - 1) {
if(getbit(x, j))
{
f[0][x] += f[0][x ^ (1 << j)];
f[1][x] += f[1][x ^ (1 << j)];
}
}
fi(_, 1, q)
{
string s; cin >> s;
int A = 0, B = 0, C = 0;
int ans = 0;
fi(i, 0, N - 1) {
if(s[N - 1 - i] == '0') A += (1 << i);
else if(s[N - 1 - i] == '1') B += (1 << i);
else C += (1 << i);
}
int cA = cntbit(A), cB = cntbit(B), cC = cntbit(C);
if(cA <= min(cB, cC)) ans = CASE(0, A, C);
else if(cB <= min(cA, cC)) ans = CASE(1, B, C);
else
{
for(int x = C; ; x = (x - 1) & C) {
ans += a[x | B];
if(x == 0) break;
}
}
cout << ans << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message (stderr)
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |