Submission #364830

# Submission time Handle Problem Language Result Execution time Memory
364830 2021-02-10T07:52:07 Z balbit Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1208 ms 21740 KB
#include <bits/stdc++.h>
#pragma GCC target ("popcnt")
using namespace std;
#define ll long long
#define y1 zck_is_king
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif

const int iinf = 1e9+10;
const ll inf = 1ll<<60;
const ll mod = 1e9+7 ;


void GG(){cout<<"0\n"; exit(0);}

ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
    ll re=1;
    while (n>0){
        if (n&1) re = re*a %mo;
        a = a*a %mo;
        n>>=1;
    }
    return re;
}

ll inv (ll b, ll mo = mod){
    if (b==1) return b;
    return (mo-mo/b) * inv(mo%b,mo) % mo;
}

const int maxn = 1e5+5;

int A[1<<20|7], B[1<<20|7], F[1<<20|7];
bool poppar[1<<20|7];

signed main(){
    IOS();
    int n, q; cin>>n>>q;
    REP(i, 1<<n) {
        poppar[i] = __builtin_popcount(i) & 1;
    }
    int flip = (1<<n)-1;
    REP(i, 1<<n) {
        char c; cin>>c;
        F[i] = A[i] = B[i^flip] = c-'0';
    }
    REP(i, n) {
        REP(j, 1<<n) {
            if (j & (1<<i)) {
                A[j] += A[j^(1<<i)];
                B[j] += B[j^(1<<i)];
            }
        }
    }
//    bug(A[6]);
    while (q--) {
        string s; cin>>s;
        reverse(ALL(s));
        int a,b,c; a=b=c=0;
        for( char ch : s) {
            if (ch == '?') ++c;
            if (ch == '1') ++a;
            if (ch == '0') ++b;
        }
        int mn = min({a,b,c});
        int re = 0;
        if (mn == c) {
            int msk = 0;
            int base = 0;
            REP(i, n) {
                if (s[i] == '?') msk += 1<<i;
                if (s[i] == '1') base+= 1<<i;
            }
            int org = msk;
            for (;;msk = (msk-1) & (org)) {
                re += F[base | msk];
                if(msk == 0) break;
            }
        }else {
            int *U = min(a,b) == a? A:B;
            if (min(a,b) != a) {
                for (char &ch : s) {
                    if (ch != '?') ch = ch=='1'?'0':'1';
                }
            }
            int msk = 0;
            int base = 0;
            REP(i,n) {
                if (s[i] == '1') msk += 1<<i;
                if (s[i] == '?') base+= 1<<i;
            }
            bug(msk, base, min(a,b) == a);
            int org = msk;
            for (; ; msk = (msk - 1) & org) {
                re += (poppar[msk^org])?-U[msk|base]:U[msk|base];
                if (msk == 0) break;
            }
        }
        cout<<re<<endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 282 ms 5612 KB Output is correct
12 Correct 276 ms 5356 KB Output is correct
13 Correct 294 ms 4708 KB Output is correct
14 Correct 327 ms 4588 KB Output is correct
15 Correct 306 ms 5612 KB Output is correct
16 Correct 328 ms 4716 KB Output is correct
17 Correct 340 ms 4716 KB Output is correct
18 Correct 270 ms 6636 KB Output is correct
19 Correct 243 ms 3692 KB Output is correct
20 Correct 316 ms 5612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 282 ms 5612 KB Output is correct
12 Correct 276 ms 5356 KB Output is correct
13 Correct 294 ms 4708 KB Output is correct
14 Correct 327 ms 4588 KB Output is correct
15 Correct 306 ms 5612 KB Output is correct
16 Correct 328 ms 4716 KB Output is correct
17 Correct 340 ms 4716 KB Output is correct
18 Correct 270 ms 6636 KB Output is correct
19 Correct 243 ms 3692 KB Output is correct
20 Correct 316 ms 5612 KB Output is correct
21 Correct 343 ms 5604 KB Output is correct
22 Correct 321 ms 5824 KB Output is correct
23 Correct 356 ms 4948 KB Output is correct
24 Correct 362 ms 4716 KB Output is correct
25 Correct 396 ms 6892 KB Output is correct
26 Correct 394 ms 5100 KB Output is correct
27 Correct 405 ms 5100 KB Output is correct
28 Correct 252 ms 7788 KB Output is correct
29 Correct 286 ms 3820 KB Output is correct
30 Correct 383 ms 5952 KB Output is correct
31 Correct 404 ms 5740 KB Output is correct
32 Correct 393 ms 5996 KB Output is correct
33 Correct 352 ms 4628 KB Output is correct
34 Correct 398 ms 4588 KB Output is correct
35 Correct 405 ms 5208 KB Output is correct
36 Correct 203 ms 3692 KB Output is correct
37 Correct 304 ms 5868 KB Output is correct
38 Correct 340 ms 3692 KB Output is correct
39 Correct 431 ms 4844 KB Output is correct
40 Correct 383 ms 4844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 78 ms 15084 KB Output is correct
12 Correct 78 ms 15212 KB Output is correct
13 Correct 82 ms 14956 KB Output is correct
14 Correct 97 ms 15084 KB Output is correct
15 Correct 79 ms 15084 KB Output is correct
16 Correct 117 ms 15212 KB Output is correct
17 Correct 99 ms 15084 KB Output is correct
18 Correct 66 ms 15212 KB Output is correct
19 Correct 76 ms 14956 KB Output is correct
20 Correct 82 ms 15084 KB Output is correct
21 Correct 94 ms 15084 KB Output is correct
22 Correct 103 ms 15084 KB Output is correct
23 Correct 79 ms 14956 KB Output is correct
24 Correct 115 ms 15084 KB Output is correct
25 Correct 100 ms 15084 KB Output is correct
26 Correct 65 ms 14956 KB Output is correct
27 Correct 75 ms 15084 KB Output is correct
28 Correct 79 ms 14956 KB Output is correct
29 Correct 86 ms 14956 KB Output is correct
30 Correct 94 ms 15084 KB Output is correct
31 Correct 77 ms 15212 KB Output is correct
32 Correct 109 ms 15084 KB Output is correct
33 Correct 101 ms 15084 KB Output is correct
34 Correct 66 ms 14956 KB Output is correct
35 Correct 92 ms 15084 KB Output is correct
36 Correct 93 ms 15084 KB Output is correct
37 Correct 92 ms 15084 KB Output is correct
38 Correct 92 ms 15084 KB Output is correct
39 Correct 92 ms 15084 KB Output is correct
40 Correct 93 ms 15084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 282 ms 5612 KB Output is correct
12 Correct 276 ms 5356 KB Output is correct
13 Correct 294 ms 4708 KB Output is correct
14 Correct 327 ms 4588 KB Output is correct
15 Correct 306 ms 5612 KB Output is correct
16 Correct 328 ms 4716 KB Output is correct
17 Correct 340 ms 4716 KB Output is correct
18 Correct 270 ms 6636 KB Output is correct
19 Correct 243 ms 3692 KB Output is correct
20 Correct 316 ms 5612 KB Output is correct
21 Correct 343 ms 5604 KB Output is correct
22 Correct 321 ms 5824 KB Output is correct
23 Correct 356 ms 4948 KB Output is correct
24 Correct 362 ms 4716 KB Output is correct
25 Correct 396 ms 6892 KB Output is correct
26 Correct 394 ms 5100 KB Output is correct
27 Correct 405 ms 5100 KB Output is correct
28 Correct 252 ms 7788 KB Output is correct
29 Correct 286 ms 3820 KB Output is correct
30 Correct 383 ms 5952 KB Output is correct
31 Correct 404 ms 5740 KB Output is correct
32 Correct 393 ms 5996 KB Output is correct
33 Correct 352 ms 4628 KB Output is correct
34 Correct 398 ms 4588 KB Output is correct
35 Correct 405 ms 5208 KB Output is correct
36 Correct 203 ms 3692 KB Output is correct
37 Correct 304 ms 5868 KB Output is correct
38 Correct 340 ms 3692 KB Output is correct
39 Correct 431 ms 4844 KB Output is correct
40 Correct 383 ms 4844 KB Output is correct
41 Correct 78 ms 15084 KB Output is correct
42 Correct 78 ms 15212 KB Output is correct
43 Correct 82 ms 14956 KB Output is correct
44 Correct 97 ms 15084 KB Output is correct
45 Correct 79 ms 15084 KB Output is correct
46 Correct 117 ms 15212 KB Output is correct
47 Correct 99 ms 15084 KB Output is correct
48 Correct 66 ms 15212 KB Output is correct
49 Correct 76 ms 14956 KB Output is correct
50 Correct 82 ms 15084 KB Output is correct
51 Correct 94 ms 15084 KB Output is correct
52 Correct 103 ms 15084 KB Output is correct
53 Correct 79 ms 14956 KB Output is correct
54 Correct 115 ms 15084 KB Output is correct
55 Correct 100 ms 15084 KB Output is correct
56 Correct 65 ms 14956 KB Output is correct
57 Correct 75 ms 15084 KB Output is correct
58 Correct 79 ms 14956 KB Output is correct
59 Correct 86 ms 14956 KB Output is correct
60 Correct 94 ms 15084 KB Output is correct
61 Correct 77 ms 15212 KB Output is correct
62 Correct 109 ms 15084 KB Output is correct
63 Correct 101 ms 15084 KB Output is correct
64 Correct 66 ms 14956 KB Output is correct
65 Correct 92 ms 15084 KB Output is correct
66 Correct 93 ms 15084 KB Output is correct
67 Correct 92 ms 15084 KB Output is correct
68 Correct 92 ms 15084 KB Output is correct
69 Correct 92 ms 15084 KB Output is correct
70 Correct 93 ms 15084 KB Output is correct
71 Correct 674 ms 18796 KB Output is correct
72 Correct 564 ms 18976 KB Output is correct
73 Correct 721 ms 17388 KB Output is correct
74 Correct 1012 ms 17848 KB Output is correct
75 Correct 636 ms 19948 KB Output is correct
76 Correct 1208 ms 18248 KB Output is correct
77 Correct 1044 ms 18040 KB Output is correct
78 Correct 417 ms 21740 KB Output is correct
79 Correct 522 ms 15800 KB Output is correct
80 Correct 650 ms 18876 KB Output is correct
81 Correct 837 ms 18832 KB Output is correct
82 Correct 1120 ms 17772 KB Output is correct
83 Correct 583 ms 16968 KB Output is correct
84 Correct 982 ms 18028 KB Output is correct
85 Correct 1004 ms 17944 KB Output is correct
86 Correct 350 ms 15724 KB Output is correct
87 Correct 518 ms 18924 KB Output is correct
88 Correct 520 ms 16020 KB Output is correct
89 Correct 744 ms 17588 KB Output is correct
90 Correct 785 ms 17784 KB Output is correct
91 Correct 544 ms 17004 KB Output is correct
92 Correct 1201 ms 18060 KB Output is correct
93 Correct 1000 ms 17872 KB Output is correct
94 Correct 337 ms 15724 KB Output is correct
95 Correct 903 ms 18028 KB Output is correct
96 Correct 877 ms 17920 KB Output is correct
97 Correct 851 ms 17772 KB Output is correct
98 Correct 838 ms 18028 KB Output is correct
99 Correct 860 ms 18016 KB Output is correct
100 Correct 885 ms 17992 KB Output is correct