#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
ll SOS[(1 << 20)];
ll odwSOS[(1 << 20)];
ll cost[(1 << 20)];
int odw_mask;
int l,q;
ll ans0(string s)
{
int mask = 0;
vi zeros;
rep(i,l)
{
if(s[i] != '1')
{
mask += (1 << i);
}
if(s[i] == '0') zeros.pb(i);
}
ll ans = odwSOS[mask];
rep2(mask2,1,(1 << siz(zeros))-1)
{
int new_mask = mask;
rep(i,siz(zeros))
{
if(mask2 & (1 << i)) new_mask -= (1 << zeros[i]);
}
if(count_bits(mask2) & 1)
{
ans -= odwSOS[new_mask];
}
else
{
ans += odwSOS[new_mask];
}
}
return ans;
}
ll ans1(string s)
{
int mask = 0;
vi ones;
rep(i,l)
{
if(s[i] != '0')
{
mask += (1 << i);
}
if(s[i] == '1') ones.pb(i);
}
ll ans = SOS[mask];
rep2(mask2,1,(1 << siz(ones))-1)
{
int new_mask = mask;
rep(i,siz(ones))
{
if(mask2 & (1 << i)) new_mask -= (1 << ones[i]);
}
if(count_bits(mask2) & 1)
{
ans -= SOS[new_mask];
}
else
{
ans += SOS[new_mask];
}
}
return ans;
}
ll ansp(string s)
{
ll ans = 0;
vi p;
int mask = 0;
rep(i,l)
{
if(s[i] == '?') p.pb(i);
if(s[i] == '1') mask += (1 << i);
}
rep(mask2,(1 << siz(p)))
{
int new_mask = mask;
rep(i,l)
{
if(mask2 & (1 << l))
{
new_mask += (1 << p[i]);
}
}
ans += cost[new_mask];
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
string s;
cin >> l >> q >> s;
odw_mask = (1 << l)-1;
rep(i,(1 << l))
{
cost[i] = s[i] - '0';
SOS[i] = s[i] - '0';
odwSOS[i^odw_mask] = s[i] - '0';
}
rep(bit,l)
{
rep(mask,(1 << l))
{
if(mask & (1 << bit))
{
SOS[mask] += SOS[mask-(1 << bit)];
odwSOS[mask] += odwSOS[mask-(1 << bit)];
}
}
}
rep(qq,q)
{
string query;
cin >> query;
reverse(all(query));
int count0 = 0;
int count1 = 0;
int countp = 0;
rep(i,l)
{
if(query[i] == '0') count0++;
else if(query[i] == '1') count1++;
else countp++;
}
cout << ans0(query) << "\n";
continue;
if(count0 <= count1 && count0 <= countp)
{
cout << ans0(query) << "\n";
continue;
}
if(count1 <= count0 && count1 <= countp)
{
cout << ans1(query) << "\n";
continue;
}
if(countp <= count1 && countp <= count0)
{
cout << ansp(query) << "\n";
continue;
}
}
}
# | 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... |