# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
944361 | guechotjrhh | Snake Escaping (JOI18_snake_escaping) | C++14 | 729 ms | 46344 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<iostream>
#include<string>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#define maxp 1048580
using namespace std;
int st[maxp],n,p,zer[maxp], on[maxp], cnt[maxp];
int ab(int x) { return x > 0 ? x : -x; }
void init(int N, int Q, char*S){
n=N; p= 1<<n;
for (int i = 0; i < p; i++) zer[i] = on[i] = st[i] = S[i] - '0';
for (int i = 1; i < p; i<<=1) {
for (int j = 0; j < p; j++) {
if (j & i) zer[j] += zer[j ^ i];
else on[j] += on[j ^ i];
}
}
cnt[0] = 0;
for (int i = 0; i < p/2; i++) {
cnt[i << 1] = cnt[i];
cnt[(i << 1) + 1] = cnt[i] + 1;
}
}
int query(char* A) {
int a = 0, b = 0, q = 0, res=0;
for (int i = 0; i < n; i++) {
if (A[i] == '0') a|=1<<(n-1-i);
else if (A[i] == '1') b |= 1 << (n - 1 - i);
else q |= 1 << (n - 1 - i);
}
int oc = cnt[b], zc = cnt[a], qc = cnt[q];
if (oc < zc && oc < qc) {
res = 0;
for (int i = b; i >= 0; i--) {
i &= b;
if (cnt[i | q] & 1) res += zer[i | q];
else res -= zer[i | q];
}
res= ab(res);
}
else if (qc > zc) {
res = 0;
for (int i = a; i >= 0; i--) {
i &= a;
if (cnt[i | b] & 1) res += on[i | b];
else res -= on[i | b];
}
res= ab(res);
}
else {
res = 0;
for (int i = q; i >= 0; i--) {
i &= q;
res += st[i | b];
}
}
return res;
}
/*
4 8
3141592653589793
0101
?01?
??1?
?0??
1?00
01?1
??10
????
3 5
12345678
000
0??
1?0
?11
???
*/
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, Q; scanf("%d%d", &N, &Q);
char S[(1<<N)+1]; scanf("%s", S);
init(N, Q, S);
for (int i = 0; i < Q; i++) {
char A[N+1]; scanf("%s", A);
printf("%d\n", query(A));
}
}
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... |