# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
42153 | fest | Snake Escaping (JOI18_snake_escaping) | C++14 | 2029 ms | 32348 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// fest
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define FILENAME ""
#define __ fflush(stdout)
typedef long long ll;
typedef long double ld;
using namespace std;
void FREOPEN() {
#ifdef COMP
freopen(".in", "r", stdin);
freopen("1.out", "w", stdout);
#else
freopen(FILENAME".in", "r", stdin);
freopen(FILENAME".out", "w", stdout);
#endif
}
inline double Time() {return (clock() * 1.0) / CLOCKS_PER_SEC; }
const int N = 20, inf = 1e9 * 2, MOD = (int)1e9 + 7;
char CH[N];
const ll INF = 1e18;
const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};
int built[1 << N], ones[21], dp[2][2][1 << N], cost[2][1 << N], qs[21];
int n;
int rev(int x) {
int ret = 0;
for (int i = 0; i < n; i++) {
if (x & (1 << i)) continue;
ret |= (1 << i);
}
return ret;
}
void build(int ty) {
for (int mask = 0; mask < (1 << n); mask++) {
dp[ty][0][mask] += cost[ty][mask];
if (mask & 1) dp[ty][0][mask] += cost[ty][mask ^ 1];
}
for (int bit = 1; bit < n; bit++) {
int j = bit % 2;
for (int mask = 0; mask < (1 << n); mask++) {
dp[ty][j][mask] = dp[ty][j ^ 1][mask];
if (mask & (1 << bit)) dp[ty][j][mask] += dp[ty][j ^ 1][mask ^ (1 << bit)];
}
}
}
int calc(int ty, int cnt) {
int ret = 0;
int alw = 0;
int r = 0;
for (int i = 0; i < n; i++) {
if (qs[i] == 2) alw |= (1 << i);
if (qs[i] == 1) ones[r++] = i;
}
for (int mask = 0; mask < (1 << cnt); mask++) {
int now = alw;
for (int i = 0; i < r; i++) {
if (mask & (1 << i)) now |= (1 << ones[i]);
}
if (built[mask] % 2) ret -= dp[ty][(n - 1) % 2][now];
else ret += dp[ty][(n - 1) % 2][now];
}
return abs(ret);
}
int main() {
int q;
cin >> n >> q;
scanf("\n");
for (int i = 0; i < (1 << n); i++) {
char x;
scanf("%c", &x);
cost[0][i] = (int)(x - '0');
cost[1][rev(i)] = (int)(x - '0');
}
for (int mask = 0; mask < (1 << n); mask++) built[mask] = __builtin_popcount(mask);
build(0);
build(1);
while (q--) {
scanf("\n");
int zer = 0, one = 0, que = 0;
for (int i = n - 1; i >= 0; i--) {
char x;
scanf("%c", &x);
if (x == '?') qs[i] = 2, que++;
if (x == '1') qs[i] = 1, one++;
if (x == '0') qs[i] = 0, zer++;
}
if (que < 8) {
int ans = 0;
for (int mask = 0; mask < (1 << que); mask++) {
int ret = 0;
int j = 0;
for (int i = 0; i < n; i++) {
if (qs[i] == 2) {
if (mask & (1 << j)) ret |= (1 << i);
j++;
}
if (qs[i] == 1) ret |= (1 << i);
}
ans += cost[0][ret];
}
printf("%d\n", ans);
continue;
}
if (!one) {
int ret = 0;
for (int i = 0; i < n; i++) {
if (qs[i] == 2) ret |= (1 << i);
}
printf("%d\n", dp[0][(n - 1) % 2][ret]);
continue;
}
if (!zer) {
int ret = 0;
for (int i = 0; i < n; i++) {
if (qs[i] == 2) ret |= (1 << i);
}
printf("%d\n", dp[1][(n - 1) % 2][ret]);
continue;
}
if (one < 8) {
printf("%d\n", calc(0, one));
continue;
}
for (int i = 0; i < n; i++) if (qs[i] < 2) qs[i] ^= 1;
printf("%d\n", calc(1, zer));
}
return 0;
}
Compilation message (stderr)
# | 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... |