답안 #364826

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
364826 2021-02-10T07:49:16 Z balbit Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1317 ms 43244 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], B[1<<20], F[1<<20];
bool poppar[1<<20];

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;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 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 364 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 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 364 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 286 ms 15212 KB Output is correct
12 Correct 335 ms 14956 KB Output is correct
13 Correct 295 ms 14188 KB Output is correct
14 Correct 320 ms 14308 KB Output is correct
15 Correct 318 ms 15212 KB Output is correct
16 Correct 343 ms 14536 KB Output is correct
17 Correct 347 ms 14316 KB Output is correct
18 Correct 244 ms 16236 KB Output is correct
19 Correct 250 ms 13164 KB Output is correct
20 Correct 320 ms 14876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 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 364 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 286 ms 15212 KB Output is correct
12 Correct 335 ms 14956 KB Output is correct
13 Correct 295 ms 14188 KB Output is correct
14 Correct 320 ms 14308 KB Output is correct
15 Correct 318 ms 15212 KB Output is correct
16 Correct 343 ms 14536 KB Output is correct
17 Correct 347 ms 14316 KB Output is correct
18 Correct 244 ms 16236 KB Output is correct
19 Correct 250 ms 13164 KB Output is correct
20 Correct 320 ms 14876 KB Output is correct
21 Correct 355 ms 18284 KB Output is correct
22 Correct 318 ms 18412 KB Output is correct
23 Correct 380 ms 17516 KB Output is correct
24 Correct 377 ms 17260 KB Output is correct
25 Correct 381 ms 19412 KB Output is correct
26 Correct 397 ms 17796 KB Output is correct
27 Correct 437 ms 17704 KB Output is correct
28 Correct 253 ms 20332 KB Output is correct
29 Correct 287 ms 16256 KB Output is correct
30 Correct 392 ms 18412 KB Output is correct
31 Correct 425 ms 18204 KB Output is correct
32 Correct 404 ms 18188 KB Output is correct
33 Correct 353 ms 17148 KB Output is correct
34 Correct 414 ms 17388 KB Output is correct
35 Correct 422 ms 17644 KB Output is correct
36 Correct 210 ms 16236 KB Output is correct
37 Correct 294 ms 18412 KB Output is correct
38 Correct 284 ms 16364 KB Output is correct
39 Correct 413 ms 17388 KB Output is correct
40 Correct 376 ms 17260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 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 364 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 83 ms 15980 KB Output is correct
12 Correct 78 ms 15980 KB Output is correct
13 Correct 88 ms 15980 KB Output is correct
14 Correct 102 ms 16068 KB Output is correct
15 Correct 83 ms 15980 KB Output is correct
16 Correct 109 ms 16036 KB Output is correct
17 Correct 106 ms 15980 KB Output is correct
18 Correct 72 ms 16108 KB Output is correct
19 Correct 76 ms 15852 KB Output is correct
20 Correct 84 ms 15980 KB Output is correct
21 Correct 90 ms 15980 KB Output is correct
22 Correct 105 ms 15980 KB Output is correct
23 Correct 81 ms 15852 KB Output is correct
24 Correct 104 ms 15980 KB Output is correct
25 Correct 104 ms 15980 KB Output is correct
26 Correct 67 ms 15852 KB Output is correct
27 Correct 77 ms 15980 KB Output is correct
28 Correct 78 ms 16128 KB Output is correct
29 Correct 91 ms 15980 KB Output is correct
30 Correct 96 ms 15980 KB Output is correct
31 Correct 81 ms 15852 KB Output is correct
32 Correct 115 ms 15980 KB Output is correct
33 Correct 107 ms 16024 KB Output is correct
34 Correct 68 ms 15852 KB Output is correct
35 Correct 94 ms 15980 KB Output is correct
36 Correct 93 ms 15980 KB Output is correct
37 Correct 107 ms 15980 KB Output is correct
38 Correct 106 ms 15980 KB Output is correct
39 Correct 95 ms 15980 KB Output is correct
40 Correct 107 ms 15980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 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 364 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 286 ms 15212 KB Output is correct
12 Correct 335 ms 14956 KB Output is correct
13 Correct 295 ms 14188 KB Output is correct
14 Correct 320 ms 14308 KB Output is correct
15 Correct 318 ms 15212 KB Output is correct
16 Correct 343 ms 14536 KB Output is correct
17 Correct 347 ms 14316 KB Output is correct
18 Correct 244 ms 16236 KB Output is correct
19 Correct 250 ms 13164 KB Output is correct
20 Correct 320 ms 14876 KB Output is correct
21 Correct 355 ms 18284 KB Output is correct
22 Correct 318 ms 18412 KB Output is correct
23 Correct 380 ms 17516 KB Output is correct
24 Correct 377 ms 17260 KB Output is correct
25 Correct 381 ms 19412 KB Output is correct
26 Correct 397 ms 17796 KB Output is correct
27 Correct 437 ms 17704 KB Output is correct
28 Correct 253 ms 20332 KB Output is correct
29 Correct 287 ms 16256 KB Output is correct
30 Correct 392 ms 18412 KB Output is correct
31 Correct 425 ms 18204 KB Output is correct
32 Correct 404 ms 18188 KB Output is correct
33 Correct 353 ms 17148 KB Output is correct
34 Correct 414 ms 17388 KB Output is correct
35 Correct 422 ms 17644 KB Output is correct
36 Correct 210 ms 16236 KB Output is correct
37 Correct 294 ms 18412 KB Output is correct
38 Correct 284 ms 16364 KB Output is correct
39 Correct 413 ms 17388 KB Output is correct
40 Correct 376 ms 17260 KB Output is correct
41 Correct 83 ms 15980 KB Output is correct
42 Correct 78 ms 15980 KB Output is correct
43 Correct 88 ms 15980 KB Output is correct
44 Correct 102 ms 16068 KB Output is correct
45 Correct 83 ms 15980 KB Output is correct
46 Correct 109 ms 16036 KB Output is correct
47 Correct 106 ms 15980 KB Output is correct
48 Correct 72 ms 16108 KB Output is correct
49 Correct 76 ms 15852 KB Output is correct
50 Correct 84 ms 15980 KB Output is correct
51 Correct 90 ms 15980 KB Output is correct
52 Correct 105 ms 15980 KB Output is correct
53 Correct 81 ms 15852 KB Output is correct
54 Correct 104 ms 15980 KB Output is correct
55 Correct 104 ms 15980 KB Output is correct
56 Correct 67 ms 15852 KB Output is correct
57 Correct 77 ms 15980 KB Output is correct
58 Correct 78 ms 16128 KB Output is correct
59 Correct 91 ms 15980 KB Output is correct
60 Correct 96 ms 15980 KB Output is correct
61 Correct 81 ms 15852 KB Output is correct
62 Correct 115 ms 15980 KB Output is correct
63 Correct 107 ms 16024 KB Output is correct
64 Correct 68 ms 15852 KB Output is correct
65 Correct 94 ms 15980 KB Output is correct
66 Correct 93 ms 15980 KB Output is correct
67 Correct 107 ms 15980 KB Output is correct
68 Correct 106 ms 15980 KB Output is correct
69 Correct 95 ms 15980 KB Output is correct
70 Correct 107 ms 15980 KB Output is correct
71 Correct 672 ms 40428 KB Output is correct
72 Correct 592 ms 40368 KB Output is correct
73 Correct 740 ms 38976 KB Output is correct
74 Correct 1123 ms 39416 KB Output is correct
75 Correct 666 ms 41324 KB Output is correct
76 Correct 1261 ms 39592 KB Output is correct
77 Correct 1058 ms 39532 KB Output is correct
78 Correct 416 ms 43244 KB Output is correct
79 Correct 553 ms 37356 KB Output is correct
80 Correct 698 ms 40468 KB Output is correct
81 Correct 927 ms 40320 KB Output is correct
82 Correct 1317 ms 39356 KB Output is correct
83 Correct 608 ms 38640 KB Output is correct
84 Correct 1137 ms 39276 KB Output is correct
85 Correct 1044 ms 39428 KB Output is correct
86 Correct 358 ms 37356 KB Output is correct
87 Correct 558 ms 40300 KB Output is correct
88 Correct 612 ms 37284 KB Output is correct
89 Correct 903 ms 38892 KB Output is correct
90 Correct 913 ms 39424 KB Output is correct
91 Correct 607 ms 38504 KB Output is correct
92 Correct 1256 ms 39840 KB Output is correct
93 Correct 1082 ms 39504 KB Output is correct
94 Correct 368 ms 37264 KB Output is correct
95 Correct 942 ms 39320 KB Output is correct
96 Correct 888 ms 39352 KB Output is correct
97 Correct 903 ms 39420 KB Output is correct
98 Correct 898 ms 39484 KB Output is correct
99 Correct 918 ms 39468 KB Output is correct
100 Correct 901 ms 39436 KB Output is correct