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>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen(".inp", "r"))
assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout));
#else
using namespace chrono;
auto start = steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = steady_clock::now();
cout << "\nExecution time : " << duration_cast<milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
int n, q, f0[1 << 20], f1[1 << 20], pc[1 << 20];
string s;
void Hollwo_Pelw(){
cin >> n >> q >> s;
for (int i = 0; i < 1 << n; i++) {
f0[i] = f1[i] = s[i] - '0';
}
for (int i = 0; i < 1 << n; i++)
pc[i] = __builtin_popcount(i);
for (int i = 0; i < n; i++) {
for (int mask = 0; mask < 1 << n; mask++) {
if (mask >> i & 1) {
f0[mask ^ 1 << i] += f0[mask];
f1[mask] += f1[mask ^ 1 << i];
}
}
}
for (string base; q --; ) {
cin >> base;
reverse(base.begin(), base.end());
int c0 = 0, c1 = 0, cq = 0;
for (int i = 0; i < n; i++) {
if (base[i] == '0') c0 |= 1 << i;
if (base[i] == '1') c1 |= 1 << i;
if (base[i] == '?') cq |= 1 << i;
}
if (pc[c0] <= 7) {
int res = 0;
for (int sub = c0; ; sub = (sub - 1) & c0) {
res += (pc[c0] % 2 == pc[sub] % 2 ? 1 : -1) * f0[c0 ^ c1 ^ sub];
if (!sub) break;
}
cout << res << '\n';
continue;
}
if (pc[c1] <= 7) {
int res = 0;
for (int sub = c1; ; sub = (sub - 1) & c1) {
res += (pc[c1] % 2 == pc[sub] % 2 ? 1 : -1) * f1[cq ^ sub];
if (!sub) break;
}
cout << res << '\n';
continue;
}
if (pc[cq] <= 7) {
int res = 0;
for (int sub = cq; ; sub = (sub - 1) & cq) {
res += s[sub | c1] - '0';
if (!sub) break;
}
cout << res << '\n';
continue;
}
}
}
# | 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... |