이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
ll dp[1 << 20], dp2[1 << 20], ans;
string b, s; int l, q;
void recurr1(int i, int num, int tot) {
if(i == l) {
if(tot & 1) ans -= dp[num];
else ans += dp[num];
return;
}
if(s[i] == '0') recurr1(i + 1, num << 1, tot);
else if(s[i] == '?') recurr1(i + 1, num << 1 | 1, tot);
else {
recurr1(i + 1, num << 1 | 1, tot);
recurr1(i + 1, num << 1, tot + 1);
}
}
void recurr0(int i, int num, int tot) {
if(i == l) {
if(tot & 1) ans -= dp2[num];
else ans += dp2[num];
return;
}
if(s[i] == '1') recurr0(i + 1, num << 1 | 1, tot);
else if(s[i] == '?') recurr0(i + 1, num << 1, tot);
else {
recurr0(i + 1, num << 1, tot);
recurr0(i + 1, num << 1 | 1, tot + 1);
}
}
void recurrw0t(int i, int num) {
if(i == l) {
ans += b[num] - '0';
return;
}
if(s[i] == '?') {
recurrw0t(i + 1, num << 1 | 1);
recurrw0t(i + 1, num << 1);
} else recurrw0t(i + 1, (num << 1) + (s[i] == '1'));
}
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> l >> q >> b;
for(int i = 0; i < (1 << l); i++) {
dp[i] = dp2[i] = b[i] - '0';
}
for(int i = 0; i < l; i++) {
for(int j = 0; j < (1 << l); j++) {
if(j >> i & 1)
dp[j] += dp[j ^ (1 << i)];
}
}
for(int i = 0; i < l; i++) {
for(int j = 0; j < (1 << l); j++) {
if(~j >> i & 1)
dp2[j] += dp2[j | (1 << i)];
}
}
while(q--) {
cin >> s;
int n0 = 0, n1 = 0, nw0t = 0;
for(char c : s) {
n0 += c == '0';
n1 += c == '1';
nw0t += c == '?';
}
if(n1 == 0) {
int num = 0;
for(char c : s) num = (num << 1) + (c == '?');
cout << dp[num] << endl;
} else if(nw0t == 0) {
int num = 0;
for(char c : s) num = (num << 1) + (c == '1');
cout << b[num] << endl;
} else {
ans = 0;
if(nw0t <= n1 && nw0t <= n0) recurrw0t(0, 0);
else if(n1 <= nw0t && n1 <= n0) recurr1(0, 0, 0);
else recurr0(0, 0, 0);
cout << ans << endl;
}
}
}
# | 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... |