#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
typedef pair<int,pii> P;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int pw[22] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152};
int main()
{
owo
int l,q;
cin>>l>>q;
vector<int>sub(pw[(l+1)]),sup(pw[(l+1)]),cnt(pw[l+1]);
for(int i=0;i<(1<<l);i++)cnt[i] = __builtin_popcount(i);
string snek;
for(int i=0;i<pw[l];i++){
char c;cin>>c;
sub[i]+=c-'0';
sup[i]+=c-'0';
snek+=c;
}
for(int i=0;i<l;i++){
for(int j=0;j<pw[l];j++){
if(j&pw[i])sub[j]+=sub[j^pw[i]];
else sup[j]+=sup[j^pw[i]];
}
}
while(q--){
vector<int>a,b,c;
string s;cin>>s;
int ans = 0;
for(int i=0;i<l;i++){
if(s[i] == '?')c.pb(l-i-1);
else if(s[i] == '0')a.pb(l-i-1);
else b.pb(l-i-1);
}
if(sz(c) <= 6){
int cur = 0;
for(int i=0;i<l;i++)if(s[i]=='1')cur+=pw[(l-i-1)];
for(int i=0;i<pw[sz(c)];i++){
ans+=snek[cur|i]-'0';
}
}
else if(sz(a) <= 6){
int cur = 0;
for(int i=0;i<l;i++)if(s[i] == '1')cur+=pw[(l-i-1)];
for(int i=0;i<pw[sz(a)];i++){
if(cnt[i]&1)ans-=sup[cur|i];
else ans+=sup[cur|i];
}
}else{
int cur = 0;
for(int i=0;i<l;i++)if(s[i] == '?')cur+=pw[(l-i-1)];
for(int mask=0;mask<pw[sz(b)];mask++){
if(cnt[mask]&1)ans-=sub[cur|mask];
else ans+=sub[cur|mask];
}
}
cout<<ans<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |