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 <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 23;
int N, Q;
string cost;
int sum[2][MASK(20) + 10];
void solve(){
cin >> N >> Q >> cost;
for(int i = 0; i < MASK(N); i++){
int c = cost[i] - '0';
sum[0][i] = sum[1][i] = c;
}
for(int j = 0; j < N; j++){
for(int i = 0; i < MASK(N); i++){
if(BIT(i, j)){
sum[0][i] += sum[0][i - MASK(j)];
}
}
for(int i = MASK(N) - 1; i >= 0; i--){
if(!BIT(i, j)){
sum[1][i] += sum[1][i + MASK(j)];
}
}
}
while(Q--){
string s; cin >> s;
reverse(ALL(s));
int mask0 = 0, mask1 = 0, mask2 = 0;
for(int i = 0; i < N; i++){
if(s[i] == '0') mask0 += MASK(i);
if(s[i] == '1') mask1 += MASK(i);
if(s[i] == '?') mask2 += MASK(i);
}
if(__builtin_popcount(mask0) <= 6){
int ans = 0;
for(int mask = mask0; mask >= 0; mask = (mask - 1) & mask0){
if(__builtin_popcount(mask) & 1) ans -= sum[1][mask1 + mask];
else ans += sum[1][mask1 + mask];
if(mask == 0) break;
}
cout << ans << '\n';
}
else if(__builtin_popcount(mask1) <= 6){
int ans = 0;
for(int mask = mask1; mask >= 0; mask = (mask - 1) & mask1){
if(__builtin_popcount(mask) & 1) ans -= sum[0][MASK(N) - 1 - mask0 - mask];
else ans += sum[0][MASK(N) - 1 - mask0 - mask];
if(mask == 0) break;
}
cout << ans << '\n';
}
else{
int ans = 0;
for(int mask = mask2; mask >= 0; mask = (mask - 1) & mask2){
ans += cost[mask1 + mask] - '0';
if(mask == 0) break;
}
cout << ans << '\n';
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("btower.inp","r",stdin);
// freopen("btower.out","w",stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
# | 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... |