Submission #714116

# Submission time Handle Problem Language Result Execution time Memory
714116 2023-03-24T01:26:21 Z gaga999 Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 15384 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 == 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

snake_escaping.cpp: In function 'int main()':
snake_escaping.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);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 316 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 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 316 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 308 KB Output is correct
11 Correct 263 ms 4688 KB Output is correct
12 Correct 348 ms 4356 KB Output is correct
13 Correct 409 ms 3564 KB Output is correct
14 Correct 515 ms 3648 KB Output is correct
15 Correct 338 ms 4644 KB Output is correct
16 Correct 497 ms 3876 KB Output is correct
17 Correct 468 ms 3776 KB Output is correct
18 Correct 219 ms 5632 KB Output is correct
19 Correct 247 ms 2644 KB Output is correct
20 Correct 310 ms 4352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 316 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 308 KB Output is correct
11 Correct 263 ms 4688 KB Output is correct
12 Correct 348 ms 4356 KB Output is correct
13 Correct 409 ms 3564 KB Output is correct
14 Correct 515 ms 3648 KB Output is correct
15 Correct 338 ms 4644 KB Output is correct
16 Correct 497 ms 3876 KB Output is correct
17 Correct 468 ms 3776 KB Output is correct
18 Correct 219 ms 5632 KB Output is correct
19 Correct 247 ms 2644 KB Output is correct
20 Correct 310 ms 4352 KB Output is correct
21 Correct 318 ms 4796 KB Output is correct
22 Correct 360 ms 4912 KB Output is correct
23 Correct 578 ms 3840 KB Output is correct
24 Correct 802 ms 3720 KB Output is correct
25 Correct 424 ms 5688 KB Output is correct
26 Correct 740 ms 4192 KB Output is correct
27 Correct 652 ms 4192 KB Output is correct
28 Correct 260 ms 6836 KB Output is correct
29 Correct 305 ms 2756 KB Output is correct
30 Correct 357 ms 4868 KB Output is correct
31 Correct 611 ms 4716 KB Output is correct
32 Correct 830 ms 4776 KB Output is correct
33 Correct 471 ms 3584 KB Output is correct
34 Correct 717 ms 3840 KB Output is correct
35 Correct 638 ms 4132 KB Output is correct
36 Correct 264 ms 2716 KB Output is correct
37 Correct 357 ms 4648 KB Output is correct
38 Correct 353 ms 2844 KB Output is correct
39 Correct 596 ms 3992 KB Output is correct
40 Correct 799 ms 3916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 316 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 308 KB Output is correct
11 Correct 82 ms 10196 KB Output is correct
12 Correct 83 ms 10192 KB Output is correct
13 Correct 126 ms 10144 KB Output is correct
14 Correct 230 ms 10120 KB Output is correct
15 Correct 98 ms 10144 KB Output is correct
16 Correct 172 ms 10260 KB Output is correct
17 Correct 159 ms 10060 KB Output is correct
18 Correct 68 ms 10340 KB Output is correct
19 Correct 84 ms 9980 KB Output is correct
20 Correct 82 ms 10140 KB Output is correct
21 Correct 128 ms 10172 KB Output is correct
22 Correct 202 ms 10196 KB Output is correct
23 Correct 128 ms 10088 KB Output is correct
24 Correct 186 ms 10152 KB Output is correct
25 Correct 158 ms 10060 KB Output is correct
26 Correct 71 ms 9944 KB Output is correct
27 Correct 102 ms 10200 KB Output is correct
28 Correct 77 ms 10024 KB Output is correct
29 Correct 121 ms 10064 KB Output is correct
30 Correct 158 ms 10316 KB Output is correct
31 Correct 91 ms 10028 KB Output is correct
32 Correct 162 ms 10072 KB Output is correct
33 Correct 154 ms 10200 KB Output is correct
34 Correct 91 ms 10140 KB Output is correct
35 Correct 117 ms 10080 KB Output is correct
36 Correct 126 ms 10136 KB Output is correct
37 Correct 120 ms 10056 KB Output is correct
38 Correct 134 ms 10180 KB Output is correct
39 Correct 138 ms 10028 KB Output is correct
40 Correct 118 ms 10032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 316 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 308 KB Output is correct
11 Correct 263 ms 4688 KB Output is correct
12 Correct 348 ms 4356 KB Output is correct
13 Correct 409 ms 3564 KB Output is correct
14 Correct 515 ms 3648 KB Output is correct
15 Correct 338 ms 4644 KB Output is correct
16 Correct 497 ms 3876 KB Output is correct
17 Correct 468 ms 3776 KB Output is correct
18 Correct 219 ms 5632 KB Output is correct
19 Correct 247 ms 2644 KB Output is correct
20 Correct 310 ms 4352 KB Output is correct
21 Correct 318 ms 4796 KB Output is correct
22 Correct 360 ms 4912 KB Output is correct
23 Correct 578 ms 3840 KB Output is correct
24 Correct 802 ms 3720 KB Output is correct
25 Correct 424 ms 5688 KB Output is correct
26 Correct 740 ms 4192 KB Output is correct
27 Correct 652 ms 4192 KB Output is correct
28 Correct 260 ms 6836 KB Output is correct
29 Correct 305 ms 2756 KB Output is correct
30 Correct 357 ms 4868 KB Output is correct
31 Correct 611 ms 4716 KB Output is correct
32 Correct 830 ms 4776 KB Output is correct
33 Correct 471 ms 3584 KB Output is correct
34 Correct 717 ms 3840 KB Output is correct
35 Correct 638 ms 4132 KB Output is correct
36 Correct 264 ms 2716 KB Output is correct
37 Correct 357 ms 4648 KB Output is correct
38 Correct 353 ms 2844 KB Output is correct
39 Correct 596 ms 3992 KB Output is correct
40 Correct 799 ms 3916 KB Output is correct
41 Correct 82 ms 10196 KB Output is correct
42 Correct 83 ms 10192 KB Output is correct
43 Correct 126 ms 10144 KB Output is correct
44 Correct 230 ms 10120 KB Output is correct
45 Correct 98 ms 10144 KB Output is correct
46 Correct 172 ms 10260 KB Output is correct
47 Correct 159 ms 10060 KB Output is correct
48 Correct 68 ms 10340 KB Output is correct
49 Correct 84 ms 9980 KB Output is correct
50 Correct 82 ms 10140 KB Output is correct
51 Correct 128 ms 10172 KB Output is correct
52 Correct 202 ms 10196 KB Output is correct
53 Correct 128 ms 10088 KB Output is correct
54 Correct 186 ms 10152 KB Output is correct
55 Correct 158 ms 10060 KB Output is correct
56 Correct 71 ms 9944 KB Output is correct
57 Correct 102 ms 10200 KB Output is correct
58 Correct 77 ms 10024 KB Output is correct
59 Correct 121 ms 10064 KB Output is correct
60 Correct 158 ms 10316 KB Output is correct
61 Correct 91 ms 10028 KB Output is correct
62 Correct 162 ms 10072 KB Output is correct
63 Correct 154 ms 10200 KB Output is correct
64 Correct 91 ms 10140 KB Output is correct
65 Correct 117 ms 10080 KB Output is correct
66 Correct 126 ms 10136 KB Output is correct
67 Correct 120 ms 10056 KB Output is correct
68 Correct 134 ms 10180 KB Output is correct
69 Correct 138 ms 10028 KB Output is correct
70 Correct 118 ms 10032 KB Output is correct
71 Correct 603 ms 15252 KB Output is correct
72 Correct 616 ms 15384 KB Output is correct
73 Correct 1357 ms 13848 KB Output is correct
74 Execution timed out 2088 ms 13392 KB Time limit exceeded
75 Halted 0 ms 0 KB -