Submission #991635

# Submission time Handle Problem Language Result Execution time Memory
991635 2024-06-02T17:24:39 Z GrindMachine Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1781 ms 48664 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
 
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
 
template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}
 
template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}
 
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
 
/*
 
 
 
*/
 
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
 
int8_t ok_mem[6561][256];

void solve(int test_case)
{
    int n,q; cin >> n >> q;
    string s; cin >> s;
    int B = max(n/2,1);
    amin(B,8);

    vector<int> pow3(n+5);
    pow3[0] = 1;
    rep1(i,n) pow3[i] = pow3[i-1]*3;
 
    vector<pii> queries(q);
    rep(id,q){
        string t; cin >> t;
        int first_t = 0, last_t = 0;
        rep(i,sz(t)){
            ll x = 0;
            if(t[i] == '0') x = 0;
            if(t[i] == '1') x = 1;
            if(t[i] == '?') x = 2;
            if(i < B) first_t = first_t*3+x;
            else last_t = last_t*3+x;
        }
 
        queries[id] = {first_t,last_t};
    }
 
    vector<int> get_mask_mem(1<<(n-B),-2);
 
    auto get_mask = [&](ll x){
        if(get_mask_mem[x] != -2) return get_mask_mem[x];
 
        int mask = 0;
        rep(bit,n-B){
            int b = 0;
            if(x&(1<<bit)) b = 1;
            mask += pow3[bit]*b;
        }
 
        return get_mask_mem[x] = mask;
    };
 
    vector<int> get_lsq_mem(pow3[n-B],-2);
 
    auto get_lsq = [&](ll x){
        if(get_lsq_mem[x] != -2) return get_lsq_mem[x];
 
        int ox = x;
        int pos = 0;
        while(x){
            int b = x%3;
            x /= 3;
            if(b == 2) return get_lsq_mem[ox] = pos;
            pos++; 
        }
    
        return get_lsq_mem[ox] = -1;        
    };
 
    memset(ok_mem,-1,sizeof ok_mem);
 
    auto ok = [&](int mask1, int mask2){
        if(ok_mem[mask1][mask2] != -1) return ok_mem[mask1][mask2];
        auto omask1 = mask1, omask2 = mask2;
 
        rev(b,B-1,0){
            int b1 = 0, b2 = 0;
            b1 = mask1%3;
            mask1 /= 3;
            if(mask2&(1<<(B-b-1))) b2 = 1;
 
            if(b1 == 2 or b1 == b2){
 
            }
            else{
                return ok_mem[omask1][omask2] = false;
            }
        }
 
        return ok_mem[omask1][omask2] = true;
    };
 
    vector<int> ans(q);
    vector<int> dp(pow3[n-B]);
 
    rep(mask1,1<<B){
        fill(all(dp),0);
 
        rep(i,1<<n){
            int firstB = i>>(n-B);
            int lastB = i^(firstB<<(n-B));
            if(firstB != mask1) conts;
            dp[get_mask(lastB)] += s[i]-'0';
        }
 
        rep(mask2,pow3[n-B]){
            int lsq = get_lsq(mask2);
            if(lsq == -1) conts;
            rep(x,2){
                int mask3 = mask2-pow3[lsq]*2+pow3[lsq]*x;
                dp[mask2] += dp[mask3];
            }
        }
 
        rep(id,q){
            if(ok(queries[id].ff,mask1)){
                ans[id] += dp[queries[id].ss];
            }
        }
    }
 
    rep(i,q) cout << ans[i] << endl;
}
 
int main()
{
    fastio;
 
    int t = 1;
    // cin >> t;
 
    rep1(i, t) {
        solve(i);
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 251 ms 17772 KB Output is correct
12 Correct 213 ms 17492 KB Output is correct
13 Correct 206 ms 16720 KB Output is correct
14 Correct 210 ms 16720 KB Output is correct
15 Correct 260 ms 17756 KB Output is correct
16 Correct 235 ms 16984 KB Output is correct
17 Correct 282 ms 16976 KB Output is correct
18 Correct 173 ms 18768 KB Output is correct
19 Correct 188 ms 15724 KB Output is correct
20 Correct 264 ms 17492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 251 ms 17772 KB Output is correct
12 Correct 213 ms 17492 KB Output is correct
13 Correct 206 ms 16720 KB Output is correct
14 Correct 210 ms 16720 KB Output is correct
15 Correct 260 ms 17756 KB Output is correct
16 Correct 235 ms 16984 KB Output is correct
17 Correct 282 ms 16976 KB Output is correct
18 Correct 173 ms 18768 KB Output is correct
19 Correct 188 ms 15724 KB Output is correct
20 Correct 264 ms 17492 KB Output is correct
21 Correct 355 ms 17748 KB Output is correct
22 Correct 323 ms 18040 KB Output is correct
23 Correct 301 ms 16980 KB Output is correct
24 Correct 309 ms 16724 KB Output is correct
25 Correct 414 ms 18772 KB Output is correct
26 Correct 326 ms 17300 KB Output is correct
27 Correct 325 ms 17232 KB Output is correct
28 Correct 250 ms 19792 KB Output is correct
29 Correct 275 ms 15700 KB Output is correct
30 Correct 414 ms 18004 KB Output is correct
31 Correct 339 ms 17748 KB Output is correct
32 Correct 353 ms 17748 KB Output is correct
33 Correct 329 ms 16764 KB Output is correct
34 Correct 330 ms 16764 KB Output is correct
35 Correct 332 ms 17280 KB Output is correct
36 Correct 291 ms 15728 KB Output is correct
37 Correct 283 ms 17748 KB Output is correct
38 Correct 272 ms 15724 KB Output is correct
39 Correct 313 ms 16984 KB Output is correct
40 Correct 304 ms 16720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 491 ms 7920 KB Output is correct
12 Correct 484 ms 7920 KB Output is correct
13 Correct 511 ms 7920 KB Output is correct
14 Correct 578 ms 7924 KB Output is correct
15 Correct 538 ms 8180 KB Output is correct
16 Correct 544 ms 7920 KB Output is correct
17 Correct 526 ms 7932 KB Output is correct
18 Correct 494 ms 8180 KB Output is correct
19 Correct 480 ms 7880 KB Output is correct
20 Correct 487 ms 7924 KB Output is correct
21 Correct 527 ms 7884 KB Output is correct
22 Correct 533 ms 7884 KB Output is correct
23 Correct 538 ms 7880 KB Output is correct
24 Correct 537 ms 7888 KB Output is correct
25 Correct 525 ms 7924 KB Output is correct
26 Correct 466 ms 7924 KB Output is correct
27 Correct 479 ms 7920 KB Output is correct
28 Correct 467 ms 7920 KB Output is correct
29 Correct 572 ms 7900 KB Output is correct
30 Correct 530 ms 7884 KB Output is correct
31 Correct 519 ms 7920 KB Output is correct
32 Correct 535 ms 7924 KB Output is correct
33 Correct 560 ms 7884 KB Output is correct
34 Correct 463 ms 7924 KB Output is correct
35 Correct 534 ms 7924 KB Output is correct
36 Correct 549 ms 7884 KB Output is correct
37 Correct 540 ms 7884 KB Output is correct
38 Correct 546 ms 7924 KB Output is correct
39 Correct 558 ms 7888 KB Output is correct
40 Correct 547 ms 7924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 251 ms 17772 KB Output is correct
12 Correct 213 ms 17492 KB Output is correct
13 Correct 206 ms 16720 KB Output is correct
14 Correct 210 ms 16720 KB Output is correct
15 Correct 260 ms 17756 KB Output is correct
16 Correct 235 ms 16984 KB Output is correct
17 Correct 282 ms 16976 KB Output is correct
18 Correct 173 ms 18768 KB Output is correct
19 Correct 188 ms 15724 KB Output is correct
20 Correct 264 ms 17492 KB Output is correct
21 Correct 355 ms 17748 KB Output is correct
22 Correct 323 ms 18040 KB Output is correct
23 Correct 301 ms 16980 KB Output is correct
24 Correct 309 ms 16724 KB Output is correct
25 Correct 414 ms 18772 KB Output is correct
26 Correct 326 ms 17300 KB Output is correct
27 Correct 325 ms 17232 KB Output is correct
28 Correct 250 ms 19792 KB Output is correct
29 Correct 275 ms 15700 KB Output is correct
30 Correct 414 ms 18004 KB Output is correct
31 Correct 339 ms 17748 KB Output is correct
32 Correct 353 ms 17748 KB Output is correct
33 Correct 329 ms 16764 KB Output is correct
34 Correct 330 ms 16764 KB Output is correct
35 Correct 332 ms 17280 KB Output is correct
36 Correct 291 ms 15728 KB Output is correct
37 Correct 283 ms 17748 KB Output is correct
38 Correct 272 ms 15724 KB Output is correct
39 Correct 313 ms 16984 KB Output is correct
40 Correct 304 ms 16720 KB Output is correct
41 Correct 491 ms 7920 KB Output is correct
42 Correct 484 ms 7920 KB Output is correct
43 Correct 511 ms 7920 KB Output is correct
44 Correct 578 ms 7924 KB Output is correct
45 Correct 538 ms 8180 KB Output is correct
46 Correct 544 ms 7920 KB Output is correct
47 Correct 526 ms 7932 KB Output is correct
48 Correct 494 ms 8180 KB Output is correct
49 Correct 480 ms 7880 KB Output is correct
50 Correct 487 ms 7924 KB Output is correct
51 Correct 527 ms 7884 KB Output is correct
52 Correct 533 ms 7884 KB Output is correct
53 Correct 538 ms 7880 KB Output is correct
54 Correct 537 ms 7888 KB Output is correct
55 Correct 525 ms 7924 KB Output is correct
56 Correct 466 ms 7924 KB Output is correct
57 Correct 479 ms 7920 KB Output is correct
58 Correct 467 ms 7920 KB Output is correct
59 Correct 572 ms 7900 KB Output is correct
60 Correct 530 ms 7884 KB Output is correct
61 Correct 519 ms 7920 KB Output is correct
62 Correct 535 ms 7924 KB Output is correct
63 Correct 560 ms 7884 KB Output is correct
64 Correct 463 ms 7924 KB Output is correct
65 Correct 534 ms 7924 KB Output is correct
66 Correct 549 ms 7884 KB Output is correct
67 Correct 540 ms 7884 KB Output is correct
68 Correct 546 ms 7924 KB Output is correct
69 Correct 558 ms 7888 KB Output is correct
70 Correct 547 ms 7924 KB Output is correct
71 Correct 1411 ms 45592 KB Output is correct
72 Correct 1349 ms 45796 KB Output is correct
73 Correct 1461 ms 44312 KB Output is correct
74 Correct 1618 ms 44564 KB Output is correct
75 Correct 1781 ms 46620 KB Output is correct
76 Correct 1547 ms 44828 KB Output is correct
77 Correct 1524 ms 44776 KB Output is correct
78 Correct 1148 ms 48664 KB Output is correct
79 Correct 1122 ms 42540 KB Output is correct
80 Correct 1433 ms 45852 KB Output is correct
81 Correct 1590 ms 45564 KB Output is correct
82 Correct 1490 ms 44576 KB Output is correct
83 Correct 1369 ms 43736 KB Output is correct
84 Correct 1475 ms 44504 KB Output is correct
85 Correct 1567 ms 44888 KB Output is correct
86 Correct 1000 ms 42740 KB Output is correct
87 Correct 1299 ms 45540 KB Output is correct
88 Correct 1149 ms 42740 KB Output is correct
89 Correct 1412 ms 44276 KB Output is correct
90 Correct 1465 ms 44640 KB Output is correct
91 Correct 1238 ms 43764 KB Output is correct
92 Correct 1485 ms 45028 KB Output is correct
93 Correct 1539 ms 44792 KB Output is correct
94 Correct 993 ms 42736 KB Output is correct
95 Correct 1544 ms 44660 KB Output is correct
96 Correct 1548 ms 44828 KB Output is correct
97 Correct 1606 ms 44788 KB Output is correct
98 Correct 1637 ms 44828 KB Output is correct
99 Correct 1653 ms 44828 KB Output is correct
100 Correct 1568 ms 44752 KB Output is correct