#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
/*
Competitive Programming notes that I need to study & fix my dumbass self:
1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops, I don't know
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you
2. Stress:
- Don't be cocky and think stressing with your dumbass brute-force solution will give you guaranteed AC.
- Always try generating big testcases and try if they run
3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time
I hate offline because I am dumb
*/
typedef pair<int, int> ii;
const int N = 1e6+5;
const int M = 20;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;}
int n,q,m;
int a[N], f[N];
void solve()
{
cin>>n>>q;
m = (1<<n);
for (int i = 0; i < m; i++)
{
char c; cin>>c;
a[i] = c - '0';
// cout<<a[i]<<" ";
f[i] = a[i];
}
// cout<<endl;
for (int i = 0; i < n; i++)
{
for (int mask = 0; mask < m; mask++) if (mask>>i & 1) f[mask] += f[mask ^ (1<<i)];
}
while (q--)
{
string s; cin>>s;
int cnt = 0;
int msk = 0;
for (int i = 0; i < n; i++)
{
if (s[i] == '1') cnt++;
// if (s[i] == '1') msk |= (1<<i);
}
// cout<<cnt<<endl;
int ans = 0;
for (int mask = 0; mask < (1<<cnt); mask++)
{
int id = 0;
int nmsk1 = 0, nmsk2 = 0;
// cout<<mask<<endl;
for (int i = 0; i < n; i++)
{
int j = n-i-1;
if (s[i] == '1')
{
int bit = (mask>>id & 1);
// cout<<i<<" "<<id<<" "<<bit<<endl;
if (mask>>id & 1) nmsk1 |= (1<<j), nmsk2 |= (1<<j);
id++;
} else if (s[i] == '?') nmsk1 |= (1<<j);
}
// cout<<nmsk1<<" "<<nmsk2<<" "<<f[nmsk1] - f[nmsk2] + a[nmsk2]<<endl;
int diff = cnt - __builtin_popcount(mask);
if (diff % 2 == 0) ans += f[nmsk1] - f[nmsk2];
else ans -= f[nmsk1] - f[nmsk2];
if (!diff) ans += a[nmsk2];
}
cout<<ans<<endl;
}
}
/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
// cin>>t;
while (t--)
{
solve();
cout<<"\n";
}
}
# | 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... |