Submission #714119

# Submission time Handle Problem Language Result Execution time Memory
714119 2023-03-24T01:38:59 Z gaga999 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1238 ms 18108 KB
#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

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
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 268 ms 5212 KB Output is correct
12 Correct 297 ms 4780 KB Output is correct
13 Correct 351 ms 4048 KB Output is correct
14 Correct 351 ms 4176 KB Output is correct
15 Correct 315 ms 5108 KB Output is correct
16 Correct 369 ms 4252 KB Output is correct
17 Correct 374 ms 4152 KB Output is correct
18 Correct 219 ms 6036 KB Output is correct
19 Correct 264 ms 3244 KB Output is correct
20 Correct 296 ms 4684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 268 ms 5212 KB Output is correct
12 Correct 297 ms 4780 KB Output is correct
13 Correct 351 ms 4048 KB Output is correct
14 Correct 351 ms 4176 KB Output is correct
15 Correct 315 ms 5108 KB Output is correct
16 Correct 369 ms 4252 KB Output is correct
17 Correct 374 ms 4152 KB Output is correct
18 Correct 219 ms 6036 KB Output is correct
19 Correct 264 ms 3244 KB Output is correct
20 Correct 296 ms 4684 KB Output is correct
21 Correct 329 ms 5072 KB Output is correct
22 Correct 357 ms 5324 KB Output is correct
23 Correct 411 ms 4452 KB Output is correct
24 Correct 474 ms 4148 KB Output is correct
25 Correct 365 ms 6176 KB Output is correct
26 Correct 471 ms 4800 KB Output is correct
27 Correct 461 ms 4624 KB Output is correct
28 Correct 236 ms 7084 KB Output is correct
29 Correct 337 ms 3148 KB Output is correct
30 Correct 365 ms 5276 KB Output is correct
31 Correct 416 ms 5156 KB Output is correct
32 Correct 488 ms 5248 KB Output is correct
33 Correct 397 ms 4048 KB Output is correct
34 Correct 454 ms 4072 KB Output is correct
35 Correct 445 ms 4752 KB Output is correct
36 Correct 235 ms 3148 KB Output is correct
37 Correct 315 ms 5256 KB Output is correct
38 Correct 348 ms 3224 KB Output is correct
39 Correct 438 ms 4244 KB Output is correct
40 Correct 476 ms 4044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 80 ms 10516 KB Output is correct
12 Correct 80 ms 10404 KB Output is correct
13 Correct 84 ms 10364 KB Output is correct
14 Correct 101 ms 10448 KB Output is correct
15 Correct 78 ms 10568 KB Output is correct
16 Correct 113 ms 10372 KB Output is correct
17 Correct 104 ms 10372 KB Output is correct
18 Correct 68 ms 10648 KB Output is correct
19 Correct 79 ms 10264 KB Output is correct
20 Correct 79 ms 10452 KB Output is correct
21 Correct 89 ms 10456 KB Output is correct
22 Correct 117 ms 10400 KB Output is correct
23 Correct 79 ms 10336 KB Output is correct
24 Correct 106 ms 10396 KB Output is correct
25 Correct 103 ms 10352 KB Output is correct
26 Correct 68 ms 10200 KB Output is correct
27 Correct 79 ms 10348 KB Output is correct
28 Correct 78 ms 10200 KB Output is correct
29 Correct 98 ms 10316 KB Output is correct
30 Correct 107 ms 10312 KB Output is correct
31 Correct 82 ms 10332 KB Output is correct
32 Correct 116 ms 10388 KB Output is correct
33 Correct 112 ms 10392 KB Output is correct
34 Correct 73 ms 10196 KB Output is correct
35 Correct 94 ms 10340 KB Output is correct
36 Correct 91 ms 10316 KB Output is correct
37 Correct 94 ms 10380 KB Output is correct
38 Correct 97 ms 10552 KB Output is correct
39 Correct 92 ms 10316 KB Output is correct
40 Correct 94 ms 10240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 268 ms 5212 KB Output is correct
12 Correct 297 ms 4780 KB Output is correct
13 Correct 351 ms 4048 KB Output is correct
14 Correct 351 ms 4176 KB Output is correct
15 Correct 315 ms 5108 KB Output is correct
16 Correct 369 ms 4252 KB Output is correct
17 Correct 374 ms 4152 KB Output is correct
18 Correct 219 ms 6036 KB Output is correct
19 Correct 264 ms 3244 KB Output is correct
20 Correct 296 ms 4684 KB Output is correct
21 Correct 329 ms 5072 KB Output is correct
22 Correct 357 ms 5324 KB Output is correct
23 Correct 411 ms 4452 KB Output is correct
24 Correct 474 ms 4148 KB Output is correct
25 Correct 365 ms 6176 KB Output is correct
26 Correct 471 ms 4800 KB Output is correct
27 Correct 461 ms 4624 KB Output is correct
28 Correct 236 ms 7084 KB Output is correct
29 Correct 337 ms 3148 KB Output is correct
30 Correct 365 ms 5276 KB Output is correct
31 Correct 416 ms 5156 KB Output is correct
32 Correct 488 ms 5248 KB Output is correct
33 Correct 397 ms 4048 KB Output is correct
34 Correct 454 ms 4072 KB Output is correct
35 Correct 445 ms 4752 KB Output is correct
36 Correct 235 ms 3148 KB Output is correct
37 Correct 315 ms 5256 KB Output is correct
38 Correct 348 ms 3224 KB Output is correct
39 Correct 438 ms 4244 KB Output is correct
40 Correct 476 ms 4044 KB Output is correct
41 Correct 80 ms 10516 KB Output is correct
42 Correct 80 ms 10404 KB Output is correct
43 Correct 84 ms 10364 KB Output is correct
44 Correct 101 ms 10448 KB Output is correct
45 Correct 78 ms 10568 KB Output is correct
46 Correct 113 ms 10372 KB Output is correct
47 Correct 104 ms 10372 KB Output is correct
48 Correct 68 ms 10648 KB Output is correct
49 Correct 79 ms 10264 KB Output is correct
50 Correct 79 ms 10452 KB Output is correct
51 Correct 89 ms 10456 KB Output is correct
52 Correct 117 ms 10400 KB Output is correct
53 Correct 79 ms 10336 KB Output is correct
54 Correct 106 ms 10396 KB Output is correct
55 Correct 103 ms 10352 KB Output is correct
56 Correct 68 ms 10200 KB Output is correct
57 Correct 79 ms 10348 KB Output is correct
58 Correct 78 ms 10200 KB Output is correct
59 Correct 98 ms 10316 KB Output is correct
60 Correct 107 ms 10312 KB Output is correct
61 Correct 82 ms 10332 KB Output is correct
62 Correct 116 ms 10388 KB Output is correct
63 Correct 112 ms 10392 KB Output is correct
64 Correct 73 ms 10196 KB Output is correct
65 Correct 94 ms 10340 KB Output is correct
66 Correct 91 ms 10316 KB Output is correct
67 Correct 94 ms 10380 KB Output is correct
68 Correct 97 ms 10552 KB Output is correct
69 Correct 92 ms 10316 KB Output is correct
70 Correct 94 ms 10240 KB Output is correct
71 Correct 554 ms 15440 KB Output is correct
72 Correct 593 ms 15496 KB Output is correct
73 Correct 719 ms 14028 KB Output is correct
74 Correct 1238 ms 14716 KB Output is correct
75 Correct 587 ms 16164 KB Output is correct
76 Correct 1198 ms 14492 KB Output is correct
77 Correct 1010 ms 14360 KB Output is correct
78 Correct 404 ms 18108 KB Output is correct
79 Correct 527 ms 12152 KB Output is correct
80 Correct 583 ms 15292 KB Output is correct
81 Correct 763 ms 15108 KB Output is correct
82 Correct 1118 ms 14160 KB Output is correct
83 Correct 616 ms 13432 KB Output is correct
84 Correct 1006 ms 14228 KB Output is correct
85 Correct 1040 ms 14352 KB Output is correct
86 Correct 349 ms 12220 KB Output is correct
87 Correct 545 ms 15124 KB Output is correct
88 Correct 559 ms 12108 KB Output is correct
89 Correct 740 ms 13740 KB Output is correct
90 Correct 964 ms 14292 KB Output is correct
91 Correct 568 ms 13380 KB Output is correct
92 Correct 1182 ms 14436 KB Output is correct
93 Correct 1066 ms 14472 KB Output is correct
94 Correct 381 ms 11948 KB Output is correct
95 Correct 872 ms 14112 KB Output is correct
96 Correct 869 ms 14156 KB Output is correct
97 Correct 879 ms 14076 KB Output is correct
98 Correct 859 ms 13868 KB Output is correct
99 Correct 896 ms 13888 KB Output is correct
100 Correct 895 ms 13828 KB Output is correct