답안 #1098454

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1098454 2024-10-09T12:13:26 Z anhthi Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
606 ms 47320 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define heap priority_queue
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)

 
template <class X, class Y>
    inline bool maximize(X &x, Y y) {
        return (x < y ? x = y, true : false);
    }
 
template <class X, class Y>
    inline bool minimize(X &x, Y y) {
        return (x > y ? x = y, true : false);
    }
 
template <class X>
    inline void compress(vector<X> &a) {
        sort(all(a));
        a.resize(unique(all(a)) - a.begin());
    }
 
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcnt(ll x) { return __builtin_popcount(x); }
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
    return l + abs((ll) rng()) % (r - l + 1);
}
 
const ll oo = (ll) 1e18 + 5;
const int inf = (ll) 1e9 + 7, mod = (ll) 1e9 + 7;
 
const int N = 21, LOG = 21;
 
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }

int L, Q;
char cost[MASK(N) + 5];
ll f[MASK(N) + 5], dp[MASK(N) + 5];

int main(void) {
 
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> L >> Q;
    forn(i, 0, MASK(L) - 1) {
        cin >> cost[i];
        f[i] = dp[i] = cost[i] - '0'; 
    }

    forn(i, 0, L - 1) forn(msk, 0, MASK(L) - 1)
        if (BIT(msk, i)) f[msk] += f[msk - MASK(i)];

    forn(i, 0, L - 1) forn(msk, 0, MASK(L) - 1)
        if (!BIT(msk, i)) dp[msk] += dp[msk + MASK(i)];

    string s;
    rep(_, Q) {
        cin >> s;

        int mask1 = 0, mask2 = 0, mask3 = 0;
        forn(i, 0, L - 1) {
            int cur = MASK(L - i - 1);
            if (s[i] == '1')
                mask1 += cur;
            else if (s[i] == '?') mask2 += cur;
            else mask3 += cur;
        }
        ll res = 0;
        if (popcnt(mask2) <= 6) {
            for (int msk = mask2; ; msk = (msk - 1) & mask2) {
                res += cost[msk + mask1] - '0';
                if (!msk) break;
            }
        }
        else if (popcnt(mask1) <= 6) {
            for (int msk = mask1; ; msk = (msk - 1) & mask1) {
                if ((popcnt(mask1) - popcnt(msk)) & 1) res -= f[msk + mask2];
                else res += f[msk + mask2];
                if (!msk) break;
            }
        }
        else {
            for (int msk = mask3; ; msk = (msk - 1) & mask3) {
                if (popcnt(msk) & 1) res -= dp[msk + mask1];
                else res += dp[msk + mask1];
                if (!msk) break;
            }
        }
        cout << res << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 484 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 476 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 476 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 484 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 476 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 476 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 198 ms 15196 KB Output is correct
12 Correct 176 ms 14664 KB Output is correct
13 Correct 161 ms 14140 KB Output is correct
14 Correct 163 ms 14148 KB Output is correct
15 Correct 210 ms 15184 KB Output is correct
16 Correct 182 ms 14164 KB Output is correct
17 Correct 182 ms 14164 KB Output is correct
18 Correct 111 ms 16212 KB Output is correct
19 Correct 142 ms 13120 KB Output is correct
20 Correct 172 ms 14676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 484 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 476 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 476 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 198 ms 15196 KB Output is correct
12 Correct 176 ms 14664 KB Output is correct
13 Correct 161 ms 14140 KB Output is correct
14 Correct 163 ms 14148 KB Output is correct
15 Correct 210 ms 15184 KB Output is correct
16 Correct 182 ms 14164 KB Output is correct
17 Correct 182 ms 14164 KB Output is correct
18 Correct 111 ms 16212 KB Output is correct
19 Correct 142 ms 13120 KB Output is correct
20 Correct 172 ms 14676 KB Output is correct
21 Correct 360 ms 18228 KB Output is correct
22 Correct 193 ms 18256 KB Output is correct
23 Correct 199 ms 17440 KB Output is correct
24 Correct 203 ms 17236 KB Output is correct
25 Correct 203 ms 19160 KB Output is correct
26 Correct 213 ms 17744 KB Output is correct
27 Correct 218 ms 17540 KB Output is correct
28 Correct 114 ms 20108 KB Output is correct
29 Correct 143 ms 16272 KB Output is correct
30 Correct 246 ms 18572 KB Output is correct
31 Correct 240 ms 18264 KB Output is correct
32 Correct 223 ms 18228 KB Output is correct
33 Correct 198 ms 16976 KB Output is correct
34 Correct 195 ms 17236 KB Output is correct
35 Correct 225 ms 17688 KB Output is correct
36 Correct 109 ms 16208 KB Output is correct
37 Correct 233 ms 18132 KB Output is correct
38 Correct 162 ms 16212 KB Output is correct
39 Correct 246 ms 17236 KB Output is correct
40 Correct 224 ms 17236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 484 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 476 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 476 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 48 ms 20052 KB Output is correct
12 Correct 57 ms 20052 KB Output is correct
13 Correct 56 ms 20052 KB Output is correct
14 Correct 70 ms 20056 KB Output is correct
15 Correct 54 ms 20204 KB Output is correct
16 Correct 66 ms 20052 KB Output is correct
17 Correct 57 ms 20048 KB Output is correct
18 Correct 47 ms 20304 KB Output is correct
19 Correct 49 ms 20052 KB Output is correct
20 Correct 51 ms 20204 KB Output is correct
21 Correct 57 ms 20060 KB Output is correct
22 Correct 67 ms 19960 KB Output is correct
23 Correct 53 ms 19948 KB Output is correct
24 Correct 62 ms 20052 KB Output is correct
25 Correct 59 ms 19944 KB Output is correct
26 Correct 44 ms 20048 KB Output is correct
27 Correct 52 ms 20048 KB Output is correct
28 Correct 48 ms 20048 KB Output is correct
29 Correct 56 ms 20048 KB Output is correct
30 Correct 57 ms 20048 KB Output is correct
31 Correct 48 ms 20052 KB Output is correct
32 Correct 63 ms 19952 KB Output is correct
33 Correct 58 ms 19928 KB Output is correct
34 Correct 46 ms 20052 KB Output is correct
35 Correct 54 ms 20052 KB Output is correct
36 Correct 55 ms 20056 KB Output is correct
37 Correct 54 ms 20052 KB Output is correct
38 Correct 54 ms 20048 KB Output is correct
39 Correct 54 ms 20056 KB Output is correct
40 Correct 56 ms 20148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 484 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 476 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 476 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 198 ms 15196 KB Output is correct
12 Correct 176 ms 14664 KB Output is correct
13 Correct 161 ms 14140 KB Output is correct
14 Correct 163 ms 14148 KB Output is correct
15 Correct 210 ms 15184 KB Output is correct
16 Correct 182 ms 14164 KB Output is correct
17 Correct 182 ms 14164 KB Output is correct
18 Correct 111 ms 16212 KB Output is correct
19 Correct 142 ms 13120 KB Output is correct
20 Correct 172 ms 14676 KB Output is correct
21 Correct 360 ms 18228 KB Output is correct
22 Correct 193 ms 18256 KB Output is correct
23 Correct 199 ms 17440 KB Output is correct
24 Correct 203 ms 17236 KB Output is correct
25 Correct 203 ms 19160 KB Output is correct
26 Correct 213 ms 17744 KB Output is correct
27 Correct 218 ms 17540 KB Output is correct
28 Correct 114 ms 20108 KB Output is correct
29 Correct 143 ms 16272 KB Output is correct
30 Correct 246 ms 18572 KB Output is correct
31 Correct 240 ms 18264 KB Output is correct
32 Correct 223 ms 18228 KB Output is correct
33 Correct 198 ms 16976 KB Output is correct
34 Correct 195 ms 17236 KB Output is correct
35 Correct 225 ms 17688 KB Output is correct
36 Correct 109 ms 16208 KB Output is correct
37 Correct 233 ms 18132 KB Output is correct
38 Correct 162 ms 16212 KB Output is correct
39 Correct 246 ms 17236 KB Output is correct
40 Correct 224 ms 17236 KB Output is correct
41 Correct 48 ms 20052 KB Output is correct
42 Correct 57 ms 20052 KB Output is correct
43 Correct 56 ms 20052 KB Output is correct
44 Correct 70 ms 20056 KB Output is correct
45 Correct 54 ms 20204 KB Output is correct
46 Correct 66 ms 20052 KB Output is correct
47 Correct 57 ms 20048 KB Output is correct
48 Correct 47 ms 20304 KB Output is correct
49 Correct 49 ms 20052 KB Output is correct
50 Correct 51 ms 20204 KB Output is correct
51 Correct 57 ms 20060 KB Output is correct
52 Correct 67 ms 19960 KB Output is correct
53 Correct 53 ms 19948 KB Output is correct
54 Correct 62 ms 20052 KB Output is correct
55 Correct 59 ms 19944 KB Output is correct
56 Correct 44 ms 20048 KB Output is correct
57 Correct 52 ms 20048 KB Output is correct
58 Correct 48 ms 20048 KB Output is correct
59 Correct 56 ms 20048 KB Output is correct
60 Correct 57 ms 20048 KB Output is correct
61 Correct 48 ms 20052 KB Output is correct
62 Correct 63 ms 19952 KB Output is correct
63 Correct 58 ms 19928 KB Output is correct
64 Correct 46 ms 20052 KB Output is correct
65 Correct 54 ms 20052 KB Output is correct
66 Correct 55 ms 20056 KB Output is correct
67 Correct 54 ms 20052 KB Output is correct
68 Correct 54 ms 20048 KB Output is correct
69 Correct 54 ms 20056 KB Output is correct
70 Correct 56 ms 20148 KB Output is correct
71 Correct 245 ms 44356 KB Output is correct
72 Correct 281 ms 44520 KB Output is correct
73 Correct 330 ms 43092 KB Output is correct
74 Correct 606 ms 43348 KB Output is correct
75 Correct 330 ms 45220 KB Output is correct
76 Correct 523 ms 43752 KB Output is correct
77 Correct 450 ms 43600 KB Output is correct
78 Correct 194 ms 47320 KB Output is correct
79 Correct 270 ms 41200 KB Output is correct
80 Correct 303 ms 44380 KB Output is correct
81 Correct 437 ms 44252 KB Output is correct
82 Correct 604 ms 43348 KB Output is correct
83 Correct 287 ms 42576 KB Output is correct
84 Correct 392 ms 43344 KB Output is correct
85 Correct 416 ms 43604 KB Output is correct
86 Correct 156 ms 41352 KB Output is correct
87 Correct 245 ms 44364 KB Output is correct
88 Correct 238 ms 41208 KB Output is correct
89 Correct 336 ms 43088 KB Output is correct
90 Correct 408 ms 43444 KB Output is correct
91 Correct 274 ms 42576 KB Output is correct
92 Correct 537 ms 43688 KB Output is correct
93 Correct 451 ms 43600 KB Output is correct
94 Correct 169 ms 41556 KB Output is correct
95 Correct 399 ms 43484 KB Output is correct
96 Correct 427 ms 43500 KB Output is correct
97 Correct 403 ms 43344 KB Output is correct
98 Correct 394 ms 43416 KB Output is correct
99 Correct 402 ms 43496 KB Output is correct
100 Correct 374 ms 43344 KB Output is correct