Submission #535732

# Submission time Handle Problem Language Result Execution time Memory
535732 2022-03-11T03:53:34 Z sare Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
578 ms 17712 KB
//In the name of Allah :)
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
string to_string(char c) { return string(1,c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return (string)s; }
string to_string(string s) { return s; }
template<class A> string to_string(complex<A> c) { 
    stringstream ss; ss << c; return ss.str(); }
string to_string(vector<bool> v) { 
    string res = "{"; for(int i = 0; i < (int)v.size(); i++) res += char('0'+v[i]);
    res += "}"; return res; }
template<size_t SZ> string to_string(bitset<SZ> b) {
    string res = ""; for(size_t i = 0; i < SZ; i++) res += char('0'+b[i]);
    return res; }
template<class A, class B> string to_string(pair<A,B> p);
template<class T> string to_string(T v) { // containers with begin(), end()
    bool fst = 1; string res = "{";
    for (const auto& x: v) {
        if (!fst) res += ", ";
        fst = 0; res += to_string(x);
    }
    res += "}"; return res;
}
template<class A, class B> string to_string(pair<A,B> p) {
    return "("+to_string(p.first)+", "+to_string(p.second)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << to_string(h); if (sizeof...(t)) cerr << ", ";
    DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define wis(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
#else
#define wis(...) 0
#endif
typedef long long ll;
typedef long double ld;
#define all(x) (x).begin(), (x).end()
 
const int MAXN = 20, MAXQ = 1e6;
int l, q, dp[2][1 << MAXN];
string s;
 
int main() {
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> l >> q >> s;
    for (int i = 0; i < (1 << l); i++) {
        dp[0][i] = dp[1][i] = s[i] - '0';
    }
    for (int k = 0; k < l; k++) {
        for (int i = 0; i < (1 << l); i++) {
            if (i >> k & 1) {
                dp[0][i] += dp[0][i ^ (1 << k)];
            }
            else {
                dp[1][i] += dp[1][i ^ (1 << k)];
            }
        }
    }
    while(q--) {
        int n[3] = {}, mask[3] = {};
        string t;
        cin >> t;
        for (int i = 0; i < l; i++) {
            if (t[i] == '?') {
                n[2]++;
                mask[2] ^= 1 << (l - i - 1);
            }
            else {
                n[t[i] - '0']++;
                mask[t[i] - '0'] ^= 1 << (l - i - 1);
            }
        }
        wis(n[0], n[1], n[2], mask[0], mask[1], mask[2]);
        ll ans = 0;
        if (n[2] <= min(n[0], n[1])) {
            for (int sub = mask[2]; sub; sub = (sub - 1) & mask[2]) {
                ans += s[sub | mask[1]] - '0';
            }
            ans += s[mask[1]] - '0';
        }
        else if (n[1] <= n[0]) {
            for (int sub = mask[1]; sub; sub = (sub - 1) & mask[1]) {
                ans += dp[0][sub ^ mask[1] ^ mask[2]] * (__builtin_popcount(sub) % 2 ? -1 : 1);
            } 
            ans += dp[0][mask[1] ^ mask[2]];
        }
        else {
            for (int sub = mask[0]; sub; sub = (sub - 1) & mask[0]) {
                ans += dp[1][((1 << l) - 1) ^ sub ^ mask[0] ^ mask[2]] * (__builtin_popcount(sub) % 2 ? -1 : 1);
            }
            ans += dp[1][((1 << l) - 1) ^ mask[0] ^ mask[2]];
        }
        cout << ans << '\n';
    }
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:37:18: warning: statement has no effect [-Wunused-value]
   37 | #define wis(...) 0
      |                  ^
snake_escaping.cpp:77:9: note: in expansion of macro 'wis'
   77 |         wis(n[0], n[1], n[2], mask[0], mask[1], mask[2]);
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 182 ms 4316 KB Output is correct
12 Correct 224 ms 3932 KB Output is correct
13 Correct 194 ms 3136 KB Output is correct
14 Correct 221 ms 3232 KB Output is correct
15 Correct 202 ms 4268 KB Output is correct
16 Correct 222 ms 3620 KB Output is correct
17 Correct 226 ms 3488 KB Output is correct
18 Correct 162 ms 5324 KB Output is correct
19 Correct 168 ms 2284 KB Output is correct
20 Correct 199 ms 4044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 182 ms 4316 KB Output is correct
12 Correct 224 ms 3932 KB Output is correct
13 Correct 194 ms 3136 KB Output is correct
14 Correct 221 ms 3232 KB Output is correct
15 Correct 202 ms 4268 KB Output is correct
16 Correct 222 ms 3620 KB Output is correct
17 Correct 226 ms 3488 KB Output is correct
18 Correct 162 ms 5324 KB Output is correct
19 Correct 168 ms 2284 KB Output is correct
20 Correct 199 ms 4044 KB Output is correct
21 Correct 232 ms 4320 KB Output is correct
22 Correct 231 ms 4532 KB Output is correct
23 Correct 226 ms 3500 KB Output is correct
24 Correct 224 ms 3348 KB Output is correct
25 Correct 220 ms 5328 KB Output is correct
26 Correct 268 ms 3952 KB Output is correct
27 Correct 236 ms 3812 KB Output is correct
28 Correct 196 ms 6408 KB Output is correct
29 Correct 165 ms 2384 KB Output is correct
30 Correct 227 ms 4480 KB Output is correct
31 Correct 237 ms 4384 KB Output is correct
32 Correct 231 ms 4328 KB Output is correct
33 Correct 220 ms 3304 KB Output is correct
34 Correct 246 ms 3368 KB Output is correct
35 Correct 234 ms 3932 KB Output is correct
36 Correct 182 ms 2312 KB Output is correct
37 Correct 204 ms 4460 KB Output is correct
38 Correct 197 ms 2328 KB Output is correct
39 Correct 208 ms 3564 KB Output is correct
40 Correct 240 ms 3416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 43 ms 9820 KB Output is correct
12 Correct 47 ms 9824 KB Output is correct
13 Correct 46 ms 9824 KB Output is correct
14 Correct 54 ms 9804 KB Output is correct
15 Correct 39 ms 9932 KB Output is correct
16 Correct 50 ms 9812 KB Output is correct
17 Correct 50 ms 9848 KB Output is correct
18 Correct 33 ms 9964 KB Output is correct
19 Correct 51 ms 9672 KB Output is correct
20 Correct 47 ms 9928 KB Output is correct
21 Correct 52 ms 9892 KB Output is correct
22 Correct 45 ms 9828 KB Output is correct
23 Correct 40 ms 9708 KB Output is correct
24 Correct 44 ms 9808 KB Output is correct
25 Correct 56 ms 9800 KB Output is correct
26 Correct 37 ms 9684 KB Output is correct
27 Correct 37 ms 9832 KB Output is correct
28 Correct 34 ms 9692 KB Output is correct
29 Correct 38 ms 9804 KB Output is correct
30 Correct 43 ms 9848 KB Output is correct
31 Correct 38 ms 9824 KB Output is correct
32 Correct 72 ms 9832 KB Output is correct
33 Correct 55 ms 9820 KB Output is correct
34 Correct 37 ms 9724 KB Output is correct
35 Correct 43 ms 9768 KB Output is correct
36 Correct 44 ms 9848 KB Output is correct
37 Correct 44 ms 9836 KB Output is correct
38 Correct 64 ms 9760 KB Output is correct
39 Correct 51 ms 9808 KB Output is correct
40 Correct 44 ms 9852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 182 ms 4316 KB Output is correct
12 Correct 224 ms 3932 KB Output is correct
13 Correct 194 ms 3136 KB Output is correct
14 Correct 221 ms 3232 KB Output is correct
15 Correct 202 ms 4268 KB Output is correct
16 Correct 222 ms 3620 KB Output is correct
17 Correct 226 ms 3488 KB Output is correct
18 Correct 162 ms 5324 KB Output is correct
19 Correct 168 ms 2284 KB Output is correct
20 Correct 199 ms 4044 KB Output is correct
21 Correct 232 ms 4320 KB Output is correct
22 Correct 231 ms 4532 KB Output is correct
23 Correct 226 ms 3500 KB Output is correct
24 Correct 224 ms 3348 KB Output is correct
25 Correct 220 ms 5328 KB Output is correct
26 Correct 268 ms 3952 KB Output is correct
27 Correct 236 ms 3812 KB Output is correct
28 Correct 196 ms 6408 KB Output is correct
29 Correct 165 ms 2384 KB Output is correct
30 Correct 227 ms 4480 KB Output is correct
31 Correct 237 ms 4384 KB Output is correct
32 Correct 231 ms 4328 KB Output is correct
33 Correct 220 ms 3304 KB Output is correct
34 Correct 246 ms 3368 KB Output is correct
35 Correct 234 ms 3932 KB Output is correct
36 Correct 182 ms 2312 KB Output is correct
37 Correct 204 ms 4460 KB Output is correct
38 Correct 197 ms 2328 KB Output is correct
39 Correct 208 ms 3564 KB Output is correct
40 Correct 240 ms 3416 KB Output is correct
41 Correct 43 ms 9820 KB Output is correct
42 Correct 47 ms 9824 KB Output is correct
43 Correct 46 ms 9824 KB Output is correct
44 Correct 54 ms 9804 KB Output is correct
45 Correct 39 ms 9932 KB Output is correct
46 Correct 50 ms 9812 KB Output is correct
47 Correct 50 ms 9848 KB Output is correct
48 Correct 33 ms 9964 KB Output is correct
49 Correct 51 ms 9672 KB Output is correct
50 Correct 47 ms 9928 KB Output is correct
51 Correct 52 ms 9892 KB Output is correct
52 Correct 45 ms 9828 KB Output is correct
53 Correct 40 ms 9708 KB Output is correct
54 Correct 44 ms 9808 KB Output is correct
55 Correct 56 ms 9800 KB Output is correct
56 Correct 37 ms 9684 KB Output is correct
57 Correct 37 ms 9832 KB Output is correct
58 Correct 34 ms 9692 KB Output is correct
59 Correct 38 ms 9804 KB Output is correct
60 Correct 43 ms 9848 KB Output is correct
61 Correct 38 ms 9824 KB Output is correct
62 Correct 72 ms 9832 KB Output is correct
63 Correct 55 ms 9820 KB Output is correct
64 Correct 37 ms 9724 KB Output is correct
65 Correct 43 ms 9768 KB Output is correct
66 Correct 44 ms 9848 KB Output is correct
67 Correct 44 ms 9836 KB Output is correct
68 Correct 64 ms 9760 KB Output is correct
69 Correct 51 ms 9808 KB Output is correct
70 Correct 44 ms 9852 KB Output is correct
71 Correct 320 ms 14768 KB Output is correct
72 Correct 384 ms 14800 KB Output is correct
73 Correct 443 ms 13264 KB Output is correct
74 Correct 526 ms 13688 KB Output is correct
75 Correct 335 ms 15624 KB Output is correct
76 Correct 578 ms 14012 KB Output is correct
77 Correct 530 ms 13812 KB Output is correct
78 Correct 264 ms 17712 KB Output is correct
79 Correct 303 ms 11632 KB Output is correct
80 Correct 387 ms 14780 KB Output is correct
81 Correct 419 ms 14636 KB Output is correct
82 Correct 467 ms 13640 KB Output is correct
83 Correct 339 ms 12776 KB Output is correct
84 Correct 518 ms 13644 KB Output is correct
85 Correct 516 ms 13872 KB Output is correct
86 Correct 248 ms 11628 KB Output is correct
87 Correct 387 ms 14680 KB Output is correct
88 Correct 280 ms 11632 KB Output is correct
89 Correct 364 ms 13316 KB Output is correct
90 Correct 466 ms 13768 KB Output is correct
91 Correct 307 ms 12860 KB Output is correct
92 Correct 578 ms 14044 KB Output is correct
93 Correct 528 ms 13876 KB Output is correct
94 Correct 249 ms 11628 KB Output is correct
95 Correct 525 ms 13708 KB Output is correct
96 Correct 503 ms 13756 KB Output is correct
97 Correct 505 ms 13784 KB Output is correct
98 Correct 499 ms 13804 KB Output is correct
99 Correct 482 ms 13688 KB Output is correct
100 Correct 496 ms 13888 KB Output is correct