답안 #1103974

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1103974 2024-10-22T13:46:28 Z Whisper Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
604 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
int numSnake, numQuery;
string label;
string s[MASK(20) + 5];


int dp_on[MASK(20) + 5], dp_off[MASK(20) + 5];
void process(void){
    cin >> numSnake >> numQuery >> label;
    #define cnt(x)                 __builtin_popcount(x)

    for (int mask = 0; mask < MASK(numSnake); ++mask){
        dp_on[mask] = label[mask] - '0';
        dp_off[(MASK(numSnake) - 1) ^ mask] = label[mask] - '0';
    }

    for (int i = 0; i < numSnake; ++i){
        for (int mask = 0; mask < MASK(numSnake); ++mask) if(!BIT(mask, i)){
            dp_on[mask]  += dp_on[mask ^ MASK(i)];
            dp_off[mask] += dp_off[mask ^ MASK(i)];
        }
    }


    for (int qu = 1; qu <= numQuery; ++qu){
        int on = 0, off = 0, ask = 0;
        string  s; cin >> s;
        for (int i = 0; i < numSnake; ++i){
            if(s[i] == '1') on  |= MASK(numSnake - i - 1);
            if(s[i] == '0') off |= MASK(numSnake - i - 1);
            if(s[i] == '?') ask |= MASK(numSnake - i - 1);
        }

        if(cnt(on) <= 6){
            int ans = dp_off[off];
            for (int sub = on; sub > 0; sub = (sub - 1) & on){
                if(cnt(sub) & 1){
                    ans -= dp_off[sub | off];
                }
                else{
                    ans += dp_off[sub | off];
                }
            }
            cout << ans << '\n';
            continue;
        }

        if(cnt(off) <= 6){
            int ans = dp_on[on];
            for (int sub = off; sub > 0; sub = (sub - 1) & off){
                if(cnt(sub) & 1){
                    ans -= dp_on[on | sub];
                }
                else{
                    ans += dp_on[on | sub];
                }
            }
            cout << ans << '\n';
            continue;
        }

        if(cnt(ask) <= 6){
            int ans = label[on] - '0';
            for (int sub = ask; sub > 0; sub = (sub - 1) & ask){
                ans += label[on | sub] - '0';
            }
            cout << ans << '\n';
        }
    }

}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
    return (0 ^ 0);
}




# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 37200 KB Output is correct
2 Correct 7 ms 37200 KB Output is correct
3 Correct 8 ms 37200 KB Output is correct
4 Correct 9 ms 37380 KB Output is correct
5 Correct 8 ms 37200 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 7 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37200 KB Output is correct
10 Correct 7 ms 37136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 37200 KB Output is correct
2 Correct 7 ms 37200 KB Output is correct
3 Correct 8 ms 37200 KB Output is correct
4 Correct 9 ms 37380 KB Output is correct
5 Correct 8 ms 37200 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 7 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37200 KB Output is correct
10 Correct 7 ms 37136 KB Output is correct
11 Correct 229 ms 51840 KB Output is correct
12 Correct 157 ms 51528 KB Output is correct
13 Correct 167 ms 50684 KB Output is correct
14 Correct 164 ms 51016 KB Output is correct
15 Correct 153 ms 52040 KB Output is correct
16 Correct 204 ms 50992 KB Output is correct
17 Correct 212 ms 51144 KB Output is correct
18 Correct 108 ms 52808 KB Output is correct
19 Correct 211 ms 49772 KB Output is correct
20 Correct 206 ms 51528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 37200 KB Output is correct
2 Correct 7 ms 37200 KB Output is correct
3 Correct 8 ms 37200 KB Output is correct
4 Correct 9 ms 37380 KB Output is correct
5 Correct 8 ms 37200 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 7 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37200 KB Output is correct
10 Correct 7 ms 37136 KB Output is correct
11 Correct 229 ms 51840 KB Output is correct
12 Correct 157 ms 51528 KB Output is correct
13 Correct 167 ms 50684 KB Output is correct
14 Correct 164 ms 51016 KB Output is correct
15 Correct 153 ms 52040 KB Output is correct
16 Correct 204 ms 50992 KB Output is correct
17 Correct 212 ms 51144 KB Output is correct
18 Correct 108 ms 52808 KB Output is correct
19 Correct 211 ms 49772 KB Output is correct
20 Correct 206 ms 51528 KB Output is correct
21 Correct 354 ms 54912 KB Output is correct
22 Correct 185 ms 55112 KB Output is correct
23 Correct 241 ms 54344 KB Output is correct
24 Correct 236 ms 53832 KB Output is correct
25 Correct 206 ms 56144 KB Output is correct
26 Correct 268 ms 54384 KB Output is correct
27 Correct 270 ms 54344 KB Output is correct
28 Correct 122 ms 56904 KB Output is correct
29 Correct 326 ms 52808 KB Output is correct
30 Correct 244 ms 55156 KB Output is correct
31 Correct 235 ms 54864 KB Output is correct
32 Correct 239 ms 54856 KB Output is correct
33 Correct 210 ms 53728 KB Output is correct
34 Correct 237 ms 53832 KB Output is correct
35 Correct 239 ms 54344 KB Output is correct
36 Correct 112 ms 52992 KB Output is correct
37 Correct 154 ms 54856 KB Output is correct
38 Correct 264 ms 52808 KB Output is correct
39 Correct 342 ms 54156 KB Output is correct
40 Correct 266 ms 53996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 37200 KB Output is correct
2 Correct 7 ms 37200 KB Output is correct
3 Correct 8 ms 37200 KB Output is correct
4 Correct 9 ms 37380 KB Output is correct
5 Correct 8 ms 37200 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 7 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37200 KB Output is correct
10 Correct 7 ms 37136 KB Output is correct
11 Correct 42 ms 53996 KB Output is correct
12 Correct 43 ms 54008 KB Output is correct
13 Correct 47 ms 53996 KB Output is correct
14 Correct 57 ms 53996 KB Output is correct
15 Correct 45 ms 53992 KB Output is correct
16 Correct 58 ms 54236 KB Output is correct
17 Correct 51 ms 53988 KB Output is correct
18 Correct 40 ms 54252 KB Output is correct
19 Correct 41 ms 53996 KB Output is correct
20 Correct 51 ms 54004 KB Output is correct
21 Correct 52 ms 53992 KB Output is correct
22 Correct 56 ms 54044 KB Output is correct
23 Correct 48 ms 53996 KB Output is correct
24 Correct 50 ms 53916 KB Output is correct
25 Correct 52 ms 53996 KB Output is correct
26 Correct 39 ms 53996 KB Output is correct
27 Correct 44 ms 54004 KB Output is correct
28 Correct 45 ms 53964 KB Output is correct
29 Correct 53 ms 54000 KB Output is correct
30 Correct 51 ms 53992 KB Output is correct
31 Correct 44 ms 53996 KB Output is correct
32 Correct 55 ms 53992 KB Output is correct
33 Correct 56 ms 53964 KB Output is correct
34 Correct 38 ms 53996 KB Output is correct
35 Correct 46 ms 53992 KB Output is correct
36 Correct 64 ms 53968 KB Output is correct
37 Correct 49 ms 54144 KB Output is correct
38 Correct 50 ms 53996 KB Output is correct
39 Correct 46 ms 53996 KB Output is correct
40 Correct 53 ms 54004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 37200 KB Output is correct
2 Correct 7 ms 37200 KB Output is correct
3 Correct 8 ms 37200 KB Output is correct
4 Correct 9 ms 37380 KB Output is correct
5 Correct 8 ms 37200 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 7 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37200 KB Output is correct
10 Correct 7 ms 37136 KB Output is correct
11 Correct 229 ms 51840 KB Output is correct
12 Correct 157 ms 51528 KB Output is correct
13 Correct 167 ms 50684 KB Output is correct
14 Correct 164 ms 51016 KB Output is correct
15 Correct 153 ms 52040 KB Output is correct
16 Correct 204 ms 50992 KB Output is correct
17 Correct 212 ms 51144 KB Output is correct
18 Correct 108 ms 52808 KB Output is correct
19 Correct 211 ms 49772 KB Output is correct
20 Correct 206 ms 51528 KB Output is correct
21 Correct 354 ms 54912 KB Output is correct
22 Correct 185 ms 55112 KB Output is correct
23 Correct 241 ms 54344 KB Output is correct
24 Correct 236 ms 53832 KB Output is correct
25 Correct 206 ms 56144 KB Output is correct
26 Correct 268 ms 54384 KB Output is correct
27 Correct 270 ms 54344 KB Output is correct
28 Correct 122 ms 56904 KB Output is correct
29 Correct 326 ms 52808 KB Output is correct
30 Correct 244 ms 55156 KB Output is correct
31 Correct 235 ms 54864 KB Output is correct
32 Correct 239 ms 54856 KB Output is correct
33 Correct 210 ms 53728 KB Output is correct
34 Correct 237 ms 53832 KB Output is correct
35 Correct 239 ms 54344 KB Output is correct
36 Correct 112 ms 52992 KB Output is correct
37 Correct 154 ms 54856 KB Output is correct
38 Correct 264 ms 52808 KB Output is correct
39 Correct 342 ms 54156 KB Output is correct
40 Correct 266 ms 53996 KB Output is correct
41 Correct 42 ms 53996 KB Output is correct
42 Correct 43 ms 54008 KB Output is correct
43 Correct 47 ms 53996 KB Output is correct
44 Correct 57 ms 53996 KB Output is correct
45 Correct 45 ms 53992 KB Output is correct
46 Correct 58 ms 54236 KB Output is correct
47 Correct 51 ms 53988 KB Output is correct
48 Correct 40 ms 54252 KB Output is correct
49 Correct 41 ms 53996 KB Output is correct
50 Correct 51 ms 54004 KB Output is correct
51 Correct 52 ms 53992 KB Output is correct
52 Correct 56 ms 54044 KB Output is correct
53 Correct 48 ms 53996 KB Output is correct
54 Correct 50 ms 53916 KB Output is correct
55 Correct 52 ms 53996 KB Output is correct
56 Correct 39 ms 53996 KB Output is correct
57 Correct 44 ms 54004 KB Output is correct
58 Correct 45 ms 53964 KB Output is correct
59 Correct 53 ms 54000 KB Output is correct
60 Correct 51 ms 53992 KB Output is correct
61 Correct 44 ms 53996 KB Output is correct
62 Correct 55 ms 53992 KB Output is correct
63 Correct 56 ms 53964 KB Output is correct
64 Correct 38 ms 53996 KB Output is correct
65 Correct 46 ms 53992 KB Output is correct
66 Correct 64 ms 53968 KB Output is correct
67 Correct 49 ms 54144 KB Output is correct
68 Correct 50 ms 53996 KB Output is correct
69 Correct 46 ms 53996 KB Output is correct
70 Correct 53 ms 54004 KB Output is correct
71 Correct 302 ms 65536 KB Output is correct
72 Correct 349 ms 65536 KB Output is correct
73 Correct 403 ms 65536 KB Output is correct
74 Correct 536 ms 65536 KB Output is correct
75 Correct 307 ms 65536 KB Output is correct
76 Correct 501 ms 65536 KB Output is correct
77 Correct 464 ms 65536 KB Output is correct
78 Correct 180 ms 65536 KB Output is correct
79 Correct 274 ms 65536 KB Output is correct
80 Correct 297 ms 65536 KB Output is correct
81 Correct 417 ms 65536 KB Output is correct
82 Correct 582 ms 65536 KB Output is correct
83 Correct 343 ms 65536 KB Output is correct
84 Correct 559 ms 65536 KB Output is correct
85 Correct 604 ms 65536 KB Output is correct
86 Correct 196 ms 65536 KB Output is correct
87 Correct 337 ms 65536 KB Output is correct
88 Correct 299 ms 65536 KB Output is correct
89 Correct 406 ms 65536 KB Output is correct
90 Correct 361 ms 65536 KB Output is correct
91 Correct 286 ms 65536 KB Output is correct
92 Correct 519 ms 65536 KB Output is correct
93 Correct 449 ms 65536 KB Output is correct
94 Correct 165 ms 65536 KB Output is correct
95 Correct 400 ms 65536 KB Output is correct
96 Correct 405 ms 65536 KB Output is correct
97 Correct 399 ms 65536 KB Output is correct
98 Correct 419 ms 65536 KB Output is correct
99 Correct 459 ms 65536 KB Output is correct
100 Correct 469 ms 65536 KB Output is correct