Submission #860630

# Submission time Handle Problem Language Result Execution time Memory
860630 2023-10-13T14:58:44 Z green_gold_dog Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1657 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> xxx(100, l + q / 2);
        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:159:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |                 freopen("", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp:160:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |                 freopen("", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 472 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 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 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 2 ms 344 KB Output is correct
2 Correct 1 ms 472 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 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 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 457 ms 43764 KB Output is correct
12 Correct 459 ms 37588 KB Output is correct
13 Correct 273 ms 32908 KB Output is correct
14 Correct 371 ms 30196 KB Output is correct
15 Correct 932 ms 37228 KB Output is correct
16 Correct 414 ms 33712 KB Output is correct
17 Correct 477 ms 30896 KB Output is correct
18 Correct 925 ms 41712 KB Output is correct
19 Correct 167 ms 27724 KB Output is correct
20 Correct 711 ms 38188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 472 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 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 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 457 ms 43764 KB Output is correct
12 Correct 459 ms 37588 KB Output is correct
13 Correct 273 ms 32908 KB Output is correct
14 Correct 371 ms 30196 KB Output is correct
15 Correct 932 ms 37228 KB Output is correct
16 Correct 414 ms 33712 KB Output is correct
17 Correct 477 ms 30896 KB Output is correct
18 Correct 925 ms 41712 KB Output is correct
19 Correct 167 ms 27724 KB Output is correct
20 Correct 711 ms 38188 KB Output is correct
21 Correct 803 ms 53624 KB Output is correct
22 Correct 502 ms 39580 KB Output is correct
23 Correct 305 ms 34600 KB Output is correct
24 Correct 312 ms 32556 KB Output is correct
25 Correct 916 ms 38768 KB Output is correct
26 Correct 380 ms 33032 KB Output is correct
27 Correct 462 ms 32184 KB Output is correct
28 Correct 913 ms 43616 KB Output is correct
29 Correct 210 ms 30840 KB Output is correct
30 Correct 599 ms 45080 KB Output is correct
31 Correct 482 ms 35320 KB Output is correct
32 Correct 417 ms 34952 KB Output is correct
33 Correct 312 ms 33408 KB Output is correct
34 Correct 332 ms 32300 KB Output is correct
35 Correct 459 ms 32112 KB Output is correct
36 Correct 157 ms 27836 KB Output is correct
37 Correct 375 ms 41712 KB Output is correct
38 Correct 194 ms 28500 KB Output is correct
39 Correct 337 ms 33288 KB Output is correct
40 Correct 363 ms 32232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 472 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 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 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 566 ms 28844 KB Output is correct
12 Correct 534 ms 27556 KB Output is correct
13 Correct 597 ms 27064 KB Output is correct
14 Correct 567 ms 27848 KB Output is correct
15 Correct 579 ms 28488 KB Output is correct
16 Correct 551 ms 27572 KB Output is correct
17 Correct 552 ms 26436 KB Output is correct
18 Correct 586 ms 28836 KB Output is correct
19 Correct 581 ms 27652 KB Output is correct
20 Correct 569 ms 28588 KB Output is correct
21 Correct 580 ms 27472 KB Output is correct
22 Correct 554 ms 27048 KB Output is correct
23 Correct 550 ms 26876 KB Output is correct
24 Correct 547 ms 26772 KB Output is correct
25 Correct 600 ms 27064 KB Output is correct
26 Correct 545 ms 25644 KB Output is correct
27 Correct 555 ms 28244 KB Output is correct
28 Correct 529 ms 25804 KB Output is correct
29 Correct 525 ms 27304 KB Output is correct
30 Correct 532 ms 26876 KB Output is correct
31 Correct 538 ms 26352 KB Output is correct
32 Correct 528 ms 27064 KB Output is correct
33 Correct 534 ms 27308 KB Output is correct
34 Correct 507 ms 27508 KB Output is correct
35 Correct 543 ms 27728 KB Output is correct
36 Correct 561 ms 27396 KB Output is correct
37 Correct 524 ms 27792 KB Output is correct
38 Correct 552 ms 26988 KB Output is correct
39 Correct 538 ms 27084 KB Output is correct
40 Correct 549 ms 27380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 472 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 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 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 457 ms 43764 KB Output is correct
12 Correct 459 ms 37588 KB Output is correct
13 Correct 273 ms 32908 KB Output is correct
14 Correct 371 ms 30196 KB Output is correct
15 Correct 932 ms 37228 KB Output is correct
16 Correct 414 ms 33712 KB Output is correct
17 Correct 477 ms 30896 KB Output is correct
18 Correct 925 ms 41712 KB Output is correct
19 Correct 167 ms 27724 KB Output is correct
20 Correct 711 ms 38188 KB Output is correct
21 Correct 803 ms 53624 KB Output is correct
22 Correct 502 ms 39580 KB Output is correct
23 Correct 305 ms 34600 KB Output is correct
24 Correct 312 ms 32556 KB Output is correct
25 Correct 916 ms 38768 KB Output is correct
26 Correct 380 ms 33032 KB Output is correct
27 Correct 462 ms 32184 KB Output is correct
28 Correct 913 ms 43616 KB Output is correct
29 Correct 210 ms 30840 KB Output is correct
30 Correct 599 ms 45080 KB Output is correct
31 Correct 482 ms 35320 KB Output is correct
32 Correct 417 ms 34952 KB Output is correct
33 Correct 312 ms 33408 KB Output is correct
34 Correct 332 ms 32300 KB Output is correct
35 Correct 459 ms 32112 KB Output is correct
36 Correct 157 ms 27836 KB Output is correct
37 Correct 375 ms 41712 KB Output is correct
38 Correct 194 ms 28500 KB Output is correct
39 Correct 337 ms 33288 KB Output is correct
40 Correct 363 ms 32232 KB Output is correct
41 Correct 566 ms 28844 KB Output is correct
42 Correct 534 ms 27556 KB Output is correct
43 Correct 597 ms 27064 KB Output is correct
44 Correct 567 ms 27848 KB Output is correct
45 Correct 579 ms 28488 KB Output is correct
46 Correct 551 ms 27572 KB Output is correct
47 Correct 552 ms 26436 KB Output is correct
48 Correct 586 ms 28836 KB Output is correct
49 Correct 581 ms 27652 KB Output is correct
50 Correct 569 ms 28588 KB Output is correct
51 Correct 580 ms 27472 KB Output is correct
52 Correct 554 ms 27048 KB Output is correct
53 Correct 550 ms 26876 KB Output is correct
54 Correct 547 ms 26772 KB Output is correct
55 Correct 600 ms 27064 KB Output is correct
56 Correct 545 ms 25644 KB Output is correct
57 Correct 555 ms 28244 KB Output is correct
58 Correct 529 ms 25804 KB Output is correct
59 Correct 525 ms 27304 KB Output is correct
60 Correct 532 ms 26876 KB Output is correct
61 Correct 538 ms 26352 KB Output is correct
62 Correct 528 ms 27064 KB Output is correct
63 Correct 534 ms 27308 KB Output is correct
64 Correct 507 ms 27508 KB Output is correct
65 Correct 543 ms 27728 KB Output is correct
66 Correct 561 ms 27396 KB Output is correct
67 Correct 524 ms 27792 KB Output is correct
68 Correct 552 ms 26988 KB Output is correct
69 Correct 538 ms 27084 KB Output is correct
70 Correct 549 ms 27380 KB Output is correct
71 Correct 1243 ms 65536 KB Output is correct
72 Correct 1190 ms 64788 KB Output is correct
73 Correct 994 ms 63432 KB Output is correct
74 Correct 1123 ms 60580 KB Output is correct
75 Correct 1657 ms 65536 KB Output is correct
76 Correct 1157 ms 62616 KB Output is correct
77 Correct 1131 ms 62492 KB Output is correct
78 Correct 1475 ms 65536 KB Output is correct
79 Correct 779 ms 47972 KB Output is correct
80 Correct 1284 ms 65536 KB Output is correct
81 Correct 1359 ms 65536 KB Output is correct
82 Correct 1128 ms 57784 KB Output is correct
83 Correct 909 ms 52164 KB Output is correct
84 Correct 1044 ms 60088 KB Output is correct
85 Correct 1157 ms 58580 KB Output is correct
86 Correct 698 ms 43292 KB Output is correct
87 Correct 1096 ms 65536 KB Output is correct
88 Correct 777 ms 46972 KB Output is correct
89 Correct 1012 ms 54936 KB Output is correct
90 Correct 1102 ms 65536 KB Output is correct
91 Correct 889 ms 64968 KB Output is correct
92 Correct 1219 ms 65536 KB Output is correct
93 Correct 1208 ms 65536 KB Output is correct
94 Correct 684 ms 54804 KB Output is correct
95 Correct 1244 ms 65536 KB Output is correct
96 Correct 1280 ms 65536 KB Output is correct
97 Correct 1266 ms 65536 KB Output is correct
98 Correct 1263 ms 65536 KB Output is correct
99 Correct 1249 ms 65536 KB Output is correct
100 Correct 1258 ms 65536 KB Output is correct