# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1175464 | TrendBattles | Snake Escaping (JOI18_snake_escaping) | C++17 | 268 ms | 15116 KiB |
#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
#define INFILE "main.inp"
#define OUTFILE "main.out"
namespace SUBTASK_small_L {
void main(int L, int Q, string digits) {
vector <int> pow_3(L + 1);
pow_3[0] = 1;
for (int i = 1; i <= L; ++i) {
pow_3[i] = pow_3[i - 1] * 3;
}
const int M = pow_3[L];
vector <int> mask_3(1 << L);
vector <int> finale(M);
for (int m = 1; m < (1 << L); ++m) {
int p = m & -m;
mask_3[m] = mask_3[m ^ p] + pow_3[__builtin_ctz(p)];
}
for (int m = 0; m < (1 << L); ++m) {
int x = digits[m] - '0';
for (int p = 0; p < (1 << L); ++p) {
int f_m = mask_3[p] * 2 + mask_3[m & ~p];
finale[f_m] += x;
}
}
for (int _ = 0; _ < Q; ++_) {
string que; cin >> que;
int m = 0;
for (int i = 0; i < L; ++i) {
if (que[i] == '1') m += pow_3[L - i - 1];
else if (que[i] == '?') m += pow_3[L - i - 1] * 2;
}
cout << finale[m] << '\n';
}
}
}
namespace SUBTASK_small_Q {
const int HIGH_BIT = 20, MAX_MASK = 1 << HIGH_BIT;
int sum_A[MAX_MASK], sum_B[MAX_MASK];
int bit_cnt[MAX_MASK];
void main(int L, int Q, string digits) {
for (int m = 0; m < (1 << L); ++m) {
sum_A[m] = digits[m] - '0';
sum_B[~(-1 << L) ^ m] = sum_A[m];
bit_cnt[m] = bit_cnt[m >> 1] ^ (m & 1);
}
for (int b = 0; b < L; ++b) {
for (int m = 0; m < (1 << L); ++m) {
if ((m >> b & 1) == 0) {
sum_A[m] += sum_A[m ^ (1 << b)];
sum_B[m] += sum_B[m ^ (1 << b)];
}
}
}
for (int _ = 0; _ < Q; ++_) {
string que; cin >> que;
int ans = 0;
if (count(que.begin(), que.end(), '0') <= L / 2) {
int allowed = 0, banned = 0;
for (int i = 0; i < L; ++i) {
if (que[i] == '1') allowed ^= 1 << (L - i - 1);
if (que[i] == '0') banned ^= 1 << (L - i - 1);
}
for (int p = banned; p >= 0; p = (p - 1) & banned) {
if (bit_cnt[p]) {
ans -= sum_A[allowed ^ p];
} else {
ans += sum_A[allowed ^ p];
}
if (p == 0) break;
}
} else {
int allowed = 0, banned = 0;
for (int i = 0; i < L; ++i) {
if (que[i] == '0') allowed ^= 1 << (L - i - 1);
if (que[i] == '1') banned ^= 1 << (L - i - 1);
}
for (int p = banned; p >= 0; p = (p - 1) & banned) {
if (bit_cnt[p]) {
ans -= sum_B[allowed ^ p];
} else {
ans += sum_B[allowed ^ p];
}
if (p == 0) break;
}
}
cout << ans << '\n';
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
if (fopen(INFILE, "r")) {
freopen(INFILE, "r", stdin);
freopen(OUTFILE, "w", stdout);
}
int L, Q; cin >> L >> Q;
string digits; cin >> digits;
if (L <= 13) {
SUBTASK_small_L::main(L, Q, digits);
return 0;
}
if (Q <= 50'000) {
SUBTASK_small_Q::main(L, Q, digits);
return 0;
}
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... |