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>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int readInt () {
bool minus = false;
int result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-') minus = true; else result = ch-'0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result*10 + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
int n, q;
const int MAXN = 20;
string s;
int dp[(1 << MAXN) + 7];
int dp2[(1 << MAXN) + 7];
int z(int x){
if(__builtin_popcount(x) % 2 == 0){
return 1;
}
return -1;
}
int z2(int a, int x){
if(((int)__builtin_popcount(a) - (int)__builtin_popcount(x)) % 2 == 0){
return 1;
}
return -1;
}
void solve(){
cin >> n >> q;
cin >> s;
for(int i = 0; i < (1 << n); i++){
dp[i] = s[i] - '0';
dp2[i] = s[i] - '0';
}
for(int i = 0; i < n; i++){
for(int msk = 0; msk < (1 << n); msk++){
if((msk & (1 << i))){
dp[msk] += dp[(msk ^ (1 << i))];
}else{
dp2[msk] += dp2[(msk ^ (1 << i))];
}
}
}
while(q--){
string t;
cin >> t;
reverse(t.begin(), t.end());
int a = 0, b = 0, c = 0;
for(int i = 0; i < n; i++){
if(t[i] == '0'){
a += (1 << i);
}else if(t[i] == '1'){
b += (1 << i);
}else{
c += (1 << i);
}
}
int ans = 0;
if(__builtin_popcount(c) <= 6){
for(int x = c; x > 0; x = (x - 1) & c){
ans += (s[x | b] - '0');
}
ans += (s[b] - '0');
}else if(__builtin_popcount(a) <= 6){
for(int x = a; x > 0; x = (x - 1) & a){
ans += (dp2[x | b] * z(x));
}
ans += dp2[b];
}else{
for(int x = b; x > 0; x = (x - 1) & b){
ans += (dp[x | c] * z2(b, x));
}
ans += (dp[c] * z(b));
}
cout << ans << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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... |