Submission #610842

#TimeUsernameProblemLanguageResultExecution timeMemory
610842MohammadAghilSnake Escaping (JOI18_snake_escaping)C++17
100 / 100
1728 ms39196 KiB
#include <bits/stdc++.h> // #pragma GCC optimize ("Ofast,unroll-loops") // #pragma GCC target ("avx2") using namespace std; typedef long long ll; typedef pair<int, int> pp; #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl #define per(i,r,l) for(int i = (r); i >= (l); i--) #define rep(i,l,r) for(int i = (l); i < (r); i++) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define pb push_back #define ss second #define ff first void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);} void IOS(){ cin.tie(0) -> sync_with_stdio(0); // #ifndef ONLINE_JUDGE // freopen("in.in", "r", stdin); // freopen("out.out", "w", stdout); // #endif } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll mod = 1e9 + 7, maxn = 5e5 + 5, lg = 20, inf = ll(1e18) + 5; ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;} int sml[1<<lg], lrg[1<<lg]; int main(){ IOS(); int n, q; cin >> n >> q; string s; cin >> s; rep(i,0,sz(s)) sml[i] = lrg[i] = s[i] - '0'; rep(i,0,n){ rep(j,0,1<<n){ if(j>>i&1){ sml[j] += sml[j^(1<<i)]; }else{ lrg[j] += lrg[j^(1<<i)]; } } } while(q--){ string t; cin >> t; vector<int> a[3]; rep(i,0,n){ if(t[n-1-i] == '?') a[2].pb(i); else a[t[n-1-i]-'0'].pb(i); } int ans = 0; if(sz(a[0]) <= n/3){ int vl = 0; for(char c: t){ vl <<= 1; if(c == '1') vl |= 1; } rep(i,0,1<<sz(a[0])){ int bt = vl; int ptr = 0; bool f = false; for(int p: a[0]){ if(i>>ptr&1) bt ^= 1<<p, f ^= 1; ptr++; } ans += (f?-1:1)*lrg[bt]; } }else if(sz(a[1]) <= n/3){ int vl = 0; for(char c: t){ vl <<= 1; if(c != '0') vl |= 1; } rep(i,0,1<<sz(a[1])){ int bt = vl; int ptr = 0; bool f = false; for(int p: a[1]){ if(i>>ptr&1) bt ^= 1<<p, f ^= 1; ptr++; } ans += (f?-1:1)*sml[bt]; } }else{ int vl = 0; for(char c: t){ vl <<= 1; if(c == '1') vl |= 1; } rep(i,0,1<<sz(a[2])){ int bt = vl; int ptr = 0; for(int p: a[2]){ if(i>>ptr&1) bt ^= 1<<p; ptr++; } ans += s[bt]-'0'; } } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...