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 <cstdio>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stdlib.h>
#include <cstring>
#include <string.h>
#include <string>
#include <sstream>
#include <assert.h>
#include <climits>
#include <sstream>
#include <numeric>
#include <time.h>
#include <limits.h>
#include <list>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <iomanip>
#include <complex>
#include <chrono>
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,lzcnt,popcnt")
#define lowbit(x) ((x) & -(x))
#define ml(a, b) ((1ll * (a) * (b)) % M)
#define tml(a, b) (a) = ((1ll * (a) * (b)) % M)
#define ad(a, b) ((0ll + (a) + (b)) % M)
#define tad(a, b) (a) = ((0ll + (a) + (b)) % M)
#define mi(a, b) ((0ll + M + (a) - (b)) % M)
#define tmi(a, b) (a) = ((0ll + M + (a) - (b)) % M)
#define tmin(a, b) (a) = min((a), (b))
#define tmax(a, b) (a) = max((a), (b))
#define iter(a) (a).begin(), (a).end()
#define riter(a) (a).rbegin(), (a).rend()
#define init(a, b) memset((a), (b), sizeof(a))
#define cpy(a, b) memcpy((a), (b), sizeof(a))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define pb push_back
#define mpr make_pair
#define ls(i) ((i) << 1)
#define rs(i) ((i) << 1 | 1)
#define INF 0x3f3f3f3f
#define eps 1e-9
#define F first
#define S second
#define N 20
#define AC cin.tie(0)->sync_with_stdio(0)
using namespace std;
typedef long long llt;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<llt, llt> pll;
typedef complex<double> cd;
// const int M = 998244353;
// random_device rm;
// mt19937 rg(rm());
// default_random_engine rg(rm());
// uniform_int_distribution<int> rd(INT_MIN, INT_MAX);
// uniform_real_distribution<double> rd(0, M_PI);
void db() { cout << "\n"; }
template <class T, class... U>
void db(T a, U... b) { cout << a << " ", db(b...); }
char s[1 << N], qs[N];
int dp[2][1 << N], n, ca, cb, cc, an, cr, ct;
vector<int> ar;
void df1(int p)
{
if (p == ar.size())
{
an += s[cr];
return;
}
df1(p + 1), cr ^= 1 << ar[p];
df1(p + 1), cr ^= 1 << ar[p];
}
void df2(int p)
{
if (p == ar.size())
{
an += dp[0][cr] * (ct & 1 ? -1 : 1);
return;
}
df2(p + 1), ct++, cr ^= 1 << ar[p];
df2(p + 1), ct--, cr ^= 1 << ar[p];
}
void df3(int p)
{
if (p == ar.size())
{
an += dp[1][cr] * (ct & 1 ? -1 : 1);
return;
}
ct++, df3(p + 1), ct--;
cr ^= 1 << ar[p], df3(p + 1), cr ^= 1 << ar[p];
}
signed main()
{
int q;
scanf("%d%d%s", &n, &q, s);
for (int i = 0; i < 1 << n; i++)
dp[0][i] = dp[1][i] = s[i] -= '0';
for (int j = 0; j < n; j++)
{
for (int i = 0; i < 1 << n; i++)
if ((i >> j) & 1)
dp[1][i] += dp[1][i ^ (1 << j)];
for (int i = (1 << n) - 1; i >= 0; i--)
if (~(i >> j) & 1)
dp[0][i] += dp[0][i ^ (1 << j)];
}
while (q-- && scanf("%s", qs))
{
reverse(qs, qs + n);
ca = cb = cc = cr = an = 0;
for (int i = 0; i < n; i++)
{
switch (qs[i])
{
case '0':
ca++;
break;
case '1':
cb++;
break;
case '?':
cc++;
break;
}
}
if (cc <= ca && cc <= cb)
{
for (int i = 0; i < n; i++)
{
switch (qs[i])
{
case '1':
cr ^= 1 << i;
break;
case '?':
ar.pb(i);
break;
}
}
df1(0);
}
else if (ca <= cb && ca <= cc)
{
for (int i = 0; i < n; i++)
{
switch (qs[i])
{
case '0':
ar.pb(i);
break;
case '1':
cr ^= 1 << i;
break;
}
}
df2(0);
}
else
{
for (int i = 0; i < n; i++)
{
switch (qs[i])
{
case '1':
ar.pb(i);
break;
case '?':
cr ^= 1 << i;
break;
}
}
df3(0);
}
ar.clear();
printf("%d\n", an);
}
}
Compilation message (stderr)
snake_escaping.cpp: In function 'void df1(int)':
snake_escaping.cpp:83:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | if (p == ar.size())
| ~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'void df2(int)':
snake_escaping.cpp:94:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | if (p == ar.size())
| ~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'void df3(int)':
snake_escaping.cpp:105:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | if (p == ar.size())
| ~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | scanf("%d%d%s", &n, &q, s);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |