답안 #589645

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589645 2022-07-05T03:32:47 Z wiwiho Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1710 ms 40352 KB
#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())

using namespace std;
using namespace __gnu_pbds;

#define orz
#ifdef orz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("[" + string(#a) + "] = ", a)
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}
#else
#define print(a...)
void kout() {}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) {}
#define printv(a, b)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}

ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}

int main(){
    StarBurstStream

    int L, q;
    cin >> L >> q;
    int n = 1 << L;
    
    string ts;
    cin >> ts;

    vector<int> a(n);
    for(int i = 0; i < n; i++){
        a[i] = ts[i] - '0';
    }

    vector<int> one(n), zero(n);
    for(int i = 0; i < n; i++){
        int tmp = 0;
        for(int j = 0; j < L; j++){
            if(1 << j & i) tmp |= 1 << j;
        }
        one[tmp] += a[i];
        zero[((1 << L) - 1) ^ tmp] += a [i];
    }

    auto solvedp = [&](vector<int>& dp){
        for(int i = 0; i < L; i++){
            for(int j = 0; j < n; j++){
                if(1 << i & j) continue;
                dp[j] += dp[j ^ (1 << i)];
            }
        }
    };

    solvedp(zero);
    solvedp(one);
    /*for(int i = 0; i < n; i++){
        cerr << "zero " << bitset<5>(i) << ": " << zero[i] << "\n";
    }*/

    auto solveq = [&](string& s){
        int now = 0;
        vector<int> pos;
        for(int i = 0; i < L; i++){
            if(s[i] == '?') pos.eb(i);
            else if(s[i] == '1') now |= 1 << (L - 1 - i);
        }
        int sz = pos.size();
        ll ans = 0;
        for(int i = 0; i < (1 << sz); i++){
            int tmp = now;
            for(int j = 0; j < sz; j++){
                if(1 << j & i) tmp |= 1 << (L - 1 - pos[j]);
            }
            ans += a[tmp];
        }
        return ans;
    };
    auto solve0 = [&](string& s){
        int now = 0;
        vector<int> pos;
        for(int i = 0; i < L; i++){
            if(s[i] == '0') pos.eb(i);
            else if(s[i] == '1') now |= 1 << (L - 1 - i);
        }
        int sz = pos.size();
        ll ans = 0;
        for(int i = 0; i < (1 << sz); i++){
            int tmp = now;
            int cnt = __builtin_popcount(i);
            for(int j = 0; j < sz; j++){
                if(1 << j & i) tmp |= 1 << (L - 1 - pos[j]);
            }
            ans += one[tmp] * (cnt % 2 ? -1 : 1);
        }
        return ans;
    };
    auto solve1 = [&](string& s){
        int now = 0;
        vector<int> pos;
        for(int i = 0; i < L; i++){
            if(s[i] == '1') pos.eb(i);
            else if(s[i] == '0') now |= 1 << (L - 1 - i);
        }
        int sz = pos.size();
        ll ans = 0;
        for(int i = 0; i < (1 << sz); i++){
            int tmp = now;
            int cnt = __builtin_popcount(i);
            for(int j = 0; j < sz; j++){
                if(1 << j & i) tmp |= 1 << (L - 1 - pos[j]);
            }
            ans += zero[tmp] * (cnt % 2 ? -1 : 1);
        }
        return ans;
    };

    while(q--){
        string s;
        cin >> s;
        int o = 0, z = 0, qm = 0;
        for(char i : s){
            if(i == '0') z++;
            else if(i == '1') o++;
            else qm++;
        }
        int mn = min({o, z, qm});
        ll ans = 0;
        if(mn == z) ans = solve0(s);
        else if(mn == o) ans = solve1(s);
        else ans = solveq(s);
        //ans = solve1(s);
        cout << ans << "\n";
    }


    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 214 ms 4784 KB Output is correct
12 Correct 246 ms 4536 KB Output is correct
13 Correct 339 ms 3736 KB Output is correct
14 Correct 408 ms 3816 KB Output is correct
15 Correct 310 ms 4752 KB Output is correct
16 Correct 397 ms 4044 KB Output is correct
17 Correct 374 ms 3888 KB Output is correct
18 Correct 165 ms 5964 KB Output is correct
19 Correct 208 ms 2824 KB Output is correct
20 Correct 248 ms 4636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 214 ms 4784 KB Output is correct
12 Correct 246 ms 4536 KB Output is correct
13 Correct 339 ms 3736 KB Output is correct
14 Correct 408 ms 3816 KB Output is correct
15 Correct 310 ms 4752 KB Output is correct
16 Correct 397 ms 4044 KB Output is correct
17 Correct 374 ms 3888 KB Output is correct
18 Correct 165 ms 5964 KB Output is correct
19 Correct 208 ms 2824 KB Output is correct
20 Correct 248 ms 4636 KB Output is correct
21 Correct 271 ms 4940 KB Output is correct
22 Correct 299 ms 4988 KB Output is correct
23 Correct 449 ms 4104 KB Output is correct
24 Correct 539 ms 3980 KB Output is correct
25 Correct 365 ms 5964 KB Output is correct
26 Correct 526 ms 4552 KB Output is correct
27 Correct 491 ms 4308 KB Output is correct
28 Correct 179 ms 6932 KB Output is correct
29 Correct 266 ms 2888 KB Output is correct
30 Correct 299 ms 5044 KB Output is correct
31 Correct 447 ms 4836 KB Output is correct
32 Correct 569 ms 4936 KB Output is correct
33 Correct 350 ms 3728 KB Output is correct
34 Correct 521 ms 3964 KB Output is correct
35 Correct 492 ms 4320 KB Output is correct
36 Correct 191 ms 2928 KB Output is correct
37 Correct 269 ms 5152 KB Output is correct
38 Correct 289 ms 2900 KB Output is correct
39 Correct 433 ms 4060 KB Output is correct
40 Correct 573 ms 4016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 157 ms 14420 KB Output is correct
12 Correct 166 ms 14452 KB Output is correct
13 Correct 173 ms 14328 KB Output is correct
14 Correct 200 ms 14332 KB Output is correct
15 Correct 166 ms 14576 KB Output is correct
16 Correct 204 ms 14424 KB Output is correct
17 Correct 196 ms 14492 KB Output is correct
18 Correct 158 ms 14608 KB Output is correct
19 Correct 157 ms 14348 KB Output is correct
20 Correct 158 ms 14428 KB Output is correct
21 Correct 179 ms 14424 KB Output is correct
22 Correct 209 ms 14228 KB Output is correct
23 Correct 161 ms 14136 KB Output is correct
24 Correct 214 ms 14212 KB Output is correct
25 Correct 203 ms 14180 KB Output is correct
26 Correct 149 ms 14120 KB Output is correct
27 Correct 162 ms 14360 KB Output is correct
28 Correct 156 ms 14108 KB Output is correct
29 Correct 175 ms 14116 KB Output is correct
30 Correct 195 ms 14208 KB Output is correct
31 Correct 161 ms 14168 KB Output is correct
32 Correct 209 ms 14276 KB Output is correct
33 Correct 202 ms 14268 KB Output is correct
34 Correct 146 ms 14096 KB Output is correct
35 Correct 184 ms 14168 KB Output is correct
36 Correct 190 ms 14292 KB Output is correct
37 Correct 185 ms 14308 KB Output is correct
38 Correct 186 ms 14132 KB Output is correct
39 Correct 194 ms 14124 KB Output is correct
40 Correct 185 ms 14220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 214 ms 4784 KB Output is correct
12 Correct 246 ms 4536 KB Output is correct
13 Correct 339 ms 3736 KB Output is correct
14 Correct 408 ms 3816 KB Output is correct
15 Correct 310 ms 4752 KB Output is correct
16 Correct 397 ms 4044 KB Output is correct
17 Correct 374 ms 3888 KB Output is correct
18 Correct 165 ms 5964 KB Output is correct
19 Correct 208 ms 2824 KB Output is correct
20 Correct 248 ms 4636 KB Output is correct
21 Correct 271 ms 4940 KB Output is correct
22 Correct 299 ms 4988 KB Output is correct
23 Correct 449 ms 4104 KB Output is correct
24 Correct 539 ms 3980 KB Output is correct
25 Correct 365 ms 5964 KB Output is correct
26 Correct 526 ms 4552 KB Output is correct
27 Correct 491 ms 4308 KB Output is correct
28 Correct 179 ms 6932 KB Output is correct
29 Correct 266 ms 2888 KB Output is correct
30 Correct 299 ms 5044 KB Output is correct
31 Correct 447 ms 4836 KB Output is correct
32 Correct 569 ms 4936 KB Output is correct
33 Correct 350 ms 3728 KB Output is correct
34 Correct 521 ms 3964 KB Output is correct
35 Correct 492 ms 4320 KB Output is correct
36 Correct 191 ms 2928 KB Output is correct
37 Correct 269 ms 5152 KB Output is correct
38 Correct 289 ms 2900 KB Output is correct
39 Correct 433 ms 4060 KB Output is correct
40 Correct 573 ms 4016 KB Output is correct
41 Correct 157 ms 14420 KB Output is correct
42 Correct 166 ms 14452 KB Output is correct
43 Correct 173 ms 14328 KB Output is correct
44 Correct 200 ms 14332 KB Output is correct
45 Correct 166 ms 14576 KB Output is correct
46 Correct 204 ms 14424 KB Output is correct
47 Correct 196 ms 14492 KB Output is correct
48 Correct 158 ms 14608 KB Output is correct
49 Correct 157 ms 14348 KB Output is correct
50 Correct 158 ms 14428 KB Output is correct
51 Correct 179 ms 14424 KB Output is correct
52 Correct 209 ms 14228 KB Output is correct
53 Correct 161 ms 14136 KB Output is correct
54 Correct 214 ms 14212 KB Output is correct
55 Correct 203 ms 14180 KB Output is correct
56 Correct 149 ms 14120 KB Output is correct
57 Correct 162 ms 14360 KB Output is correct
58 Correct 156 ms 14108 KB Output is correct
59 Correct 175 ms 14116 KB Output is correct
60 Correct 195 ms 14208 KB Output is correct
61 Correct 161 ms 14168 KB Output is correct
62 Correct 209 ms 14276 KB Output is correct
63 Correct 202 ms 14268 KB Output is correct
64 Correct 146 ms 14096 KB Output is correct
65 Correct 184 ms 14168 KB Output is correct
66 Correct 190 ms 14292 KB Output is correct
67 Correct 185 ms 14308 KB Output is correct
68 Correct 186 ms 14132 KB Output is correct
69 Correct 194 ms 14124 KB Output is correct
70 Correct 185 ms 14220 KB Output is correct
71 Correct 564 ms 18864 KB Output is correct
72 Correct 553 ms 19056 KB Output is correct
73 Correct 939 ms 17740 KB Output is correct
74 Correct 1428 ms 18220 KB Output is correct
75 Correct 661 ms 19820 KB Output is correct
76 Correct 1527 ms 18928 KB Output is correct
77 Correct 1375 ms 19372 KB Output is correct
78 Correct 395 ms 23128 KB Output is correct
79 Correct 552 ms 17084 KB Output is correct
80 Correct 594 ms 20320 KB Output is correct
81 Correct 945 ms 20172 KB Output is correct
82 Correct 1617 ms 19116 KB Output is correct
83 Correct 674 ms 18400 KB Output is correct
84 Correct 1710 ms 19512 KB Output is correct
85 Correct 1418 ms 39668 KB Output is correct
86 Correct 388 ms 37356 KB Output is correct
87 Correct 550 ms 40352 KB Output is correct
88 Correct 570 ms 37292 KB Output is correct
89 Correct 938 ms 39008 KB Output is correct
90 Correct 1321 ms 39376 KB Output is correct
91 Correct 685 ms 38536 KB Output is correct
92 Correct 1661 ms 39760 KB Output is correct
93 Correct 1391 ms 39620 KB Output is correct
94 Correct 383 ms 37280 KB Output is correct
95 Correct 1142 ms 39460 KB Output is correct
96 Correct 1131 ms 39564 KB Output is correct
97 Correct 1135 ms 39472 KB Output is correct
98 Correct 1115 ms 39616 KB Output is correct
99 Correct 1133 ms 39580 KB Output is correct
100 Correct 1127 ms 39496 KB Output is correct