#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("Ofast") //#pragma GCC optimize "unroll-loops" //#pragma GCC target "sse,sse2,sse3,sse4,abm,avx,avx2,fma,mmx,popcnt,tune=native" //#pragma GCC optimize "prefetch-loop-arrays"
using namespace std; using namespace __gnu_pbds;
#define foru(i,a,b) for(int i=(a);i<(b);i++)
#define ford(i,a,b) for(int i=(a);i>=(b);i--)
#define fori(a,b) foru(i,a,b)
#define forj(a,b) foru(j,a,b)
#define fork(a,b) foru(k,a,b)
#define seto(x,i) memset(x,i,sizeof x)
#define pf first
#define ps second
#define pb push_back
#define eb emplace_back
#define em emplace
#define mp make_pair
#define mt make_tuple
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define ctz __builtin_ctz
#define ctzll __builtin_ctzll
#define P2(x) (1LL<<(x))
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#if __SIZEOF_INT128__
typedef __int128_t i128; typedef __uint128_t ui128;
#else
typedef int64_t i128; typedef uint64_t ui128;
#endif
typedef int64_t ll; typedef uint64_t ull; typedef int8_t byte; typedef long double lld;
typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<lld,lld> pdd;
template<class T1,class T2> using ordered_map=tree<T1,T2,less<T1>,rb_tree_tag,tree_order_statistics_node_update>; template<class T1> using ordered_set=ordered_map<T1,null_type>;
template<class T> using minpq=std::priority_queue<T,vector<T>,greater<T>>; template<class T> using maxpq=std::priority_queue<T,vector<T>,less<T>>;
template<class T> using minpairingheap=__gnu_pbds::priority_queue<T,greater<T>,pairing_heap_tag>; template<class T>using maxpairingheap=__gnu_pbds::priority_queue<T,less<T>,pairing_heap_tag>;
const int inf=0x3f3f3f3f,MOD=1e9+7; const ll INF=0x3f3f3f3f3f3f3f3f; const lld PI=acos((lld)-1);
const ll SEED=443214^chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count();
mt19937 randgen(SEED); int randint(int a, int b){return uniform_int_distribution<int>(a,b)(randgen);} ll randll(ll a, ll b){return uniform_int_distribution<ll>(a,b)(randgen);}
ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll fpow(ll a,ll b){ll ret=1;for(;b;b>>=1){if(b&1) ret=ret*a%MOD;a=a*a%MOD;}return ret;}
template<class T1,class T2>constexpr const auto _min(const T1&x,const T2&y){return x<y?x:y;} template<class T,class...Ts>constexpr auto _min(const T&x,const Ts&...xs){return _min(x,_min(xs...));}
template<class T1,class T2>constexpr const auto _max(const T1&x,const T2&y){return x>y?x:y;} template<class T,class...Ts>constexpr auto _max(const T&x,const Ts&...xs){return _max(x,_max(xs...));}
#define min(...) _min(__VA_ARGS__)
#define max(...) _max(__VA_ARGS__)
template<class T1,class T2>constexpr bool ckmin(T1&x,const T2&y){return x>y?x=y,1:0;} template<class T,class...Ts>constexpr bool ckmin(T&x,const Ts&...xs){return ckmin(x,min(xs...));}
template<class T1,class T2>constexpr bool ckmax(T1&x,const T2&y){return x<y?x=y,1:0;} template<class T,class...Ts>constexpr bool ckmax(T&x,const Ts&...xs){return ckmax(x,max(xs...));}
struct chash{
static ll splitmix64(ll x){x+=0x9e3779b97f4a7c15; x=(x^(x>>30))*0xbf58476d1ce4e5b9; x=(x^(x>>27))*0x94d049bb133111eb; return x^(x>>31);}
template<class T> size_t operator()(const T &x) const{return splitmix64(hash<T>()(x)+SEED);}
template<class T1,class T2> size_t operator()(const pair<T1,T2>&x)const{return 31*operator()(x.first)+operator()(x.second);}};
void fIn(string s){freopen(s.c_str(),"r",stdin);} void fOut(string s){freopen(s.c_str(),"w",stdout);} void fIO(string s){fIn(s+".in"); fOut(s+".out");}
string to_string(char c){return string(1,c);} string to_string(char* s){return (string)s;} string to_string(string s){return s;}
template<class T> string to_string(complex<T> c){stringstream ss; ss<<c; return ss.str();} template<class T1,class T2> string to_string(pair<T1,T2> p){return "("+to_string(p.pf)+","+to_string(p.ps)+")";}
template<size_t SZ> string to_string(bitset<SZ> b){string ret=""; fori(0,SZ) ret+=char('0'+b[i]); return ret;}
template<class T> string to_string(T v){string ret="{"; for(const auto& x:v) ret+=to_string(x)+","; return ret+"}";}
void DBG(){cerr<<"]"<<endl;} template<class T,class... Ts> void DBG(T x,Ts... xs){cerr<<to_string(x); if(sizeof...(xs)) cerr<<", "; DBG(xs...);}
#ifdef LOCAL_PROJECT
#define dbg(...) cerr<<"Line("<< __LINE__<<") -> ["<<#__VA_ARGS__<<"]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
#define nl "\n"
const int N=1100010,M=MOD,L=20;
int n,m,q,a[N],b[N],c[N],d,e,f,pc[N],ans=0;
string s;
int main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n>>q>>s; m=P2(n);
fori(0,m) a[i]=b[i]=c[i]=s[i]-'0';
fori(0,m) pc[i]=popcount(i);
fori(0,n) forj(0,m){
if(j&P2(i)) a[j]+=a[j^P2(i)];
else b[j]+=b[j^P2(i)];
}
while(q--){
cin>>s; d=e=f=0; reverse(all(s));
fori(0,n){
if(s[i]=='0') d|=P2(i);
else if(s[i]=='1') e|=P2(i);
else f|=P2(i);
}
if(pc[d]<=n/3){
ans=b[e];
for(int i=d;i;i=(i-1)&d) ans+=(1-2*(pc[i]&1))*b[e|i];
}
else if(pc[e]<=n/3){
ans=a[m-1^d];
for(int i=e;i;i=(i-1)&e) ans+=(1-2*(pc[i]&1))*a[m-1^(d|i)];
}
else{
ans=c[e];
for(int i=f;i;i=(i-1)&f) ans+=c[e|i];
}
cout<<ans<<nl;
}
return 0;
}
/**
*/
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:92:20: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
92 | ans=a[m-1^d];
| ~^~
snake_escaping.cpp:93:62: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
93 | for(int i=e;i;i=(i-1)&e) ans+=(1-2*(pc[i]&1))*a[m-1^(d|i)];
| ~^~
snake_escaping.cpp: In function 'void fIn(std::string)':
snake_escaping.cpp:54:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
54 | void fIn(string s){freopen(s.c_str(),"r",stdin);} void fOut(string s){freopen(s.c_str(),"w",stdout);} void fIO(string s){fIn(s+".in"); fOut(s+".out");}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp: In function 'void fOut(std::string)':
snake_escaping.cpp:54:78: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
54 | void fIn(string s){freopen(s.c_str(),"r",stdin);} void fOut(string s){freopen(s.c_str(),"w",stdout);} void fIO(string s){fIn(s+".in"); fOut(s+".out");}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
243 ms |
4460 KB |
Output is correct |
12 |
Correct |
257 ms |
4076 KB |
Output is correct |
13 |
Correct |
278 ms |
3308 KB |
Output is correct |
14 |
Correct |
272 ms |
3436 KB |
Output is correct |
15 |
Correct |
251 ms |
4452 KB |
Output is correct |
16 |
Correct |
282 ms |
3640 KB |
Output is correct |
17 |
Correct |
290 ms |
3564 KB |
Output is correct |
18 |
Correct |
211 ms |
5612 KB |
Output is correct |
19 |
Correct |
243 ms |
2668 KB |
Output is correct |
20 |
Correct |
266 ms |
4204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
243 ms |
4460 KB |
Output is correct |
12 |
Correct |
257 ms |
4076 KB |
Output is correct |
13 |
Correct |
278 ms |
3308 KB |
Output is correct |
14 |
Correct |
272 ms |
3436 KB |
Output is correct |
15 |
Correct |
251 ms |
4452 KB |
Output is correct |
16 |
Correct |
282 ms |
3640 KB |
Output is correct |
17 |
Correct |
290 ms |
3564 KB |
Output is correct |
18 |
Correct |
211 ms |
5612 KB |
Output is correct |
19 |
Correct |
243 ms |
2668 KB |
Output is correct |
20 |
Correct |
266 ms |
4204 KB |
Output is correct |
21 |
Correct |
275 ms |
4716 KB |
Output is correct |
22 |
Correct |
286 ms |
4844 KB |
Output is correct |
23 |
Correct |
333 ms |
4076 KB |
Output is correct |
24 |
Correct |
334 ms |
3692 KB |
Output is correct |
25 |
Correct |
283 ms |
5484 KB |
Output is correct |
26 |
Correct |
342 ms |
4076 KB |
Output is correct |
27 |
Correct |
344 ms |
4076 KB |
Output is correct |
28 |
Correct |
222 ms |
6636 KB |
Output is correct |
29 |
Correct |
276 ms |
2668 KB |
Output is correct |
30 |
Correct |
288 ms |
4936 KB |
Output is correct |
31 |
Correct |
312 ms |
4588 KB |
Output is correct |
32 |
Correct |
337 ms |
4588 KB |
Output is correct |
33 |
Correct |
296 ms |
3436 KB |
Output is correct |
34 |
Correct |
335 ms |
3520 KB |
Output is correct |
35 |
Correct |
340 ms |
4076 KB |
Output is correct |
36 |
Correct |
231 ms |
2668 KB |
Output is correct |
37 |
Correct |
269 ms |
4716 KB |
Output is correct |
38 |
Correct |
287 ms |
2584 KB |
Output is correct |
39 |
Correct |
311 ms |
3692 KB |
Output is correct |
40 |
Correct |
326 ms |
3564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
59 ms |
18192 KB |
Output is correct |
12 |
Correct |
59 ms |
18192 KB |
Output is correct |
13 |
Correct |
78 ms |
18064 KB |
Output is correct |
14 |
Correct |
82 ms |
18064 KB |
Output is correct |
15 |
Correct |
67 ms |
18216 KB |
Output is correct |
16 |
Correct |
81 ms |
18192 KB |
Output is correct |
17 |
Correct |
83 ms |
18192 KB |
Output is correct |
18 |
Correct |
51 ms |
18380 KB |
Output is correct |
19 |
Correct |
58 ms |
18064 KB |
Output is correct |
20 |
Correct |
58 ms |
18192 KB |
Output is correct |
21 |
Correct |
74 ms |
18192 KB |
Output is correct |
22 |
Correct |
84 ms |
18064 KB |
Output is correct |
23 |
Correct |
60 ms |
18320 KB |
Output is correct |
24 |
Correct |
88 ms |
18116 KB |
Output is correct |
25 |
Correct |
84 ms |
18192 KB |
Output is correct |
26 |
Correct |
52 ms |
18064 KB |
Output is correct |
27 |
Correct |
59 ms |
18116 KB |
Output is correct |
28 |
Correct |
61 ms |
18064 KB |
Output is correct |
29 |
Correct |
67 ms |
18064 KB |
Output is correct |
30 |
Correct |
79 ms |
18064 KB |
Output is correct |
31 |
Correct |
68 ms |
18064 KB |
Output is correct |
32 |
Correct |
93 ms |
18192 KB |
Output is correct |
33 |
Correct |
82 ms |
18192 KB |
Output is correct |
34 |
Correct |
50 ms |
18064 KB |
Output is correct |
35 |
Correct |
76 ms |
18064 KB |
Output is correct |
36 |
Correct |
77 ms |
18192 KB |
Output is correct |
37 |
Correct |
79 ms |
18192 KB |
Output is correct |
38 |
Correct |
77 ms |
18192 KB |
Output is correct |
39 |
Correct |
77 ms |
18316 KB |
Output is correct |
40 |
Correct |
77 ms |
18064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
243 ms |
4460 KB |
Output is correct |
12 |
Correct |
257 ms |
4076 KB |
Output is correct |
13 |
Correct |
278 ms |
3308 KB |
Output is correct |
14 |
Correct |
272 ms |
3436 KB |
Output is correct |
15 |
Correct |
251 ms |
4452 KB |
Output is correct |
16 |
Correct |
282 ms |
3640 KB |
Output is correct |
17 |
Correct |
290 ms |
3564 KB |
Output is correct |
18 |
Correct |
211 ms |
5612 KB |
Output is correct |
19 |
Correct |
243 ms |
2668 KB |
Output is correct |
20 |
Correct |
266 ms |
4204 KB |
Output is correct |
21 |
Correct |
275 ms |
4716 KB |
Output is correct |
22 |
Correct |
286 ms |
4844 KB |
Output is correct |
23 |
Correct |
333 ms |
4076 KB |
Output is correct |
24 |
Correct |
334 ms |
3692 KB |
Output is correct |
25 |
Correct |
283 ms |
5484 KB |
Output is correct |
26 |
Correct |
342 ms |
4076 KB |
Output is correct |
27 |
Correct |
344 ms |
4076 KB |
Output is correct |
28 |
Correct |
222 ms |
6636 KB |
Output is correct |
29 |
Correct |
276 ms |
2668 KB |
Output is correct |
30 |
Correct |
288 ms |
4936 KB |
Output is correct |
31 |
Correct |
312 ms |
4588 KB |
Output is correct |
32 |
Correct |
337 ms |
4588 KB |
Output is correct |
33 |
Correct |
296 ms |
3436 KB |
Output is correct |
34 |
Correct |
335 ms |
3520 KB |
Output is correct |
35 |
Correct |
340 ms |
4076 KB |
Output is correct |
36 |
Correct |
231 ms |
2668 KB |
Output is correct |
37 |
Correct |
269 ms |
4716 KB |
Output is correct |
38 |
Correct |
287 ms |
2584 KB |
Output is correct |
39 |
Correct |
311 ms |
3692 KB |
Output is correct |
40 |
Correct |
326 ms |
3564 KB |
Output is correct |
41 |
Correct |
59 ms |
18192 KB |
Output is correct |
42 |
Correct |
59 ms |
18192 KB |
Output is correct |
43 |
Correct |
78 ms |
18064 KB |
Output is correct |
44 |
Correct |
82 ms |
18064 KB |
Output is correct |
45 |
Correct |
67 ms |
18216 KB |
Output is correct |
46 |
Correct |
81 ms |
18192 KB |
Output is correct |
47 |
Correct |
83 ms |
18192 KB |
Output is correct |
48 |
Correct |
51 ms |
18380 KB |
Output is correct |
49 |
Correct |
58 ms |
18064 KB |
Output is correct |
50 |
Correct |
58 ms |
18192 KB |
Output is correct |
51 |
Correct |
74 ms |
18192 KB |
Output is correct |
52 |
Correct |
84 ms |
18064 KB |
Output is correct |
53 |
Correct |
60 ms |
18320 KB |
Output is correct |
54 |
Correct |
88 ms |
18116 KB |
Output is correct |
55 |
Correct |
84 ms |
18192 KB |
Output is correct |
56 |
Correct |
52 ms |
18064 KB |
Output is correct |
57 |
Correct |
59 ms |
18116 KB |
Output is correct |
58 |
Correct |
61 ms |
18064 KB |
Output is correct |
59 |
Correct |
67 ms |
18064 KB |
Output is correct |
60 |
Correct |
79 ms |
18064 KB |
Output is correct |
61 |
Correct |
68 ms |
18064 KB |
Output is correct |
62 |
Correct |
93 ms |
18192 KB |
Output is correct |
63 |
Correct |
82 ms |
18192 KB |
Output is correct |
64 |
Correct |
50 ms |
18064 KB |
Output is correct |
65 |
Correct |
76 ms |
18064 KB |
Output is correct |
66 |
Correct |
77 ms |
18192 KB |
Output is correct |
67 |
Correct |
79 ms |
18192 KB |
Output is correct |
68 |
Correct |
77 ms |
18192 KB |
Output is correct |
69 |
Correct |
77 ms |
18316 KB |
Output is correct |
70 |
Correct |
77 ms |
18064 KB |
Output is correct |
71 |
Correct |
418 ms |
23056 KB |
Output is correct |
72 |
Correct |
537 ms |
23336 KB |
Output is correct |
73 |
Correct |
690 ms |
21588 KB |
Output is correct |
74 |
Correct |
924 ms |
22160 KB |
Output is correct |
75 |
Correct |
443 ms |
23952 KB |
Output is correct |
76 |
Correct |
898 ms |
22544 KB |
Output is correct |
77 |
Correct |
944 ms |
22288 KB |
Output is correct |
78 |
Correct |
315 ms |
25924 KB |
Output is correct |
79 |
Correct |
458 ms |
20036 KB |
Output is correct |
80 |
Correct |
463 ms |
23056 KB |
Output is correct |
81 |
Correct |
607 ms |
22928 KB |
Output is correct |
82 |
Correct |
842 ms |
22032 KB |
Output is correct |
83 |
Correct |
459 ms |
21264 KB |
Output is correct |
84 |
Correct |
1055 ms |
22032 KB |
Output is correct |
85 |
Correct |
980 ms |
22140 KB |
Output is correct |
86 |
Correct |
297 ms |
19984 KB |
Output is correct |
87 |
Correct |
449 ms |
22932 KB |
Output is correct |
88 |
Correct |
525 ms |
19984 KB |
Output is correct |
89 |
Correct |
599 ms |
21648 KB |
Output is correct |
90 |
Correct |
735 ms |
22032 KB |
Output is correct |
91 |
Correct |
516 ms |
21136 KB |
Output is correct |
92 |
Correct |
1086 ms |
22408 KB |
Output is correct |
93 |
Correct |
927 ms |
22108 KB |
Output is correct |
94 |
Correct |
274 ms |
19984 KB |
Output is correct |
95 |
Correct |
820 ms |
22248 KB |
Output is correct |
96 |
Correct |
804 ms |
22272 KB |
Output is correct |
97 |
Correct |
817 ms |
22124 KB |
Output is correct |
98 |
Correct |
819 ms |
22072 KB |
Output is correct |
99 |
Correct |
773 ms |
22076 KB |
Output is correct |
100 |
Correct |
815 ms |
22112 KB |
Output is correct |