# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
944361 | guechotjrhh | Snake Escaping (JOI18_snake_escaping) | C++14 | 729 ms | 46344 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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));
}
}
컴파일 시 표준 에러 (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... |