답안 #860627

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
860627 2023-10-13T14:57:23 Z green_gold_dog Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1802 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;
  		ll xxxx = l + q;
        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 'void solve()':
snake_escaping.cpp:58:8: warning: unused variable 'xxxx' [-Wunused-variable]
   58 |     ll xxxx = l + q;
      |        ^~~~
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);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 448 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 448 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 413 ms 46652 KB Output is correct
12 Correct 438 ms 40316 KB Output is correct
13 Correct 288 ms 35724 KB Output is correct
14 Correct 302 ms 33132 KB Output is correct
15 Correct 678 ms 39996 KB Output is correct
16 Correct 369 ms 36184 KB Output is correct
17 Correct 343 ms 33512 KB Output is correct
18 Correct 892 ms 45092 KB Output is correct
19 Correct 169 ms 30672 KB Output is correct
20 Correct 559 ms 41504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 448 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 413 ms 46652 KB Output is correct
12 Correct 438 ms 40316 KB Output is correct
13 Correct 288 ms 35724 KB Output is correct
14 Correct 302 ms 33132 KB Output is correct
15 Correct 678 ms 39996 KB Output is correct
16 Correct 369 ms 36184 KB Output is correct
17 Correct 343 ms 33512 KB Output is correct
18 Correct 892 ms 45092 KB Output is correct
19 Correct 169 ms 30672 KB Output is correct
20 Correct 559 ms 41504 KB Output is correct
21 Correct 683 ms 58464 KB Output is correct
22 Correct 436 ms 42668 KB Output is correct
23 Correct 301 ms 37876 KB Output is correct
24 Correct 356 ms 36212 KB Output is correct
25 Correct 682 ms 43020 KB Output is correct
26 Correct 345 ms 36108 KB Output is correct
27 Correct 353 ms 35772 KB Output is correct
28 Correct 913 ms 46888 KB Output is correct
29 Correct 195 ms 34132 KB Output is correct
30 Correct 499 ms 49788 KB Output is correct
31 Correct 515 ms 38656 KB Output is correct
32 Correct 416 ms 38280 KB Output is correct
33 Correct 270 ms 36648 KB Output is correct
34 Correct 380 ms 35816 KB Output is correct
35 Correct 436 ms 35220 KB Output is correct
36 Correct 147 ms 30992 KB Output is correct
37 Correct 359 ms 45532 KB Output is correct
38 Correct 192 ms 31756 KB Output is correct
39 Correct 342 ms 36504 KB Output is correct
40 Correct 312 ms 35756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 448 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 535 ms 28968 KB Output is correct
12 Correct 536 ms 28020 KB Output is correct
13 Correct 545 ms 26796 KB Output is correct
14 Correct 578 ms 27760 KB Output is correct
15 Correct 573 ms 28192 KB Output is correct
16 Correct 528 ms 28588 KB Output is correct
17 Correct 551 ms 27820 KB Output is correct
18 Correct 565 ms 29524 KB Output is correct
19 Correct 534 ms 27876 KB Output is correct
20 Correct 526 ms 28728 KB Output is correct
21 Correct 553 ms 27200 KB Output is correct
22 Correct 538 ms 27432 KB Output is correct
23 Correct 529 ms 28788 KB Output is correct
24 Correct 540 ms 28264 KB Output is correct
25 Correct 548 ms 27168 KB Output is correct
26 Correct 528 ms 27472 KB Output is correct
27 Correct 535 ms 28328 KB Output is correct
28 Correct 515 ms 26720 KB Output is correct
29 Correct 535 ms 28852 KB Output is correct
30 Correct 530 ms 28552 KB Output is correct
31 Correct 534 ms 27208 KB Output is correct
32 Correct 520 ms 27576 KB Output is correct
33 Correct 542 ms 28456 KB Output is correct
34 Correct 515 ms 26668 KB Output is correct
35 Correct 535 ms 29016 KB Output is correct
36 Correct 548 ms 27376 KB Output is correct
37 Correct 552 ms 27364 KB Output is correct
38 Correct 538 ms 28152 KB Output is correct
39 Correct 594 ms 27580 KB Output is correct
40 Correct 586 ms 27920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 448 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 413 ms 46652 KB Output is correct
12 Correct 438 ms 40316 KB Output is correct
13 Correct 288 ms 35724 KB Output is correct
14 Correct 302 ms 33132 KB Output is correct
15 Correct 678 ms 39996 KB Output is correct
16 Correct 369 ms 36184 KB Output is correct
17 Correct 343 ms 33512 KB Output is correct
18 Correct 892 ms 45092 KB Output is correct
19 Correct 169 ms 30672 KB Output is correct
20 Correct 559 ms 41504 KB Output is correct
21 Correct 683 ms 58464 KB Output is correct
22 Correct 436 ms 42668 KB Output is correct
23 Correct 301 ms 37876 KB Output is correct
24 Correct 356 ms 36212 KB Output is correct
25 Correct 682 ms 43020 KB Output is correct
26 Correct 345 ms 36108 KB Output is correct
27 Correct 353 ms 35772 KB Output is correct
28 Correct 913 ms 46888 KB Output is correct
29 Correct 195 ms 34132 KB Output is correct
30 Correct 499 ms 49788 KB Output is correct
31 Correct 515 ms 38656 KB Output is correct
32 Correct 416 ms 38280 KB Output is correct
33 Correct 270 ms 36648 KB Output is correct
34 Correct 380 ms 35816 KB Output is correct
35 Correct 436 ms 35220 KB Output is correct
36 Correct 147 ms 30992 KB Output is correct
37 Correct 359 ms 45532 KB Output is correct
38 Correct 192 ms 31756 KB Output is correct
39 Correct 342 ms 36504 KB Output is correct
40 Correct 312 ms 35756 KB Output is correct
41 Correct 535 ms 28968 KB Output is correct
42 Correct 536 ms 28020 KB Output is correct
43 Correct 545 ms 26796 KB Output is correct
44 Correct 578 ms 27760 KB Output is correct
45 Correct 573 ms 28192 KB Output is correct
46 Correct 528 ms 28588 KB Output is correct
47 Correct 551 ms 27820 KB Output is correct
48 Correct 565 ms 29524 KB Output is correct
49 Correct 534 ms 27876 KB Output is correct
50 Correct 526 ms 28728 KB Output is correct
51 Correct 553 ms 27200 KB Output is correct
52 Correct 538 ms 27432 KB Output is correct
53 Correct 529 ms 28788 KB Output is correct
54 Correct 540 ms 28264 KB Output is correct
55 Correct 548 ms 27168 KB Output is correct
56 Correct 528 ms 27472 KB Output is correct
57 Correct 535 ms 28328 KB Output is correct
58 Correct 515 ms 26720 KB Output is correct
59 Correct 535 ms 28852 KB Output is correct
60 Correct 530 ms 28552 KB Output is correct
61 Correct 534 ms 27208 KB Output is correct
62 Correct 520 ms 27576 KB Output is correct
63 Correct 542 ms 28456 KB Output is correct
64 Correct 515 ms 26668 KB Output is correct
65 Correct 535 ms 29016 KB Output is correct
66 Correct 548 ms 27376 KB Output is correct
67 Correct 552 ms 27364 KB Output is correct
68 Correct 538 ms 28152 KB Output is correct
69 Correct 594 ms 27580 KB Output is correct
70 Correct 586 ms 27920 KB Output is correct
71 Correct 1310 ms 65536 KB Output is correct
72 Correct 1337 ms 65536 KB Output is correct
73 Correct 1025 ms 65536 KB Output is correct
74 Correct 1139 ms 65536 KB Output is correct
75 Correct 1802 ms 65536 KB Output is correct
76 Correct 1192 ms 65536 KB Output is correct
77 Correct 1244 ms 65536 KB Output is correct
78 Correct 1500 ms 65536 KB Output is correct
79 Correct 813 ms 64172 KB Output is correct
80 Correct 1345 ms 65536 KB Output is correct
81 Correct 1510 ms 65536 KB Output is correct
82 Correct 1179 ms 65536 KB Output is correct
83 Correct 976 ms 65536 KB Output is correct
84 Correct 1114 ms 65536 KB Output is correct
85 Correct 1250 ms 65536 KB Output is correct
86 Correct 717 ms 60948 KB Output is correct
87 Correct 1163 ms 65536 KB Output is correct
88 Correct 826 ms 64664 KB Output is correct
89 Correct 1048 ms 65536 KB Output is correct
90 Correct 1156 ms 65536 KB Output is correct
91 Correct 887 ms 65536 KB Output is correct
92 Correct 1196 ms 65536 KB Output is correct
93 Correct 1194 ms 65536 KB Output is correct
94 Correct 697 ms 59528 KB Output is correct
95 Correct 1317 ms 65536 KB Output is correct
96 Correct 1311 ms 65536 KB Output is correct
97 Correct 1308 ms 65536 KB Output is correct
98 Correct 1393 ms 65536 KB Output is correct
99 Correct 1334 ms 65536 KB Output is correct
100 Correct 1344 ms 65536 KB Output is correct