Submission #857785

# Submission time Handle Problem Language Result Execution time Memory
857785 2023-10-07T00:37:37 Z TS_2392 Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
788 ms 58752 KB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
// using namespace __gnu_pbds;

#define fileIO(name)  if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
#define SPEED         ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define EL            cout << '\n'
#define dbg(x)        cout << #x << " = " << (x) << ' '
#define dbgp(x)       cout << #x << " = (" << (x.fi) << ", " << (x.se) << ") "
#define dbga(x, l, r) {cout << #x << '[' << l << ", " << r << "] = { "; for(int i = l; i <= (int)r; ++i) cout << x[i] << ' '; cout << "} ";}

#define epl           emplace
#define pb            push_back
#define eb            emplace_back
#define fi            first
#define se            second
#define mp            make_pair

#define sqr(x)        ((x) * (x))
#define all(x)        (x).begin(), (x).end()
#define rall(x)       (x).rbegin(), (x).rend()
#define lwb           lower_bound
#define upb           upper_bound

#define clz           __builtin_clzll // or __builtin_clz
#define ctz           __builtin_ctzll // or __builtin_ctz
#define pct           __builtin_popcountll // or __builtin_popcount

typedef long long            ll;
typedef long double          ldb;
typedef unsigned int         uint;
typedef unsigned long long   ull;
typedef pair<ll, ll>         pll;
typedef pair<ll, int>        pli;
typedef pair<int, ll>        pil;
typedef pair<int, int>       pii;
typedef pair<ldb, ldb>       pld;
typedef pair<double, double> pdd;

template<class T1, class T2> bool minimize(T1 &a, T2 b){return a > b ? a = b, true : false;}
template<class T1, class T2> bool maximize(T1 &a, T2 b){return a < b ? a = b, true : false;}
template<class T> void remDup(vector<T> &v){sort(all(v)); v.erase(unique(all(v)),v.end());}

// int d4x[4] = {1, 0, -1, 0}, d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
// int d4y[4] = {0, 1, 0, -1}, d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};

// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
const int N = 20, Q = 1e6 + 3;
int n, q, sz[1 << N];
ll dp1[1 << N], dp2[1 << N], a[1 << N];
void TS_2392(){
    cin >> n >> q;
    for(int i = 0; i < (1 << n); ++i){
        char c; cin >> c;
        sz[i] = pct(i);
        dp1[i] = dp2[i] = a[i] = c - '0';
    }
    for(int i = 0; i < n; ++i){
        for(int mask = 0; mask < 1 << n; ++ mask){
            if(mask >> i & 1){
                dp1[mask] += dp1[mask ^ (1 << i)];
            }
            else{
                dp2[mask] += dp2[mask ^ (1 << i)];
            }
        }
    }
    while(q--){
        string s; cin >> s; reverse(all(s));
        int cnt0 = 0, cnt1 = 0, cnt_qm = 0;
        for(int i = 0; i < n; ++i){
            if(s[i] == '0') ++cnt0;
            if(s[i] == '1') ++cnt1;
            if(s[i] == '?') ++cnt_qm;
        }
        //dbg(s); EL;
        if(cnt0 <= 6){
            int mask1 = 0, mask2 = 0;
            for(int i = 0; i < n; ++i){
                if(s[i] == '1') mask1 ^= 1 << i;
                if(s[i] == '0') mask2 ^= 1 << i;
            }
            //dbg(bitset<4>(mask1)); EL;
            //dbg(bitset<4>(mask2)); EL;
            ll res = 0;
            for(int x = mask2, i = 1; i <= (1 << sz[mask2]); x = (x - 1) & mask2, ++i){
                int cur = (x ^ mask2);
                //dbg(mask1 ^ cur); dbg(dp2[mask1 ^ cur]); EL;
                if(sz[cur] & 1) res -= dp2[mask1 ^ cur];
                else res += dp2[mask1 ^ cur];
            }
            cout << res << '\n';
        }
        else if(cnt1 <= 6){
            int mask1 = 0, mask2 = 0;
            for(int i = 0; i < n; ++i){
                if(s[i] == '?') mask1 ^= 1 << i;
                if(s[i] == '1') mask2 ^= 1 << i, mask1 ^= 1 << i;
            }
            ll res = 0;
            for(int x = mask2, i = 1; i <= (1 << sz[mask2]); x = (x - 1) & mask2, ++i){
                int cur = x;
                if(sz[mask2 ^ cur] & 1) res -= dp1[mask1 ^ mask2 ^ cur];
                else res += dp1[mask1 ^ mask2 ^ cur];
            }
            cout << res << '\n';
        }
        else{
            int mask1 = 0, mask2 = 0;
            for(int i = 0; i < n; ++i){
                if(s[i] == '1') mask1 ^= 1 << i;
                if(s[i] == '?') mask2 ^= 1 << i;
            }
            ll res = 0;
            for(int x = mask2, i = 1; i <= (1 << sz[mask2]); x = (x - 1) & mask2, ++i){
                res += a[mask1 ^ x];
            }
            cout << res << '\n';
        }
    }
}
int main(){
    SPEED; fileIO("text");
    int numtest = 1; //cin >> numtest;
    for(int itest = 1; itest <= numtest; ++itest){
        // cout << "Case #" << itest << ": ";
        TS_2392();
    }
    return 0;
}
/* stuff you should look for
 * keep calm, don't panic
 * int overflow, array bounds
 * small cases, special cases (n = 1, 2 x 2 matrix, tree with n - 1 leafs, ...)
 * GUESS, fix something, lower bound or upper bound for the problem
 * do something instead of nothing: code bruteforce, ...
 * WRITE STUFF DOWN and STAY ORGANIZED
 * DON'T GET STUCK ON ONE APPROACH
 */

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:8:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 | #define fileIO(name)  if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:127:12: note: in expansion of macro 'fileIO'
  127 |     SPEED; fileIO("text");
      |            ^~~~~~
snake_escaping.cpp:8:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 | #define fileIO(name)  if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:127:12: note: in expansion of macro 'fileIO'
  127 |     SPEED; fileIO("text");
      |            ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 174 ms 21332 KB Output is correct
12 Correct 224 ms 21240 KB Output is correct
13 Correct 241 ms 20316 KB Output is correct
14 Correct 209 ms 20272 KB Output is correct
15 Correct 181 ms 21296 KB Output is correct
16 Correct 220 ms 20560 KB Output is correct
17 Correct 227 ms 20464 KB Output is correct
18 Correct 146 ms 22160 KB Output is correct
19 Correct 212 ms 19280 KB Output is correct
20 Correct 183 ms 20824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 174 ms 21332 KB Output is correct
12 Correct 224 ms 21240 KB Output is correct
13 Correct 241 ms 20316 KB Output is correct
14 Correct 209 ms 20272 KB Output is correct
15 Correct 181 ms 21296 KB Output is correct
16 Correct 220 ms 20560 KB Output is correct
17 Correct 227 ms 20464 KB Output is correct
18 Correct 146 ms 22160 KB Output is correct
19 Correct 212 ms 19280 KB Output is correct
20 Correct 183 ms 20824 KB Output is correct
21 Correct 196 ms 24284 KB Output is correct
22 Correct 261 ms 24404 KB Output is correct
23 Correct 326 ms 23372 KB Output is correct
24 Correct 268 ms 23312 KB Output is correct
25 Correct 216 ms 25348 KB Output is correct
26 Correct 257 ms 23892 KB Output is correct
27 Correct 281 ms 23636 KB Output is correct
28 Correct 156 ms 26192 KB Output is correct
29 Correct 316 ms 22232 KB Output is correct
30 Correct 205 ms 24460 KB Output is correct
31 Correct 245 ms 24376 KB Output is correct
32 Correct 327 ms 24380 KB Output is correct
33 Correct 228 ms 23084 KB Output is correct
34 Correct 286 ms 23632 KB Output is correct
35 Correct 299 ms 23632 KB Output is correct
36 Correct 150 ms 22220 KB Output is correct
37 Correct 235 ms 24152 KB Output is correct
38 Correct 318 ms 22420 KB Output is correct
39 Correct 265 ms 23408 KB Output is correct
40 Correct 251 ms 23288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 47 ms 31316 KB Output is correct
12 Correct 50 ms 31364 KB Output is correct
13 Correct 62 ms 31312 KB Output is correct
14 Correct 58 ms 31352 KB Output is correct
15 Correct 51 ms 31312 KB Output is correct
16 Correct 60 ms 31384 KB Output is correct
17 Correct 68 ms 31316 KB Output is correct
18 Correct 43 ms 31568 KB Output is correct
19 Correct 52 ms 31120 KB Output is correct
20 Correct 47 ms 31320 KB Output is correct
21 Correct 54 ms 31312 KB Output is correct
22 Correct 57 ms 31196 KB Output is correct
23 Correct 49 ms 31320 KB Output is correct
24 Correct 70 ms 31396 KB Output is correct
25 Correct 75 ms 31416 KB Output is correct
26 Correct 41 ms 31196 KB Output is correct
27 Correct 50 ms 31468 KB Output is correct
28 Correct 58 ms 31116 KB Output is correct
29 Correct 52 ms 31312 KB Output is correct
30 Correct 58 ms 31416 KB Output is correct
31 Correct 50 ms 31232 KB Output is correct
32 Correct 68 ms 31212 KB Output is correct
33 Correct 66 ms 31316 KB Output is correct
34 Correct 40 ms 31312 KB Output is correct
35 Correct 59 ms 31260 KB Output is correct
36 Correct 61 ms 31200 KB Output is correct
37 Correct 59 ms 31236 KB Output is correct
38 Correct 64 ms 31200 KB Output is correct
39 Correct 61 ms 31196 KB Output is correct
40 Correct 63 ms 31316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 174 ms 21332 KB Output is correct
12 Correct 224 ms 21240 KB Output is correct
13 Correct 241 ms 20316 KB Output is correct
14 Correct 209 ms 20272 KB Output is correct
15 Correct 181 ms 21296 KB Output is correct
16 Correct 220 ms 20560 KB Output is correct
17 Correct 227 ms 20464 KB Output is correct
18 Correct 146 ms 22160 KB Output is correct
19 Correct 212 ms 19280 KB Output is correct
20 Correct 183 ms 20824 KB Output is correct
21 Correct 196 ms 24284 KB Output is correct
22 Correct 261 ms 24404 KB Output is correct
23 Correct 326 ms 23372 KB Output is correct
24 Correct 268 ms 23312 KB Output is correct
25 Correct 216 ms 25348 KB Output is correct
26 Correct 257 ms 23892 KB Output is correct
27 Correct 281 ms 23636 KB Output is correct
28 Correct 156 ms 26192 KB Output is correct
29 Correct 316 ms 22232 KB Output is correct
30 Correct 205 ms 24460 KB Output is correct
31 Correct 245 ms 24376 KB Output is correct
32 Correct 327 ms 24380 KB Output is correct
33 Correct 228 ms 23084 KB Output is correct
34 Correct 286 ms 23632 KB Output is correct
35 Correct 299 ms 23632 KB Output is correct
36 Correct 150 ms 22220 KB Output is correct
37 Correct 235 ms 24152 KB Output is correct
38 Correct 318 ms 22420 KB Output is correct
39 Correct 265 ms 23408 KB Output is correct
40 Correct 251 ms 23288 KB Output is correct
41 Correct 47 ms 31316 KB Output is correct
42 Correct 50 ms 31364 KB Output is correct
43 Correct 62 ms 31312 KB Output is correct
44 Correct 58 ms 31352 KB Output is correct
45 Correct 51 ms 31312 KB Output is correct
46 Correct 60 ms 31384 KB Output is correct
47 Correct 68 ms 31316 KB Output is correct
48 Correct 43 ms 31568 KB Output is correct
49 Correct 52 ms 31120 KB Output is correct
50 Correct 47 ms 31320 KB Output is correct
51 Correct 54 ms 31312 KB Output is correct
52 Correct 57 ms 31196 KB Output is correct
53 Correct 49 ms 31320 KB Output is correct
54 Correct 70 ms 31396 KB Output is correct
55 Correct 75 ms 31416 KB Output is correct
56 Correct 41 ms 31196 KB Output is correct
57 Correct 50 ms 31468 KB Output is correct
58 Correct 58 ms 31116 KB Output is correct
59 Correct 52 ms 31312 KB Output is correct
60 Correct 58 ms 31416 KB Output is correct
61 Correct 50 ms 31232 KB Output is correct
62 Correct 68 ms 31212 KB Output is correct
63 Correct 66 ms 31316 KB Output is correct
64 Correct 40 ms 31312 KB Output is correct
65 Correct 59 ms 31260 KB Output is correct
66 Correct 61 ms 31200 KB Output is correct
67 Correct 59 ms 31236 KB Output is correct
68 Correct 64 ms 31200 KB Output is correct
69 Correct 61 ms 31196 KB Output is correct
70 Correct 63 ms 31316 KB Output is correct
71 Correct 319 ms 55636 KB Output is correct
72 Correct 406 ms 55860 KB Output is correct
73 Correct 485 ms 54768 KB Output is correct
74 Correct 546 ms 54672 KB Output is correct
75 Correct 338 ms 56656 KB Output is correct
76 Correct 495 ms 54908 KB Output is correct
77 Correct 747 ms 54804 KB Output is correct
78 Correct 243 ms 58752 KB Output is correct
79 Correct 400 ms 53224 KB Output is correct
80 Correct 328 ms 55820 KB Output is correct
81 Correct 429 ms 55604 KB Output is correct
82 Correct 489 ms 54740 KB Output is correct
83 Correct 365 ms 53844 KB Output is correct
84 Correct 788 ms 54636 KB Output is correct
85 Correct 690 ms 54784 KB Output is correct
86 Correct 213 ms 52680 KB Output is correct
87 Correct 380 ms 55632 KB Output is correct
88 Correct 416 ms 52564 KB Output is correct
89 Correct 450 ms 54284 KB Output is correct
90 Correct 460 ms 55176 KB Output is correct
91 Correct 395 ms 53820 KB Output is correct
92 Correct 690 ms 55072 KB Output is correct
93 Correct 703 ms 55072 KB Output is correct
94 Correct 225 ms 52548 KB Output is correct
95 Correct 595 ms 54864 KB Output is correct
96 Correct 590 ms 54612 KB Output is correct
97 Correct 587 ms 54852 KB Output is correct
98 Correct 582 ms 54612 KB Output is correct
99 Correct 612 ms 54628 KB Output is correct
100 Correct 594 ms 54736 KB Output is correct