Submission #1103975

# Submission time Handle Problem Language Result Execution time Memory
1103975 2024-10-22T13:47:14 Z vjudge1 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
558 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);
}




# Verdict Execution time Memory Grader output
1 Correct 8 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 8 ms 37200 KB Output is correct
5 Correct 8 ms 37248 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 8 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37416 KB Output is correct
10 Correct 7 ms 37200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 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 8 ms 37200 KB Output is correct
5 Correct 8 ms 37248 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 8 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37416 KB Output is correct
10 Correct 7 ms 37200 KB Output is correct
11 Correct 211 ms 41032 KB Output is correct
12 Correct 167 ms 51528 KB Output is correct
13 Correct 182 ms 48552 KB Output is correct
14 Correct 181 ms 50504 KB Output is correct
15 Correct 165 ms 51616 KB Output is correct
16 Correct 212 ms 42056 KB Output is correct
17 Correct 212 ms 50660 KB Output is correct
18 Correct 104 ms 42240 KB Output is correct
19 Correct 199 ms 39240 KB Output is correct
20 Correct 202 ms 49204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 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 8 ms 37200 KB Output is correct
5 Correct 8 ms 37248 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 8 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37416 KB Output is correct
10 Correct 7 ms 37200 KB Output is correct
11 Correct 211 ms 41032 KB Output is correct
12 Correct 167 ms 51528 KB Output is correct
13 Correct 182 ms 48552 KB Output is correct
14 Correct 181 ms 50504 KB Output is correct
15 Correct 165 ms 51616 KB Output is correct
16 Correct 212 ms 42056 KB Output is correct
17 Correct 212 ms 50660 KB Output is correct
18 Correct 104 ms 42240 KB Output is correct
19 Correct 199 ms 39240 KB Output is correct
20 Correct 202 ms 49204 KB Output is correct
21 Correct 298 ms 52040 KB Output is correct
22 Correct 167 ms 48456 KB Output is correct
23 Correct 213 ms 53576 KB Output is correct
24 Correct 211 ms 40264 KB Output is correct
25 Correct 190 ms 55620 KB Output is correct
26 Correct 254 ms 40776 KB Output is correct
27 Correct 258 ms 53100 KB Output is correct
28 Correct 127 ms 43336 KB Output is correct
29 Correct 321 ms 49992 KB Output is correct
30 Correct 239 ms 52552 KB Output is correct
31 Correct 233 ms 54600 KB Output is correct
32 Correct 229 ms 54604 KB Output is correct
33 Correct 215 ms 53292 KB Output is correct
34 Correct 253 ms 51016 KB Output is correct
35 Correct 258 ms 54344 KB Output is correct
36 Correct 112 ms 51784 KB Output is correct
37 Correct 163 ms 54856 KB Output is correct
38 Correct 270 ms 39240 KB Output is correct
39 Correct 340 ms 53744 KB Output is correct
40 Correct 242 ms 40260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 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 8 ms 37200 KB Output is correct
5 Correct 8 ms 37248 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 8 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37416 KB Output is correct
10 Correct 7 ms 37200 KB Output is correct
11 Correct 38 ms 51960 KB Output is correct
12 Correct 40 ms 53996 KB Output is correct
13 Correct 44 ms 53992 KB Output is correct
14 Correct 50 ms 51876 KB Output is correct
15 Correct 41 ms 53996 KB Output is correct
16 Correct 52 ms 53912 KB Output is correct
17 Correct 46 ms 51884 KB Output is correct
18 Correct 36 ms 54168 KB Output is correct
19 Correct 39 ms 53964 KB Output is correct
20 Correct 41 ms 54196 KB Output is correct
21 Correct 45 ms 53300 KB Output is correct
22 Correct 52 ms 53996 KB Output is correct
23 Correct 51 ms 53996 KB Output is correct
24 Correct 50 ms 53968 KB Output is correct
25 Correct 50 ms 54084 KB Output is correct
26 Correct 36 ms 53992 KB Output is correct
27 Correct 40 ms 53996 KB Output is correct
28 Correct 42 ms 53040 KB Output is correct
29 Correct 47 ms 51928 KB Output is correct
30 Correct 51 ms 53908 KB Output is correct
31 Correct 43 ms 53228 KB Output is correct
32 Correct 56 ms 53228 KB Output is correct
33 Correct 48 ms 51948 KB Output is correct
34 Correct 38 ms 53996 KB Output is correct
35 Correct 46 ms 53992 KB Output is correct
36 Correct 48 ms 53996 KB Output is correct
37 Correct 53 ms 53996 KB Output is correct
38 Correct 51 ms 53996 KB Output is correct
39 Correct 49 ms 53908 KB Output is correct
40 Correct 54 ms 53992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 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 8 ms 37200 KB Output is correct
5 Correct 8 ms 37248 KB Output is correct
6 Correct 8 ms 37200 KB Output is correct
7 Correct 8 ms 37200 KB Output is correct
8 Correct 8 ms 37200 KB Output is correct
9 Correct 7 ms 37416 KB Output is correct
10 Correct 7 ms 37200 KB Output is correct
11 Correct 211 ms 41032 KB Output is correct
12 Correct 167 ms 51528 KB Output is correct
13 Correct 182 ms 48552 KB Output is correct
14 Correct 181 ms 50504 KB Output is correct
15 Correct 165 ms 51616 KB Output is correct
16 Correct 212 ms 42056 KB Output is correct
17 Correct 212 ms 50660 KB Output is correct
18 Correct 104 ms 42240 KB Output is correct
19 Correct 199 ms 39240 KB Output is correct
20 Correct 202 ms 49204 KB Output is correct
21 Correct 298 ms 52040 KB Output is correct
22 Correct 167 ms 48456 KB Output is correct
23 Correct 213 ms 53576 KB Output is correct
24 Correct 211 ms 40264 KB Output is correct
25 Correct 190 ms 55620 KB Output is correct
26 Correct 254 ms 40776 KB Output is correct
27 Correct 258 ms 53100 KB Output is correct
28 Correct 127 ms 43336 KB Output is correct
29 Correct 321 ms 49992 KB Output is correct
30 Correct 239 ms 52552 KB Output is correct
31 Correct 233 ms 54600 KB Output is correct
32 Correct 229 ms 54604 KB Output is correct
33 Correct 215 ms 53292 KB Output is correct
34 Correct 253 ms 51016 KB Output is correct
35 Correct 258 ms 54344 KB Output is correct
36 Correct 112 ms 51784 KB Output is correct
37 Correct 163 ms 54856 KB Output is correct
38 Correct 270 ms 39240 KB Output is correct
39 Correct 340 ms 53744 KB Output is correct
40 Correct 242 ms 40260 KB Output is correct
41 Correct 38 ms 51960 KB Output is correct
42 Correct 40 ms 53996 KB Output is correct
43 Correct 44 ms 53992 KB Output is correct
44 Correct 50 ms 51876 KB Output is correct
45 Correct 41 ms 53996 KB Output is correct
46 Correct 52 ms 53912 KB Output is correct
47 Correct 46 ms 51884 KB Output is correct
48 Correct 36 ms 54168 KB Output is correct
49 Correct 39 ms 53964 KB Output is correct
50 Correct 41 ms 54196 KB Output is correct
51 Correct 45 ms 53300 KB Output is correct
52 Correct 52 ms 53996 KB Output is correct
53 Correct 51 ms 53996 KB Output is correct
54 Correct 50 ms 53968 KB Output is correct
55 Correct 50 ms 54084 KB Output is correct
56 Correct 36 ms 53992 KB Output is correct
57 Correct 40 ms 53996 KB Output is correct
58 Correct 42 ms 53040 KB Output is correct
59 Correct 47 ms 51928 KB Output is correct
60 Correct 51 ms 53908 KB Output is correct
61 Correct 43 ms 53228 KB Output is correct
62 Correct 56 ms 53228 KB Output is correct
63 Correct 48 ms 51948 KB Output is correct
64 Correct 38 ms 53996 KB Output is correct
65 Correct 46 ms 53992 KB Output is correct
66 Correct 48 ms 53996 KB Output is correct
67 Correct 53 ms 53996 KB Output is correct
68 Correct 51 ms 53996 KB Output is correct
69 Correct 49 ms 53908 KB Output is correct
70 Correct 54 ms 53992 KB Output is correct
71 Correct 311 ms 65536 KB Output is correct
72 Correct 323 ms 65536 KB Output is correct
73 Correct 388 ms 65536 KB Output is correct
74 Correct 558 ms 65536 KB Output is correct
75 Correct 325 ms 65536 KB Output is correct
76 Correct 543 ms 65536 KB Output is correct
77 Correct 502 ms 65536 KB Output is correct
78 Correct 204 ms 65536 KB Output is correct
79 Correct 257 ms 65536 KB Output is correct
80 Correct 270 ms 65536 KB Output is correct
81 Correct 380 ms 65536 KB Output is correct
82 Correct 537 ms 65536 KB Output is correct
83 Correct 320 ms 65536 KB Output is correct
84 Correct 515 ms 65536 KB Output is correct
85 Correct 509 ms 65536 KB Output is correct
86 Correct 177 ms 63464 KB Output is correct
87 Correct 282 ms 65536 KB Output is correct
88 Correct 287 ms 65536 KB Output is correct
89 Correct 432 ms 65536 KB Output is correct
90 Correct 421 ms 65536 KB Output is correct
91 Correct 340 ms 65536 KB Output is correct
92 Correct 555 ms 65536 KB Output is correct
93 Correct 466 ms 65536 KB Output is correct
94 Correct 160 ms 65536 KB Output is correct
95 Correct 380 ms 65536 KB Output is correct
96 Correct 364 ms 65536 KB Output is correct
97 Correct 427 ms 65536 KB Output is correct
98 Correct 385 ms 65536 KB Output is correct
99 Correct 377 ms 65536 KB Output is correct
100 Correct 447 ms 65536 KB Output is correct