Submission #533938

# Submission time Handle Problem Language Result Execution time Memory
533938 2022-03-07T16:19:50 Z radal Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 50992 KB
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 2e6+20,mod = 1e9+7,inf = 1e9+10,maxm = 1e6;
inline int mkay(int a,int b){
    if (a+b >= mod) return a+b-mod;
    if (a+b < 0) return a+b+mod;
    return a+b;
}
 
inline int poww(int a,int k){
    if (k < 0) return 0;
    int z = 1;
    while (k){
        if (k&1) z = 1ll*z*a%mod;
        a = 1ll*a*a%mod;
        k >>= 1;
    }
    return z;
}
int sos[N][2];
map<ll,int> mp;
ll pw[40];
int main(){
    ios :: sync_with_stdio(0); cin.tie(0);
    int l,q;
    cin >> l >> q;
    pw[0] = 1;
    rep(i,1,l) pw[i] = 3*pw[i-1];
    string s;
    cin >> s;
    int n = s.size();
    rep(i,0,n)
        sos[i][1] = sos[i][0] = (s[i]-'0');
    repr(j,l-1,0)
        repr(i,n-1,0)
            if (i&(1 << j))
                sos[i][1] += sos[i-(1 << j)][1];
    repr(j,l-1,0){
        rep(i,0,n){
            if ((i&(1 << j)) == 0)
                sos[i][0] += sos[i+(1 << j)][0];
        }
    }
    while (q--){
        string t;
        cin >> t;
        ll jam = 0;
        reverse(t.begin(),t.end());
        int t0 = 0,t2 = 0,t1 = 0,mask = 0;
        vector<int> ve;
        rep(i,0,l){
            if (t[i] == '1'){
                t1++;
                jam += pw[i];
                mask += (1 << i);
            }
            else if (t[i] == '0') t0++;
            else{
                t2++;
                jam += 2*pw[i];
            }
        }
        if (mp.find(jam) != mp.end()){
            cout << mp[jam] << endl;
            continue;
        }
        if (t2 < 7){
            rep(i,0,l) if (t[i] == '?') ve.pb(i);
            int y = (1 << t2);
            int ans = 0;
            rep(j,0,y){
                int sum = mask;
                rep(k,0,t2)
                    if (j&(1 << k)) 
                        sum += (1 << ve[k]);
                ans += (s[sum]-'0');
            }
            cout << ans << endl;
            if (q > 400000) mp[jam] = ans;
            continue;
        }
        if (t1 <= t0){
            rep(i,0,l){
                if (t[i] == '1') ve.pb(i);
                if (t[i] == '?') mask += (1 << i);
            }
            int y = (1 << t1),ans = 0;
            rep(i,0,y){
                int cnt = 0,sum = mask;
                rep(j,0,t1){
                    if ((1 << j)&i){
                        sum -= (1 << ve[j]);
                        cnt ^= 1;
                    }
                }
                if (cnt) ans -= sos[sum][1];
                else ans += sos[sum][1];
            }
            cout << ans << endl;
            if (q > 400000) mp[jam] = ans;
            continue;
        }
        rep(i,0,l)
            if (t[i] == '0')
                ve.pb(i);
        int y = (1 << t0),ans = 0;
        rep(i,0,y){
            int cnt = 0,sum = mask;
            rep(j,0,t0)
                if ((1 << j)&i){
                    sum += (1 << ve[j]);
                    cnt ^= 1;
                }
            if (cnt) ans -= sos[sum][0];
            else ans += sos[sum][0];
        }
        cout << ans << endl;
        if (q > 400000) mp[jam] = ans;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 233 ms 4292 KB Output is correct
12 Correct 278 ms 3988 KB Output is correct
13 Correct 411 ms 3796 KB Output is correct
14 Correct 379 ms 3524 KB Output is correct
15 Correct 354 ms 4516 KB Output is correct
16 Correct 426 ms 4420 KB Output is correct
17 Correct 545 ms 7360 KB Output is correct
18 Correct 157 ms 5392 KB Output is correct
19 Correct 252 ms 2228 KB Output is correct
20 Correct 301 ms 4052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 233 ms 4292 KB Output is correct
12 Correct 278 ms 3988 KB Output is correct
13 Correct 411 ms 3796 KB Output is correct
14 Correct 379 ms 3524 KB Output is correct
15 Correct 354 ms 4516 KB Output is correct
16 Correct 426 ms 4420 KB Output is correct
17 Correct 545 ms 7360 KB Output is correct
18 Correct 157 ms 5392 KB Output is correct
19 Correct 252 ms 2228 KB Output is correct
20 Correct 301 ms 4052 KB Output is correct
21 Correct 334 ms 4516 KB Output is correct
22 Correct 445 ms 5052 KB Output is correct
23 Correct 921 ms 16372 KB Output is correct
24 Correct 810 ms 9176 KB Output is correct
25 Correct 527 ms 7892 KB Output is correct
26 Correct 1051 ms 22296 KB Output is correct
27 Correct 1439 ms 35144 KB Output is correct
28 Correct 201 ms 6340 KB Output is correct
29 Correct 323 ms 2464 KB Output is correct
30 Correct 410 ms 5008 KB Output is correct
31 Correct 1006 ms 17252 KB Output is correct
32 Correct 706 ms 9988 KB Output is correct
33 Correct 578 ms 5968 KB Output is correct
34 Correct 1124 ms 21872 KB Output is correct
35 Correct 1547 ms 35140 KB Output is correct
36 Correct 170 ms 2392 KB Output is correct
37 Correct 337 ms 4512 KB Output is correct
38 Correct 419 ms 2796 KB Output is correct
39 Correct 944 ms 16424 KB Output is correct
40 Correct 577 ms 8976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 62 ms 9812 KB Output is correct
12 Correct 70 ms 9932 KB Output is correct
13 Correct 83 ms 9832 KB Output is correct
14 Correct 114 ms 9868 KB Output is correct
15 Correct 87 ms 9936 KB Output is correct
16 Correct 108 ms 9832 KB Output is correct
17 Correct 95 ms 9828 KB Output is correct
18 Correct 56 ms 10056 KB Output is correct
19 Correct 57 ms 9716 KB Output is correct
20 Correct 72 ms 9928 KB Output is correct
21 Correct 93 ms 9816 KB Output is correct
22 Correct 117 ms 9804 KB Output is correct
23 Correct 67 ms 9780 KB Output is correct
24 Correct 94 ms 9796 KB Output is correct
25 Correct 104 ms 9792 KB Output is correct
26 Correct 54 ms 9708 KB Output is correct
27 Correct 64 ms 9816 KB Output is correct
28 Correct 62 ms 9692 KB Output is correct
29 Correct 76 ms 9800 KB Output is correct
30 Correct 95 ms 9828 KB Output is correct
31 Correct 76 ms 9828 KB Output is correct
32 Correct 141 ms 9820 KB Output is correct
33 Correct 107 ms 9836 KB Output is correct
34 Correct 51 ms 9684 KB Output is correct
35 Correct 86 ms 9804 KB Output is correct
36 Correct 98 ms 9948 KB Output is correct
37 Correct 113 ms 9816 KB Output is correct
38 Correct 80 ms 9816 KB Output is correct
39 Correct 82 ms 9812 KB Output is correct
40 Correct 88 ms 9800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 233 ms 4292 KB Output is correct
12 Correct 278 ms 3988 KB Output is correct
13 Correct 411 ms 3796 KB Output is correct
14 Correct 379 ms 3524 KB Output is correct
15 Correct 354 ms 4516 KB Output is correct
16 Correct 426 ms 4420 KB Output is correct
17 Correct 545 ms 7360 KB Output is correct
18 Correct 157 ms 5392 KB Output is correct
19 Correct 252 ms 2228 KB Output is correct
20 Correct 301 ms 4052 KB Output is correct
21 Correct 334 ms 4516 KB Output is correct
22 Correct 445 ms 5052 KB Output is correct
23 Correct 921 ms 16372 KB Output is correct
24 Correct 810 ms 9176 KB Output is correct
25 Correct 527 ms 7892 KB Output is correct
26 Correct 1051 ms 22296 KB Output is correct
27 Correct 1439 ms 35144 KB Output is correct
28 Correct 201 ms 6340 KB Output is correct
29 Correct 323 ms 2464 KB Output is correct
30 Correct 410 ms 5008 KB Output is correct
31 Correct 1006 ms 17252 KB Output is correct
32 Correct 706 ms 9988 KB Output is correct
33 Correct 578 ms 5968 KB Output is correct
34 Correct 1124 ms 21872 KB Output is correct
35 Correct 1547 ms 35140 KB Output is correct
36 Correct 170 ms 2392 KB Output is correct
37 Correct 337 ms 4512 KB Output is correct
38 Correct 419 ms 2796 KB Output is correct
39 Correct 944 ms 16424 KB Output is correct
40 Correct 577 ms 8976 KB Output is correct
41 Correct 62 ms 9812 KB Output is correct
42 Correct 70 ms 9932 KB Output is correct
43 Correct 83 ms 9832 KB Output is correct
44 Correct 114 ms 9868 KB Output is correct
45 Correct 87 ms 9936 KB Output is correct
46 Correct 108 ms 9832 KB Output is correct
47 Correct 95 ms 9828 KB Output is correct
48 Correct 56 ms 10056 KB Output is correct
49 Correct 57 ms 9716 KB Output is correct
50 Correct 72 ms 9928 KB Output is correct
51 Correct 93 ms 9816 KB Output is correct
52 Correct 117 ms 9804 KB Output is correct
53 Correct 67 ms 9780 KB Output is correct
54 Correct 94 ms 9796 KB Output is correct
55 Correct 104 ms 9792 KB Output is correct
56 Correct 54 ms 9708 KB Output is correct
57 Correct 64 ms 9816 KB Output is correct
58 Correct 62 ms 9692 KB Output is correct
59 Correct 76 ms 9800 KB Output is correct
60 Correct 95 ms 9828 KB Output is correct
61 Correct 76 ms 9828 KB Output is correct
62 Correct 141 ms 9820 KB Output is correct
63 Correct 107 ms 9836 KB Output is correct
64 Correct 51 ms 9684 KB Output is correct
65 Correct 86 ms 9804 KB Output is correct
66 Correct 98 ms 9948 KB Output is correct
67 Correct 113 ms 9816 KB Output is correct
68 Correct 80 ms 9816 KB Output is correct
69 Correct 82 ms 9812 KB Output is correct
70 Correct 88 ms 9800 KB Output is correct
71 Correct 1004 ms 25772 KB Output is correct
72 Correct 1390 ms 43396 KB Output is correct
73 Correct 1834 ms 50992 KB Output is correct
74 Execution timed out 2049 ms 49596 KB Time limit exceeded
75 Halted 0 ms 0 KB -