#include<bits/stdc++.h>
using namespace std;
const int maxn = (1<<20);
int n, q;
int sub[maxn], super[maxn], val[maxn];
int gt(char x){
if(x == '0' || x == '1')return x-'0';
else return 2;
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> q;
for(int i = 0; i < (1<<n); i++){
char x;
cin >> x;
sub[i] = super[i] = val[i] = x-'0';
}
for(int i = 0; i < n; i++){
for(int j = 0; j < (1<<n); j++){
if(i&(1<<j)){
super[i^(1<<j)]+=super[i];
sub[i]+=sub[i^(1<<j)];
}
}
}
while(q--){
string str;
cin >> str;
reverse(str.begin(), str.end());
int mask[3] = {0, 0, 0};
long long ans = 0;
for(int i = 0; i < n; i++){
mask[gt(str[i])]|=(1<<i);
}
if(__builtin_popcount(mask[2]) <= 6){
for(int i = mask[2];;i=(i-1)&mask[2]){
ans += val[i^mask[1]];
if(!i)break;
}
}
else if(__builtin_popcount(mask[1]) <= 6){
for(int i = mask[1];;i=(i-1)&mask[1]){
if(__builtin_popcount(i)&1)ans -= sub[mask[1]^i];
else ans += sub[mask[1]^i];
if(!i)break;
}
}
else {
for(int i = mask[0];;i=(i-1)&mask[0]){
if(__builtin_popcount(i)&1)ans -= super[mask[1]^i];
else ans += super[mask[1]^i];
if(!i)break;
}
}
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |