This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In the name of Allah :)
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
string to_string(char c) { return string(1,c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return (string)s; }
string to_string(string s) { return s; }
template<class A> string to_string(complex<A> c) {
stringstream ss; ss << c; return ss.str(); }
string to_string(vector<bool> v) {
string res = "{"; for(int i = 0; i < (int)v.size(); i++) res += char('0'+v[i]);
res += "}"; return res; }
template<size_t SZ> string to_string(bitset<SZ> b) {
string res = ""; for(size_t i = 0; i < SZ; i++) res += char('0'+b[i]);
return res; }
template<class A, class B> string to_string(pair<A,B> p);
template<class T> string to_string(T v) { // containers with begin(), end()
bool fst = 1; string res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += to_string(x);
}
res += "}"; return res;
}
template<class A, class B> string to_string(pair<A,B> p) {
return "("+to_string(p.first)+", "+to_string(p.second)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << to_string(h); if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define wis(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
#else
#define wis(...) 0
#endif
typedef long long ll;
typedef long double ld;
#define all(x) (x).begin(), (x).end()
const int MAXN = 20, MAXQ = 1e6;
int l, q, dp[2][1 << MAXN];
string s;
int main() {
ios::sync_with_stdio(0);cin.tie(0);
cin >> l >> q >> s;
for (int i = 0; i < (1 << l); i++) {
dp[0][i] = dp[1][i] = s[i] - '0';
}
for (int k = 0; k < l; k++) {
for (int i = 0; i < (1 << l); i++) {
if (i >> k & 1) {
dp[0][i] += dp[0][i ^ (1 << k)];
}
else {
dp[1][i] += dp[1][i ^ (1 << k)];
}
}
}
while(q--) {
int n[3] = {}, mask[3] = {};
string t;
cin >> t;
for (int i = 0; i < l; i++) {
if (t[i] == '?') {
n[2]++;
mask[2] ^= 1 << (l - i - 1);
}
else {
n[t[i] - '0']++;
mask[t[i] - '0'] ^= 1 << (l - i - 1);
}
}
wis(n[0], n[1], n[2], mask[0], mask[1], mask[2]);
ll ans = 0;
if (n[2] <= min(n[0], n[1])) {
for (int sub = mask[2]; sub; sub = (sub - 1) & mask[2]) {
ans += s[sub | mask[1]] - '0';
}
ans += s[mask[1]] - '0';
}
else if (n[1] <= n[0]) {
for (int sub = mask[1]; sub; sub = (sub - 1) & mask[1]) {
ans += dp[0][sub ^ mask[1] ^ mask[2]] * (__builtin_popcount(sub) % 2 ? -1 : 1);
}
ans += dp[0][mask[1] ^ mask[2]];
}
else {
for (int sub = mask[0]; sub; sub = (sub - 1) & mask[0]) {
ans += dp[1][((1 << l) - 1) ^ sub ^ mask[0] ^ mask[2]] * (__builtin_popcount(sub) % 2 ? -1 : 1);
}
ans += dp[1][((1 << l) - 1) ^ mask[0] ^ mask[2]];
}
cout << ans << '\n';
}
}
Compilation message (stderr)
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:37:18: warning: statement has no effect [-Wunused-value]
37 | #define wis(...) 0
| ^
snake_escaping.cpp:77:9: note: in expansion of macro 'wis'
77 | wis(n[0], n[1], n[2], mask[0], mask[1], mask[2]);
| ^~~
# | 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... |