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>
using namespace std;
const int Nmax = (1<<20);
int n, q;
char S[Nmax+2], Val[Nmax+2];
int val[Nmax], up[Nmax], down[Nmax], bits[Nmax];
void precalc()
{
int i, j;
for(i=0; i<(1<<n); ++i)
bits[i] = __builtin_popcount(i), up[i] = down[i] = val[i] = Val[i] - '0';
for(i=0; i<n; ++i)
for(j=0; j<(1<<n); ++j)
if(j & (1<<i))
up[j ^ (1<<i)] += up[j];
else down[j ^ (1<<i)] += down[j];
}
int solveQ(int mask, int que)
{
int i, j, ans = 0;
for(i=que, j = 0; j < (1<<bits[que]); i = (i-1) & que, ++j)
ans += val[i ^ mask];
return ans;
}
int solve0(int mask0, int mask1)
{
int i, j, ans = 0;
for(i=mask0, j = 0; j < (1<<bits[mask0]); i = (i-1) & mask0, ++j)
if(bits[i] & 1) ans -= up[mask1 ^ i];
else ans += up[mask1 ^ i];
return ans;
}
int solve1(int mask1, int maskq)
{
int i, j, ans = 0;
for(i=mask1, j = 0; j < (1<<bits[mask1]); i = (i-1) & mask1, ++j)
if(bits[i] & 1) ans -= down[maskq ^ mask1 ^ i];
else ans += down[maskq ^ mask1 ^ i];
return ans;
}
int query()
{
scanf("%s\n", S);
reverse(S, S + n);
int i, mask0 = 0, mask1 = 0, maskq = 0;
for(i=0; i<n; ++i)
{
if(S[i] == '0') mask0 ^= (1<<i);
else if(S[i] == '1') mask1 ^= (1<<i);
else maskq ^= (1<<i);
}
if(bits[maskq] <= bits[mask0] && bits[maskq] <= bits[mask1]) return solveQ(mask1, maskq);
else if(bits[mask0] <= bits[mask1]) return solve0(mask0, mask1);
else return solve1(mask1, maskq);
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false);
scanf("%d %d\n", &n, &q);
scanf("%s\n", Val);
precalc();
while(q--)
cout << query() << '\n';
return 0;
}
Compilation message (stderr)
snake_escaping.cpp: In function 'int query()':
snake_escaping.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s\n", S);
~~~~~^~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d\n", &n, &q);
~~~~~^~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s\n", Val);
~~~~~^~~~~~~~~~~~~
# | 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... |