Submission #860616

# Submission time Handle Problem Language Result Execution time Memory
860616 2023-10-13T14:43:51 Z green_gold_dog Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1970 ms 65536 KB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;

constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
ll FIR = 7, SEC = 13;

random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());

template<typename T>
bool assign_max(T& a, T b) {
        if (b > a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
bool assign_min(T& a, T b) {
        if (b < a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
T square(T a) {
        return a * a;
}

template<>
struct std::hash<pair<ll, ll>> {
        ll operator() (pair<ll, ll> p) const {
                return ((__int128)p.first * MOD + p.second) % INF64;
        }
};

void solve() {
        ll l, q;
        cin >> l >> q;
        if (FIR > l) {
                FIR = l;
        }
        SEC = l - FIR;
        vector<ll> ans(q, 0);
        vector<ll> arr(1 << l, 0);
        for (ll i = 0; i < (1 << l); i++) {
                char c;
                cin >> c;
                arr[i] = c - '0';
        }
        vector<vector<pair<ll, ll>>> all(1 << FIR);
        vector<ll> o1(q), o0(q);
        for (ll i = 0; i < q; i++) {
                string s;
                cin >> s;
                reverse(s.begin(), s.end());
                for (ll j = 0; j < FIR; j++) {
                        if (s[j] == '1') {
                                o1[i] += (1 << j);
                                o0[i] += (1 << j);
                        }
                        if (s[j] == '0') {
                                o1[i] += (1 << j);
                        }
                        if (s[j] == '?') {
                                o0[i] += (1 << j);
                        }
                }
                ll x = 0;
                for (ll j = FIR; j < l; j++) {
                        x *= 3;
                        if (s[j] == '1') {
                                x++;
                        }
                        if (s[j] == '?') {
                                x += 2;
                        }
                }
                all[o1[i] & o0[i]].emplace_back(i, x);
        }
        vector<ll> p3(1, 0);
        ll mx = 1;
        for (ll i = 0; i < SEC; i++) {
                p3.push_back(mx);
                mx *= 3;
        }
        vector<pair<ll, ll>> aa;
        for (ll i = 0; i < mx; i++) {
                ll col = 0;
                ll now = i;
                while (now) {
                        col += now % 3 == 2;
                        now /= 3;
                }
                aa.emplace_back(col, i);
        }
        sort(aa.begin(), aa.end());
        for (auto&[col, i] : aa) {
                col = 0;
                ll now = i;
                for (ll j = 0; j < SEC; j++) {
                        if (now % 3 == 2) {
                                col = j + 1;
                                break;
                        }
                        now /= 3;
                }
                if (col == 0) {
                        now = i;
                        for (ll j = SEC - 1; j >= 0; j--) {
                                if (now % 3 == 1) {
                                        col -= 1 << j;
                                }
                                now /= 3;
                        }
                }
        }
        vector<ll> dp(mx, 0);
        for (ll i = 0; i < (1 << FIR); i++) {
                for (auto[fir, j] : aa) {
                        if (fir <= 0) {
                                fir = -fir;
                                dp[j] = arr[(fir << FIR) + i];
                        } else {
                                dp[j] = dp[j - p3[fir]] + dp[j - p3[fir] * 2];
                        }
                }
                for (auto[j, k] : all[i]) {
                        ans[j] += dp[k];
                        if ((1 << FIR) - 1 != (i | o1[j])) {
                                all[(((i | o1[j]) + 1) | o1[j]) & o0[j]].emplace_back(j, k);
                        }
                }
                all[i].clear();
                all[i].shrink_to_fit();
        }
        for (auto i : ans) {
                cout << i << '\n';
        }
}

int main() {
        if (IS_FILE) {
                freopen("", "r", stdin);
                freopen("", "w", stdout);
        }
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        ll t = 1;
        if (IS_TEST_CASES) {
                cin >> t;
        }
        for (ll i = 0; i < t; i++) {
                solve();
        }
}

Compilation message

snake_escaping.cpp:12:22: warning: overflow in conversion from 'long int' to 'll' {aka 'int'} changes value from '9000000000000000000' to '-494665728' [-Woverflow]
   12 | constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:158:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |                 freopen("", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp:159:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |                 freopen("", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 443 ms 49048 KB Output is correct
12 Correct 387 ms 42952 KB Output is correct
13 Correct 250 ms 37828 KB Output is correct
14 Correct 273 ms 35368 KB Output is correct
15 Correct 585 ms 42352 KB Output is correct
16 Correct 361 ms 38468 KB Output is correct
17 Correct 341 ms 35768 KB Output is correct
18 Correct 869 ms 47028 KB Output is correct
19 Correct 173 ms 32848 KB Output is correct
20 Correct 573 ms 43692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 443 ms 49048 KB Output is correct
12 Correct 387 ms 42952 KB Output is correct
13 Correct 250 ms 37828 KB Output is correct
14 Correct 273 ms 35368 KB Output is correct
15 Correct 585 ms 42352 KB Output is correct
16 Correct 361 ms 38468 KB Output is correct
17 Correct 341 ms 35768 KB Output is correct
18 Correct 869 ms 47028 KB Output is correct
19 Correct 173 ms 32848 KB Output is correct
20 Correct 573 ms 43692 KB Output is correct
21 Correct 527 ms 61424 KB Output is correct
22 Correct 451 ms 45436 KB Output is correct
23 Correct 332 ms 40948 KB Output is correct
24 Correct 306 ms 38956 KB Output is correct
25 Correct 716 ms 45160 KB Output is correct
26 Correct 422 ms 38940 KB Output is correct
27 Correct 359 ms 38504 KB Output is correct
28 Correct 916 ms 50152 KB Output is correct
29 Correct 189 ms 37200 KB Output is correct
30 Correct 509 ms 52568 KB Output is correct
31 Correct 433 ms 41676 KB Output is correct
32 Correct 426 ms 41092 KB Output is correct
33 Correct 273 ms 39532 KB Output is correct
34 Correct 314 ms 38692 KB Output is correct
35 Correct 368 ms 38300 KB Output is correct
36 Correct 146 ms 33748 KB Output is correct
37 Correct 340 ms 50100 KB Output is correct
38 Correct 202 ms 34836 KB Output is correct
39 Correct 318 ms 39432 KB Output is correct
40 Correct 317 ms 38612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 572 ms 25868 KB Output is correct
12 Correct 533 ms 26028 KB Output is correct
13 Correct 550 ms 28460 KB Output is correct
14 Correct 534 ms 28528 KB Output is correct
15 Correct 536 ms 28748 KB Output is correct
16 Correct 543 ms 27940 KB Output is correct
17 Correct 525 ms 28316 KB Output is correct
18 Correct 576 ms 29096 KB Output is correct
19 Correct 509 ms 26788 KB Output is correct
20 Correct 559 ms 29596 KB Output is correct
21 Correct 560 ms 28084 KB Output is correct
22 Correct 596 ms 27052 KB Output is correct
23 Correct 567 ms 27720 KB Output is correct
24 Correct 570 ms 28332 KB Output is correct
25 Correct 534 ms 27812 KB Output is correct
26 Correct 506 ms 27696 KB Output is correct
27 Correct 538 ms 27540 KB Output is correct
28 Correct 525 ms 27816 KB Output is correct
29 Correct 521 ms 28844 KB Output is correct
30 Correct 525 ms 26972 KB Output is correct
31 Correct 514 ms 27720 KB Output is correct
32 Correct 527 ms 27392 KB Output is correct
33 Correct 545 ms 28100 KB Output is correct
34 Correct 516 ms 26820 KB Output is correct
35 Correct 535 ms 28500 KB Output is correct
36 Correct 597 ms 27656 KB Output is correct
37 Correct 555 ms 27408 KB Output is correct
38 Correct 545 ms 29072 KB Output is correct
39 Correct 536 ms 28004 KB Output is correct
40 Correct 542 ms 27804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 443 ms 49048 KB Output is correct
12 Correct 387 ms 42952 KB Output is correct
13 Correct 250 ms 37828 KB Output is correct
14 Correct 273 ms 35368 KB Output is correct
15 Correct 585 ms 42352 KB Output is correct
16 Correct 361 ms 38468 KB Output is correct
17 Correct 341 ms 35768 KB Output is correct
18 Correct 869 ms 47028 KB Output is correct
19 Correct 173 ms 32848 KB Output is correct
20 Correct 573 ms 43692 KB Output is correct
21 Correct 527 ms 61424 KB Output is correct
22 Correct 451 ms 45436 KB Output is correct
23 Correct 332 ms 40948 KB Output is correct
24 Correct 306 ms 38956 KB Output is correct
25 Correct 716 ms 45160 KB Output is correct
26 Correct 422 ms 38940 KB Output is correct
27 Correct 359 ms 38504 KB Output is correct
28 Correct 916 ms 50152 KB Output is correct
29 Correct 189 ms 37200 KB Output is correct
30 Correct 509 ms 52568 KB Output is correct
31 Correct 433 ms 41676 KB Output is correct
32 Correct 426 ms 41092 KB Output is correct
33 Correct 273 ms 39532 KB Output is correct
34 Correct 314 ms 38692 KB Output is correct
35 Correct 368 ms 38300 KB Output is correct
36 Correct 146 ms 33748 KB Output is correct
37 Correct 340 ms 50100 KB Output is correct
38 Correct 202 ms 34836 KB Output is correct
39 Correct 318 ms 39432 KB Output is correct
40 Correct 317 ms 38612 KB Output is correct
41 Correct 572 ms 25868 KB Output is correct
42 Correct 533 ms 26028 KB Output is correct
43 Correct 550 ms 28460 KB Output is correct
44 Correct 534 ms 28528 KB Output is correct
45 Correct 536 ms 28748 KB Output is correct
46 Correct 543 ms 27940 KB Output is correct
47 Correct 525 ms 28316 KB Output is correct
48 Correct 576 ms 29096 KB Output is correct
49 Correct 509 ms 26788 KB Output is correct
50 Correct 559 ms 29596 KB Output is correct
51 Correct 560 ms 28084 KB Output is correct
52 Correct 596 ms 27052 KB Output is correct
53 Correct 567 ms 27720 KB Output is correct
54 Correct 570 ms 28332 KB Output is correct
55 Correct 534 ms 27812 KB Output is correct
56 Correct 506 ms 27696 KB Output is correct
57 Correct 538 ms 27540 KB Output is correct
58 Correct 525 ms 27816 KB Output is correct
59 Correct 521 ms 28844 KB Output is correct
60 Correct 525 ms 26972 KB Output is correct
61 Correct 514 ms 27720 KB Output is correct
62 Correct 527 ms 27392 KB Output is correct
63 Correct 545 ms 28100 KB Output is correct
64 Correct 516 ms 26820 KB Output is correct
65 Correct 535 ms 28500 KB Output is correct
66 Correct 597 ms 27656 KB Output is correct
67 Correct 555 ms 27408 KB Output is correct
68 Correct 545 ms 29072 KB Output is correct
69 Correct 536 ms 28004 KB Output is correct
70 Correct 542 ms 27804 KB Output is correct
71 Correct 1221 ms 65536 KB Output is correct
72 Correct 1198 ms 65536 KB Output is correct
73 Correct 986 ms 65536 KB Output is correct
74 Correct 1255 ms 65536 KB Output is correct
75 Correct 1970 ms 65536 KB Output is correct
76 Correct 1211 ms 65536 KB Output is correct
77 Correct 1232 ms 65536 KB Output is correct
78 Correct 1602 ms 65536 KB Output is correct
79 Correct 860 ms 65536 KB Output is correct
80 Correct 1370 ms 65536 KB Output is correct
81 Correct 1511 ms 65536 KB Output is correct
82 Correct 1164 ms 65536 KB Output is correct
83 Correct 917 ms 65536 KB Output is correct
84 Correct 1081 ms 65536 KB Output is correct
85 Correct 1181 ms 65536 KB Output is correct
86 Correct 699 ms 64804 KB Output is correct
87 Correct 1154 ms 65536 KB Output is correct
88 Correct 794 ms 65536 KB Output is correct
89 Correct 1053 ms 65536 KB Output is correct
90 Correct 1092 ms 65536 KB Output is correct
91 Correct 869 ms 65536 KB Output is correct
92 Correct 1172 ms 65536 KB Output is correct
93 Correct 1184 ms 65536 KB Output is correct
94 Correct 669 ms 64296 KB Output is correct
95 Correct 1244 ms 65536 KB Output is correct
96 Correct 1242 ms 65536 KB Output is correct
97 Correct 1240 ms 65536 KB Output is correct
98 Correct 1309 ms 65536 KB Output is correct
99 Correct 1256 ms 65536 KB Output is correct
100 Correct 1248 ms 65536 KB Output is correct