# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
142632 | DrumpfTheGodEmperor | Snake Escaping (JOI18_snake_escaping) | C++14 | 1374 ms | 42344 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>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int L = (1 << 20) + 5;
int l, q, cost[L], defMask = 0, sumSubset[L], sumSubsetOf[L];
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> l >> q;
fw (i, 0, (1 << l)) {
char c;
cin >> c;
cost[i] = c - '0';
}
//sumSubset[i] stores sum of costs of j that j & i = j.
fw (i, 0, (1 << l)) sumSubset[i] = cost[i];
fw (i, 0, l) fw (mask, 0, (1 << l)) {
if (mask & (1 << i)) sumSubset[mask] += sumSubset[mask ^ (1 << i)];
}
// fw (mask, 0, (1 << l)) cout << "sumSubset[" << mask << "] = " << sumSubset[mask] << "\n";
//sumSubsetOf[i] stores sum of costs of j that j & i = i
fw (i, 0, (1 << l)) sumSubsetOf[i] = cost[i];
fw (i, 0, l) bw (mask, (1 << l), 0) {
if (!(mask & (1 << i))) sumSubsetOf[mask] += sumSubsetOf[mask | (1 << i)];
}
while (q--) {
string t;
cin >> t;
reverse(t.begin(), t.end());
int cnt0 = 0, cnt1 = 0, cntQ = 0;
fw (i, 0, t.length()) {
if (t[i] == '0') cnt0++;
else if (t[i] == '1') cnt1++;
else cntQ++;
}
//At least one of these is <= 6. Iterate over them.
int ans = 0;
defMask = 0;
int alternateMask = 0;
if (cntQ <= 6) {
fw (i, 0, t.length()) {
if (t[i] == '?') alternateMask |= (1 << i);
else defMask |= ((t[i] - '0') << i);
}
//Iterate through all submasks of alternateMask and OR it into defMask
for (int tmp = alternateMask; ;tmp = (tmp - 1) & alternateMask) {
int mask = (defMask | tmp);
ans += cost[mask];
if (tmp == 0) break;
}
} else if (cnt0 <= 6) {
fw (i, 0, t.length()) {
if (t[i] == '0') alternateMask |= (1 << i);
else {
if (t[i] == '1') defMask |= (1 << i);
}
}
for (int tmp = alternateMask; ; tmp = (tmp - 1) & alternateMask) {
int mask = (defMask | tmp);
if (bp(tmp) % 2 == 0) ans += sumSubsetOf[mask];
else ans -= sumSubsetOf[mask];
if (tmp == 0) break;
}
} else if (cnt1 <= 6) {
// cout << "case3\n";
// cout << "cnt1 = " << cnt1 << "\n";
fw (i, 0, t.length()) {
if (t[i] == '1') alternateMask |= (1 << i);
else {
if (t[i] == '?') defMask |= (1 << i);
}
}
for (int tmp = alternateMask; ; tmp = (tmp - 1) & alternateMask) {
int mask = (defMask | tmp);
if ((cnt1 - bp(tmp)) % 2 == 0) ans += sumSubset[mask];
else ans -= sumSubset[mask];
if (tmp == 0) break;
}
}
cout << ans << "\n";
}
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... |