# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
897553 | avighna | Snake Escaping (JOI18_snake_escaping) | C++17 | 2075 ms | 44616 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.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 20;
ll dp[1 << N], dp2[1 << N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll l, q;
cin >> l >> q;
string s;
cin >> s;
for (ll i = 0; i < s.length(); ++i) {
dp[i] = dp2[i] = s[i] - '0';
}
for (ll i = 0; i < N; ++i) {
for (ll mask = 0; mask < (1 << N); ++mask) {
if (mask & (1 << i)) {
dp[mask] += dp[mask ^ (1 << i)];
} else {
dp2[mask] += dp2[mask ^ (1 << i)];
}
}
}
while (q--) {
string t;
cin >> t;
vector<ll> cnt(256);
ll ans = 0;
for (auto &i : t) {
cnt[i]++;
}
if (cnt['?'] <= 6) {
for (ll mask = 0; mask < (1 << cnt['?']); ++mask) {
ll val = 0;
ll m = mask;
ll p = 1;
for (ll i = t.length() - 1; i >= 0; --i) {
char c = t[i];
if (c == '?') {
c = m % 2 + '0';
m /= 2;
}
val += (c - '0') * p;
p *= 2;
}
ans += s[val] - '0';
}
} else if (cnt['1'] <= 6) {
for (ll mask = 0; mask < (1 << cnt['1']); ++mask) {
ll val = 0;
ll m = mask;
ll p = 1;
for (ll i = t.length() - 1; i >= 0; --i) {
char c = t[i];
if (c == '1') {
c = m % 2 + '0';
m /= 2;
}
if (c == '?') {
c = '1';
}
val += (c - '0') * p;
p *= 2;
}
ans +=
dp[val] * (__builtin_popcount(mask) % 2 == cnt['1'] % 2 ? 1 : -1);
}
} else {
for (ll mask = 0; mask < (1 << cnt['0']); ++mask) {
ll val = 0;
ll m = mask;
ll p = 1;
for (ll i = t.length() - 1; i >= 0; --i) {
char c = t[i];
if (c == '0') {
c = m % 2 + '0';
m /= 2;
}
if (c == '?') {
c = '0';
}
val += (c - '0') * p;
p *= 2;
}
ans += dp2[val] * (__builtin_popcount(mask) % 2 == 0 ? 1 : -1);
}
}
cout << ans << "\n";
}
}
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... |