#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 ll 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];
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;
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));
int a=0, b=0, c=0;
ll 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]<=n/3)
{
for(int mask=a;;mask=(mask-1)&a)
{
res+=s[c|mask]-'0';
if(mask==0) break;
}
}
else if(cnt[b]<=n/3)
{
for(int mask=b;;mask=(mask-1)&b)
{
if((cnt[b]-cnt[mask])&1) res-=g[c|mask];
else res+=g[c|mask];
if(mask==0) break;
}
}
else
{
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
snake_escaping.cpp:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
84 | 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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |