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 <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
int L, Q;
char IN[(1<<20)+1];
int A[1<<20], B[1<<20], C[1<<20];
char qs[21];
int sub[20];
signed main() {
scanf("%d %d", &L, &Q);
scanf("%s", IN);
rep(i, 1<<L) A[i] = (int)(IN[i]-'0');
rep(i, 1<<L) B[i] = A[i], C[((1<<L)-1)^i] = A[i];
rep(add, L) rep(S, 1<<L) if (((S>>add)&1)==0) B[S|(1<<add)] += B[S];
rep(add, L) rep(S, 1<<L) if (((S>>add)&1)==0) C[S|(1<<add)] += C[S];
rep(q, Q) {
scanf("%s", qs);
rep(i, L/2) swap(qs[i], qs[L-1-i]);
int zero = 0, one = 0, any = 0;
rep(i, L) {
if (qs[i] == '0') zero++;
else if (qs[i] == '1') one++;
else any++;
}
int s = 0;
int minsz = min(zero, min(one, any));
if (any == minsz) {
int sub = 0, mask = 0;
rep(i, L) {
if (qs[i] == '?') sub |= 1<<i;
else if (qs[i] == '1') mask |= 1<<i;
}
s += A[mask];
for (int id=sub; id>0; id=(id-1)&sub) s += A[id|mask];
}
else if (one == minsz) {
int sz = 0, sub = 0, mask = 0;
rep(i, L) {
if (qs[i] == '1') sub |= 1<<i, sz ^= 1;
else if (qs[i] == '?') mask |= 1<<i;
}
if (sz) s -= B[mask];
else s += B[mask];
for (int id=sub; id>0; id=(id-1)&sub) {
if ((sz^__builtin_popcount(id))&1) s -= B[id|mask];
else s += B[id|mask];
}
}
else {
int sz = 0, sub = 0, mask = 0;
rep(i, L) {
if (qs[i] == '0') sub |= 1<<i, sz ^= 1;
else if (qs[i] == '?') mask |= 1<<i;
}
if (sz) s -= C[mask];
else s += C[mask];
for (int id=sub; id>0; id=(id-1)&sub) {
if ((sz^__builtin_popcount(id))&1) s -= C[id|mask];
else s += C[id|mask];
}
}
printf("%d\n", s);
}
return 0;
}
Compilation message (stderr)
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:33:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &L, &Q);
^
snake_escaping.cpp:34:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", IN);
^
snake_escaping.cpp:41:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", qs);
^
# | 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... |