#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 == n)
{
an += s[cr];
return;
}
if (qs[p] != '1')
df1(p + 1);
if (qs[p] != '0')
cr ^= 1 << p, df1(p + 1), cr ^= 1 << p;
}
void df2(int p)
{
if (p == n)
{
an += dp[0][cr] * (ct & 1 ? -1 : 1);
return;
}
if (qs[p] != '1')
df2(p + 1);
if (qs[p] != '?')
{
cr ^= 1 << p, ct += qs[p] == '0';
df2(p + 1);
cr ^= 1 << p, ct -= qs[p] == '0';
}
}
void df3(int p)
{
if (p == n)
{
an += dp[1][cr] * (ct & 1 ? -1 : 1);
return;
}
if (qs[p] != '0')
cr ^= 1 << p, df3(p + 1), cr ^= 1 << p;
if (qs[p] != '?')
{
ct += qs[p] == '1';
df3(p + 1);
ct -= qs[p] == '1';
}
}
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;
default:
cc++;
}
}
if (cc <= ca && cc <= cb)
{
df1(0);
}
else if (ca <= cb && ca <= cc)
{
df2(0);
}
else
{
df3(0);
}
printf("%d\n", an);
}
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:131:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | scanf("%d%d%s", &n, &q, s);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3022 ms |
1216 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |