#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
// #pragma ("reroll")
#define all(v) v.begin(), v.end()
#define skip continue;
#define gold ios_base::sync_with_stdio(false);cin.tie(NULL);
#define xa "\n"
#define int long long
#define pb push_back
#define S second
#define F first
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 7;
const int MAX = 1e9;
const int inf = 1e18;
int gcd(int a, int b) { if (b == 0) return a; else if(a == 0) return b; else return gcd(b, a % b); }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
unordered_map < int, int > mp;
map < string, int > otvet;
set < string > st;
void f(string s, int len){
if(s.size() > len) return;
if(s.size() == len) st.insert(s);
f(s + '0', len);
f(s + '1', len);
f(s + '?', len);
}
void solve() {
int l, q;
cin >> l >> q;
int n = (1 << l);
char a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
mp[i] = a[i] - '0';
}
f("", l);
vector < string > vec;
for(string to : st){
vec.pb(to);
}
for(auto to : vec){
string orig = to;
reverse(all(to));
int ans = 0;
vector < int > voprosy;
int num = 0;
for(int i = 0; i < to.size(); i++){
if(to[i] == '?') voprosy.pb(i);
if(to[i] == '1'){
num |= (1 << i);
}
}
for(int msk = 0; msk < (1 << voprosy.size()); msk++){
for(int i = 0; i < voprosy.size(); i++){
int k = voprosy[i];
if(msk & (1 << i)){
num |= (1 << k);
}
else {
if(num & (1 << k)) num ^= (1 << k);
}
}
ans += mp[num];
}
otvet[orig] = ans;
}
while(q--){
string s;
cin >> s;
cout << otvet[s] << xa;
}
}
signed main() {
gold;
//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);
int t = 1;
int tt = 0;
//cin >> t;
while (t--) {
tt++;
//cout << "Case " << tt << ":" << xa;
solve();
}
}
| # | 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... |