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;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int nx=(1<<20)+5;
int l, q, a[nx], sub[nx], super[nx];
char c;
string s;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>l>>q;
for (int i=0; i<(1<<l); i++) cin>>c, a[i]=sub[i]=super[i]=c-'0';
for (int i=0; i<l; i++) for (int j=0; j<(1<<l); j++) if (j&(1<<i)) sub[j]+=sub[j^(1<<i)];
for (int i=0; i<l; i++) for (int j=(1<<l)-1; j>=0; j--) if (!(j&(1<<i))) super[j]+=super[j^(1<<i)];
while (q--)
{
cin>>s;
reverse(s.begin(), s.end());
int tmp=0;
vector<int> zero, one, qrs;
for (int i=0; i<l; i++)
{
if (s[i]=='0') zero.push_back(i);
if (s[i]=='1') one.push_back(i);
if (s[i]=='?') qrs.push_back(i);
}
if (zero.size()<=(l/3))
{
for (int i=0; i<l; i++) if (s[i]=='1') tmp+=(1<<i);
if (zero.empty()) cout<<super[tmp]<<'\n';
else
{
int sz=zero.size(), res=0;
for (int i=0; i<(1<<sz); i++)
{
int vl=tmp;
for (int j=0; j<sz; j++) if ((i&(1<<j))) vl+=(1<<zero[j]);
//cout<<"debug "<<i<<' '<<vl<<'\n';
if ((__builtin_popcount(i)%2)==0) res+=super[vl];
else res-=super[vl];
}
cout<<res<<'\n';
}
}
else if (one.size()<=(l/3))
{
for (int i=0; i<l; i++) if (s[i]=='?') tmp+=(1<<i);
if (one.empty()) cout<<sub[tmp]<<'\n';
else
{
int sz=one.size(), res=0;
for (int i=0; i<(1<<sz); i++)
{
int vl=tmp;
for (int j=0; j<sz; j++) if (i&(1<<j)) vl+=(1<<one[j]);
if ((sz-__builtin_popcount(i))%2==0) res+=sub[vl];
else res-=sub[vl];
}
cout<<res<<'\n';
}
}
else
{
for (int i=0; i<l; i++) if (s[i]=='1') tmp+=(1<<i);
if (qrs.empty()) cout<<a[tmp]<<'\n';
else
{
int sz=qrs.size(), res=0;
for (int i=0; i<(1<<sz); i++)
{
int vl=tmp;
for (int j=0; j<sz; j++) if (i&(1<<j)) vl+=(1<<qrs[j]);
res+=a[vl];
}
cout<<res<<'\n';
}
}
}
}
// 00011???
Compilation message (stderr)
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:33:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
33 | if (zero.size()<=(l/3))
| ~~~~~~~~~~~^~~~~~~
snake_escaping.cpp:51:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | else if (one.size()<=(l/3))
| ~~~~~~~~~~^~~~~~~
# | 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... |