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>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
//typedef tree<int,null_type,less_equal<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define Sakura_chan ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mem(a,x) memset(a,x,sizeof(a))
#define fast(s) s.reserve(2000); s.max_load_factor(0.5);
#define int long long
#define F first
#define S second
#define pb push_back
#define ins insert
#define pii pair <long long, long long>
#define vpii vector <pii>
#define sz size()
#define all(p) p.begin(), p.end()
#define For(i,a,b) for(int i=a; i<=b; ++i)
#define rFor(i,a,b) for(int i=a; i>=b; --i)
#define Auto(it,a) for(auto it: a)
const int mod=1e9+7;
const int N=1e6+5, M=20;
int n, q, f[1<<M], g[1<<M], cnt[1<<M], a, b, c, d, res;
string s, t;
void file()
{
#define task "snakes"
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
Sakura_chan;
}
void Solve()
{
cin>>n>>q>>s;
d=n/3;
for(int i=0; i<(1<<n); ++i) f[i]=g[i]=(s[i]-'0');
for(int i=0; i<n; ++i) for(int mask=0; mask<(1<<n); ++mask) if(mask&(1<<i)) {f[mask]+=f[mask^(1<<i)]; g[mask^(1<<i)]+=g[mask];}
for(int i=0; i<(1<<n); ++i) cnt[i]=__builtin_popcount(i);
while(q--)
{
cin>>t;
reverse(all(t));
a=0; b=0; c=0; res=0;
for(int i=0; i<n; ++i)
{
if(t[i]=='?') a|=(1<<i);
else if(t[i]=='0') b|=(1<<i);
else c|=(1<<i);
}
if(cnt[a]<=d)
{
for(int mask=a;;mask=(mask-1)&a)
{
res+=s[c|mask]-'0';
if(mask==0) break;
}
cout<<res<<"\n";
continue;
}
if(cnt[b]<=d)
{
for(int mask=b;;mask=(mask-1)&b)
{
if((cnt[mask]-cnt[b])&1) res-=g[c|mask];
else res+=g[c|mask];
if(mask==0) break;
}
cout<<res<<"\n";
continue;
}
for(int mask=c;;mask=(mask-1)&c)
{
if((cnt[c]-cnt[mask])&1) res-=f[a|mask];
else res+=f[a|mask];
if(mask==0) break;
}
cout<<res<<"\n";
}
}
main()
{
file();
Solve();
return 0;
}
Compilation message (stderr)
snake_escaping.cpp:85:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
85 | main()
| ^~~~
snake_escaping.cpp: In function 'void file()':
snake_escaping.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |