답안 #971228

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
971228 2024-04-28T08:06:41 Z vyshniak_n Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1036 ms 55756 KB
//#pragma optimize("Ofast")
//#pragma optimize("unroll-loops")
//#pragma target("avx,avx2")

#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>

typedef long long ll;
typedef long double ld;

using namespace std;

#define el '\n'
#define ff first
#define ss second
#define pb push_back
#define popb pop_back
#define point pair <ll, ll>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

const ll N = 1e3 + 10;
const ll INF = 2e18 + 10;
const ll inf = 2e9 + 10;

void solve() {
    ll l, q;
    cin >> l >> q;

    string s;
    cin >> s;

    vector <ll> dpL(1 << l), dpR(1 << l), bits(1 << l);
    for (ll i = 0; i < (1 << l); i++)
        for (ll j = 0; j < l; j++)
            bits[i] += i >> j & 1;
    for (ll i = 0; i < (1 << l); i++)
        dpL[i] = s[i] - '0', dpR[i] = s[i] - '0';
    for (ll i = 0; i < l; i++)
        for (ll j = 0; j < (1 << l); j++)
            if (j >> i & 1)
                dpL[j] += dpL[j ^ (1 << i)];
    for (ll i = 0; i < l; i++)
        for (ll j = (1 << l) - 1; j >= 0; j--)
            if (j >> i & 1)
                dpR[j ^ (1 << i)] += dpR[j];

    while (q--) {
        string t;
        cin >> t;

        reverse(all(t));

        ll cnt1 = 0, cnt0 = 0, cnt = 0, res = 0;
        for (ll i = 0; i < l; i++) {
            if (t[i] == '1') cnt1++;
            else if (t[i] == '?') cnt++;
            else cnt0++;
        }

        ll mn = min({cnt1, cnt0, cnt});

        if (mn == cnt1) {
            ll mask = 0, c = 0;
            for (ll i = 0; i < l; i++) {
                if (t[i] == '?') c += 1 << i;
                else if (t[i] == '1') mask += 1 << i;
            }

            ll v = mask;

            if (bits[v | c] % 2 == 0) res += dpL[v | c];
            else res -= dpL[v | c];

            while (v > 0) {
                v = (v - 1) & mask;

                if (bits[v | c] % 2 == 0) res += dpL[v | c];
                else res -= dpL[v | c];
            }

            res = abs(res);
        } else if (mn == cnt) {
            ll mask = 0, c = 0;
            for (ll i = 0; i < l; i++) {
                if (t[i] == '?') mask += 1 << i;
                else if (t[i] == '1') c += 1 << i;
            }

            ll v = mask;
            res += s[v | c] - '0';

            while (v > 0) {
                v = (v - 1) & mask;
                res += s[v | c] - '0';
            }
        } else {
            ll mask = 0, c = 0;
            for (ll i = 0; i < l; i++) {
                if (t[i] == '1') c += 1 << i;
                else if (t[i] == '0') mask += 1 << i;
            }

            ll v = mask;

            if (bits[v | c] % 2 == 0) res += dpR[v | c];
            else res -= dpR[v | c];

            while (v > 0) {
                v = (v - 1) & mask;

                if (bits[v | c] % 2 == 0) res += dpR[v | c];
                else res -= dpR[v | c];
            }

            res = abs(res);
        }

        cout << res << el;
    }
    return;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int tests = 1;
    //cin >> tests;

    while (tests--)
        solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 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 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 344 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 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 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 183 ms 15128 KB Output is correct
12 Correct 230 ms 14928 KB Output is correct
13 Correct 235 ms 14000 KB Output is correct
14 Correct 250 ms 14016 KB Output is correct
15 Correct 227 ms 15188 KB Output is correct
16 Correct 254 ms 14304 KB Output is correct
17 Correct 265 ms 14164 KB Output is correct
18 Correct 151 ms 16132 KB Output is correct
19 Correct 180 ms 12968 KB Output is correct
20 Correct 221 ms 14660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 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 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 183 ms 15128 KB Output is correct
12 Correct 230 ms 14928 KB Output is correct
13 Correct 235 ms 14000 KB Output is correct
14 Correct 250 ms 14016 KB Output is correct
15 Correct 227 ms 15188 KB Output is correct
16 Correct 254 ms 14304 KB Output is correct
17 Correct 265 ms 14164 KB Output is correct
18 Correct 151 ms 16132 KB Output is correct
19 Correct 180 ms 12968 KB Output is correct
20 Correct 221 ms 14660 KB Output is correct
21 Correct 250 ms 18244 KB Output is correct
22 Correct 281 ms 18388 KB Output is correct
23 Correct 287 ms 17392 KB Output is correct
24 Correct 325 ms 17236 KB Output is correct
25 Correct 268 ms 19524 KB Output is correct
26 Correct 314 ms 17844 KB Output is correct
27 Correct 323 ms 17628 KB Output is correct
28 Correct 182 ms 20200 KB Output is correct
29 Correct 232 ms 16208 KB Output is correct
30 Correct 270 ms 18384 KB Output is correct
31 Correct 298 ms 18308 KB Output is correct
32 Correct 335 ms 18328 KB Output is correct
33 Correct 290 ms 17148 KB Output is correct
34 Correct 331 ms 17156 KB Output is correct
35 Correct 319 ms 17752 KB Output is correct
36 Correct 149 ms 16208 KB Output is correct
37 Correct 244 ms 18256 KB Output is correct
38 Correct 253 ms 16468 KB Output is correct
39 Correct 304 ms 17420 KB Output is correct
40 Correct 300 ms 17232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 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 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 85 ms 28516 KB Output is correct
12 Correct 88 ms 28516 KB Output is correct
13 Correct 86 ms 28264 KB Output is correct
14 Correct 113 ms 28660 KB Output is correct
15 Correct 91 ms 28424 KB Output is correct
16 Correct 106 ms 28444 KB Output is correct
17 Correct 94 ms 28400 KB Output is correct
18 Correct 73 ms 28520 KB Output is correct
19 Correct 78 ms 28152 KB Output is correct
20 Correct 81 ms 28400 KB Output is correct
21 Correct 90 ms 28400 KB Output is correct
22 Correct 128 ms 28664 KB Output is correct
23 Correct 85 ms 28416 KB Output is correct
24 Correct 95 ms 28272 KB Output is correct
25 Correct 95 ms 28440 KB Output is correct
26 Correct 76 ms 28200 KB Output is correct
27 Correct 81 ms 28404 KB Output is correct
28 Correct 77 ms 28152 KB Output is correct
29 Correct 85 ms 28400 KB Output is correct
30 Correct 91 ms 28404 KB Output is correct
31 Correct 87 ms 28392 KB Output is correct
32 Correct 102 ms 28408 KB Output is correct
33 Correct 95 ms 28324 KB Output is correct
34 Correct 69 ms 28276 KB Output is correct
35 Correct 94 ms 28276 KB Output is correct
36 Correct 96 ms 28284 KB Output is correct
37 Correct 92 ms 28260 KB Output is correct
38 Correct 92 ms 28404 KB Output is correct
39 Correct 91 ms 28284 KB Output is correct
40 Correct 87 ms 28400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 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 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 183 ms 15128 KB Output is correct
12 Correct 230 ms 14928 KB Output is correct
13 Correct 235 ms 14000 KB Output is correct
14 Correct 250 ms 14016 KB Output is correct
15 Correct 227 ms 15188 KB Output is correct
16 Correct 254 ms 14304 KB Output is correct
17 Correct 265 ms 14164 KB Output is correct
18 Correct 151 ms 16132 KB Output is correct
19 Correct 180 ms 12968 KB Output is correct
20 Correct 221 ms 14660 KB Output is correct
21 Correct 250 ms 18244 KB Output is correct
22 Correct 281 ms 18388 KB Output is correct
23 Correct 287 ms 17392 KB Output is correct
24 Correct 325 ms 17236 KB Output is correct
25 Correct 268 ms 19524 KB Output is correct
26 Correct 314 ms 17844 KB Output is correct
27 Correct 323 ms 17628 KB Output is correct
28 Correct 182 ms 20200 KB Output is correct
29 Correct 232 ms 16208 KB Output is correct
30 Correct 270 ms 18384 KB Output is correct
31 Correct 298 ms 18308 KB Output is correct
32 Correct 335 ms 18328 KB Output is correct
33 Correct 290 ms 17148 KB Output is correct
34 Correct 331 ms 17156 KB Output is correct
35 Correct 319 ms 17752 KB Output is correct
36 Correct 149 ms 16208 KB Output is correct
37 Correct 244 ms 18256 KB Output is correct
38 Correct 253 ms 16468 KB Output is correct
39 Correct 304 ms 17420 KB Output is correct
40 Correct 300 ms 17232 KB Output is correct
41 Correct 85 ms 28516 KB Output is correct
42 Correct 88 ms 28516 KB Output is correct
43 Correct 86 ms 28264 KB Output is correct
44 Correct 113 ms 28660 KB Output is correct
45 Correct 91 ms 28424 KB Output is correct
46 Correct 106 ms 28444 KB Output is correct
47 Correct 94 ms 28400 KB Output is correct
48 Correct 73 ms 28520 KB Output is correct
49 Correct 78 ms 28152 KB Output is correct
50 Correct 81 ms 28400 KB Output is correct
51 Correct 90 ms 28400 KB Output is correct
52 Correct 128 ms 28664 KB Output is correct
53 Correct 85 ms 28416 KB Output is correct
54 Correct 95 ms 28272 KB Output is correct
55 Correct 95 ms 28440 KB Output is correct
56 Correct 76 ms 28200 KB Output is correct
57 Correct 81 ms 28404 KB Output is correct
58 Correct 77 ms 28152 KB Output is correct
59 Correct 85 ms 28400 KB Output is correct
60 Correct 91 ms 28404 KB Output is correct
61 Correct 87 ms 28392 KB Output is correct
62 Correct 102 ms 28408 KB Output is correct
63 Correct 95 ms 28324 KB Output is correct
64 Correct 69 ms 28276 KB Output is correct
65 Correct 94 ms 28276 KB Output is correct
66 Correct 96 ms 28284 KB Output is correct
67 Correct 92 ms 28260 KB Output is correct
68 Correct 92 ms 28404 KB Output is correct
69 Correct 91 ms 28284 KB Output is correct
70 Correct 87 ms 28400 KB Output is correct
71 Correct 491 ms 52600 KB Output is correct
72 Correct 554 ms 52780 KB Output is correct
73 Correct 558 ms 51532 KB Output is correct
74 Correct 999 ms 51664 KB Output is correct
75 Correct 461 ms 53548 KB Output is correct
76 Correct 881 ms 51952 KB Output is correct
77 Correct 807 ms 52120 KB Output is correct
78 Correct 279 ms 55756 KB Output is correct
79 Correct 400 ms 49644 KB Output is correct
80 Correct 506 ms 52948 KB Output is correct
81 Correct 690 ms 52724 KB Output is correct
82 Correct 1036 ms 51960 KB Output is correct
83 Correct 446 ms 50792 KB Output is correct
84 Correct 787 ms 51608 KB Output is correct
85 Correct 801 ms 51852 KB Output is correct
86 Correct 246 ms 49648 KB Output is correct
87 Correct 457 ms 52840 KB Output is correct
88 Correct 438 ms 49740 KB Output is correct
89 Correct 550 ms 51424 KB Output is correct
90 Correct 568 ms 51892 KB Output is correct
91 Correct 427 ms 50960 KB Output is correct
92 Correct 928 ms 52004 KB Output is correct
93 Correct 770 ms 51968 KB Output is correct
94 Correct 257 ms 49700 KB Output is correct
95 Correct 697 ms 52152 KB Output is correct
96 Correct 680 ms 51764 KB Output is correct
97 Correct 685 ms 51812 KB Output is correct
98 Correct 676 ms 52084 KB Output is correct
99 Correct 698 ms 51900 KB Output is correct
100 Correct 662 ms 51920 KB Output is correct