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<iostream>
#include<string>
using namespace std;
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#define maxp 1048580
using namespace std;
#define all(x) x.begin(),x.end()
string str;
int n,p;
int zer[maxp], on[maxp];
//vector<int> zer, on;//sum of those i contains, sum of those contains me i
void init(int N, int Q, string S) {
n = N; p = 1 << n;
str = S;
//zer.resize(p); on.resize(p);
for (int i = 0; i < p; i++) zer[i] = on[i] = S[i] - '0';
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
if (j & (1 << i)) zer[j] += zer[j - (1 << i)];
else on[j] += on[j + (1 << i)];
}
}
}
int ab(int x) { return x > 0 ? x : -x; }
int res;
void rec1(vector<int>& o, int i, int c, int x) {
if (i == o.size()) {
res += x*zer[c];
return;
}
rec1(o, i + 1, c, x);
rec1(o, i + 1, c + o[i], -x);
}
void rec0(vector<int>& z, int i, int c, int x) {
if (i == z.size()) {
res += x*on[c];
return;
}
rec0(z, i + 1, c, x);
rec0(z, i + 1, c + z[i], -x);
}
void recq(vector<int>& q, int i, int c) {
if (i == q.size()) { res += str[c] - '0'; return; }
recq(q, i + 1, c);
recq(q, i + 1, c + q[i]);
}
int query(string A) {
vector<int> ze, o, q;
for (int i = 0; i < n; i++) {
if (A[i] == '0') ze.push_back(1 << (n-1-i));
else if (A[i] == '1') o.push_back(1 << (n-1-i));
else q.push_back(1 << (n-1-i));
}
/*
3 5
12345678
000
0??
1?0
?11
???
*/
if (ze.size() > o.size() && q.size() > o.size()) {
int c = 0;
for (int i : q) c += i;
res = 0;
rec1(o, 0, c, 1);
/*for (int i = 0; i < (1 << o.size()); i++) {
int w = c, x=0;
for (int j = 0; j < o.size(); j++) {
if (i & (1 << j)) {
w += o[j];
x++;
}
}
if (x & 1) res += zer[w];
else res -= zer[w];
}*/
return ab(res);
}
else if(q.size() > ze.size()) {
int c = 0;
for (int i : o) c += i;
res = 0;
rec0(ze, 0, c, 1);
/*for (int i = 0; i < (1 << ze.size()); i++) {
int w = c, x = 0;
for (int j = 0; j < ze.size(); j++) {
if (i & (1 << j)) {
w += ze[j];
x++;
}
}
if (x & 1) res += on[w];
else res -= on[w];
}*/
return ab(res);
}
else {
res = 0;
int c = 0;
for (int i : o) c += i;
recq(q, 0, c);
return res;
}
return 0;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, Q; cin >> N >> Q;
string S; cin >> S;
init(N, Q, move(S));
for (int i = 0; i < Q; i++) {
string A;
cin >> A;
cout << query(move(A)) << endl;
}
}
Compilation message (stderr)
snake_escaping.cpp: In function 'void rec1(std::vector<int>&, int, int, int)':
snake_escaping.cpp:31:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if (i == o.size()) {
| ~~^~~~~~~~~~~
snake_escaping.cpp: In function 'void rec0(std::vector<int>&, int, int, int)':
snake_escaping.cpp:40:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | if (i == z.size()) {
| ~~^~~~~~~~~~~
snake_escaping.cpp: In function 'void recq(std::vector<int>&, int, int)':
snake_escaping.cpp:49:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | if (i == q.size()) { res += str[c] - '0'; return; }
| ~~^~~~~~~~~~~
# | 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... |