답안 #589644

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589644 2022-07-05T03:23:54 Z wiwiho Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 43372 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);
        }
        const function<ll(int)> dfs = [&](int i){
            if(i == pos.size()){
                //cerr << bitset<20>(now) << "\n";
                return (ll)a[now];
            }
            int p = pos[i];
            ll ans = 0;
            ans += dfs(i + 1);
            now ^= 1 << (L - 1 - p);
            ans += dfs(i + 1);
            now ^= 1 << (L - 1 - p);
            return ans;
        };
        return dfs(0);
    };
    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 cnt = 0;
        const function<ll(int)> dfs = [&](int i){
            if(i == pos.size()){
                //cerr << bitset<20>(now) << "\n";
                return (ll)one[now] * (cnt % 2 ? -1 : 1);
            }
            int p = pos[i];
            ll ans = 0;
            ans += dfs(i + 1);
            now ^= 1 << (L - 1 - p);
            cnt++;
            ans += dfs(i + 1);
            now ^= 1 << (L - 1 - p);
            cnt--;
            return ans;
        };
        return dfs(0);
    };
    auto solve1 = [&](string& s){
        int now = 0;
        int cnt = 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);
        }
        //printv(pos, cerr);
        const function<ll(int)> dfs = [&](int i){
            if(i == pos.size()){
                //cerr << bitset<20>(now) << "\n";
                return (ll)zero[now] * (cnt % 2 ? -1 : 1);
            }
            int p = pos[i];
            ll ans = 0;
            ans += dfs(i + 1);
            now ^= 1 << (L - 1 - p);
            cnt++;
            ans += dfs(i + 1);
            now ^= 1 << (L - 1 - p);
            cnt--;
            return ans;
        };
        return dfs(0);
    };

    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;
}

Compilation message

snake_escaping.cpp: In lambda function:
snake_escaping.cpp:123:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |             if(i == pos.size()){
      |                ~~^~~~~~~~~~~~~
snake_escaping.cpp: In lambda function:
snake_escaping.cpp:146:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |             if(i == pos.size()){
      |                ~~^~~~~~~~~~~~~
snake_escaping.cpp: In lambda function:
snake_escaping.cpp:172:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |             if(i == pos.size()){
      |                ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 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 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 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 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 292 ms 4732 KB Output is correct
12 Correct 296 ms 4300 KB Output is correct
13 Correct 406 ms 3520 KB Output is correct
14 Correct 527 ms 3664 KB Output is correct
15 Correct 353 ms 4660 KB Output is correct
16 Correct 497 ms 3808 KB Output is correct
17 Correct 461 ms 3852 KB Output is correct
18 Correct 191 ms 5860 KB Output is correct
19 Correct 271 ms 2632 KB Output is correct
20 Correct 287 ms 4316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 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 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 292 ms 4732 KB Output is correct
12 Correct 296 ms 4300 KB Output is correct
13 Correct 406 ms 3520 KB Output is correct
14 Correct 527 ms 3664 KB Output is correct
15 Correct 353 ms 4660 KB Output is correct
16 Correct 497 ms 3808 KB Output is correct
17 Correct 461 ms 3852 KB Output is correct
18 Correct 191 ms 5860 KB Output is correct
19 Correct 271 ms 2632 KB Output is correct
20 Correct 287 ms 4316 KB Output is correct
21 Correct 319 ms 4776 KB Output is correct
22 Correct 337 ms 4900 KB Output is correct
23 Correct 561 ms 3964 KB Output is correct
24 Correct 812 ms 3812 KB Output is correct
25 Correct 436 ms 5760 KB Output is correct
26 Correct 641 ms 4300 KB Output is correct
27 Correct 553 ms 4368 KB Output is correct
28 Correct 203 ms 6712 KB Output is correct
29 Correct 307 ms 2816 KB Output is correct
30 Correct 332 ms 4912 KB Output is correct
31 Correct 503 ms 4812 KB Output is correct
32 Correct 700 ms 4812 KB Output is correct
33 Correct 401 ms 3716 KB Output is correct
34 Correct 646 ms 3912 KB Output is correct
35 Correct 553 ms 4308 KB Output is correct
36 Correct 209 ms 2764 KB Output is correct
37 Correct 299 ms 4752 KB Output is correct
38 Correct 308 ms 2720 KB Output is correct
39 Correct 500 ms 3912 KB Output is correct
40 Correct 692 ms 3812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 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 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 162 ms 14172 KB Output is correct
12 Correct 166 ms 14304 KB Output is correct
13 Correct 185 ms 14188 KB Output is correct
14 Correct 225 ms 14188 KB Output is correct
15 Correct 167 ms 14280 KB Output is correct
16 Correct 226 ms 14144 KB Output is correct
17 Correct 220 ms 14168 KB Output is correct
18 Correct 155 ms 14360 KB Output is correct
19 Correct 158 ms 14092 KB Output is correct
20 Correct 160 ms 14200 KB Output is correct
21 Correct 185 ms 14096 KB Output is correct
22 Correct 222 ms 13916 KB Output is correct
23 Correct 165 ms 13932 KB Output is correct
24 Correct 242 ms 13924 KB Output is correct
25 Correct 214 ms 13920 KB Output is correct
26 Correct 150 ms 13800 KB Output is correct
27 Correct 159 ms 13928 KB Output is correct
28 Correct 161 ms 13852 KB Output is correct
29 Correct 181 ms 13912 KB Output is correct
30 Correct 214 ms 13912 KB Output is correct
31 Correct 168 ms 13928 KB Output is correct
32 Correct 230 ms 13916 KB Output is correct
33 Correct 219 ms 13912 KB Output is correct
34 Correct 148 ms 13808 KB Output is correct
35 Correct 196 ms 13932 KB Output is correct
36 Correct 197 ms 13916 KB Output is correct
37 Correct 199 ms 13932 KB Output is correct
38 Correct 194 ms 13916 KB Output is correct
39 Correct 197 ms 13988 KB Output is correct
40 Correct 194 ms 13928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 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 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 292 ms 4732 KB Output is correct
12 Correct 296 ms 4300 KB Output is correct
13 Correct 406 ms 3520 KB Output is correct
14 Correct 527 ms 3664 KB Output is correct
15 Correct 353 ms 4660 KB Output is correct
16 Correct 497 ms 3808 KB Output is correct
17 Correct 461 ms 3852 KB Output is correct
18 Correct 191 ms 5860 KB Output is correct
19 Correct 271 ms 2632 KB Output is correct
20 Correct 287 ms 4316 KB Output is correct
21 Correct 319 ms 4776 KB Output is correct
22 Correct 337 ms 4900 KB Output is correct
23 Correct 561 ms 3964 KB Output is correct
24 Correct 812 ms 3812 KB Output is correct
25 Correct 436 ms 5760 KB Output is correct
26 Correct 641 ms 4300 KB Output is correct
27 Correct 553 ms 4368 KB Output is correct
28 Correct 203 ms 6712 KB Output is correct
29 Correct 307 ms 2816 KB Output is correct
30 Correct 332 ms 4912 KB Output is correct
31 Correct 503 ms 4812 KB Output is correct
32 Correct 700 ms 4812 KB Output is correct
33 Correct 401 ms 3716 KB Output is correct
34 Correct 646 ms 3912 KB Output is correct
35 Correct 553 ms 4308 KB Output is correct
36 Correct 209 ms 2764 KB Output is correct
37 Correct 299 ms 4752 KB Output is correct
38 Correct 308 ms 2720 KB Output is correct
39 Correct 500 ms 3912 KB Output is correct
40 Correct 692 ms 3812 KB Output is correct
41 Correct 162 ms 14172 KB Output is correct
42 Correct 166 ms 14304 KB Output is correct
43 Correct 185 ms 14188 KB Output is correct
44 Correct 225 ms 14188 KB Output is correct
45 Correct 167 ms 14280 KB Output is correct
46 Correct 226 ms 14144 KB Output is correct
47 Correct 220 ms 14168 KB Output is correct
48 Correct 155 ms 14360 KB Output is correct
49 Correct 158 ms 14092 KB Output is correct
50 Correct 160 ms 14200 KB Output is correct
51 Correct 185 ms 14096 KB Output is correct
52 Correct 222 ms 13916 KB Output is correct
53 Correct 165 ms 13932 KB Output is correct
54 Correct 242 ms 13924 KB Output is correct
55 Correct 214 ms 13920 KB Output is correct
56 Correct 150 ms 13800 KB Output is correct
57 Correct 159 ms 13928 KB Output is correct
58 Correct 161 ms 13852 KB Output is correct
59 Correct 181 ms 13912 KB Output is correct
60 Correct 214 ms 13912 KB Output is correct
61 Correct 168 ms 13928 KB Output is correct
62 Correct 230 ms 13916 KB Output is correct
63 Correct 219 ms 13912 KB Output is correct
64 Correct 148 ms 13808 KB Output is correct
65 Correct 196 ms 13932 KB Output is correct
66 Correct 197 ms 13916 KB Output is correct
67 Correct 199 ms 13932 KB Output is correct
68 Correct 194 ms 13916 KB Output is correct
69 Correct 197 ms 13988 KB Output is correct
70 Correct 194 ms 13928 KB Output is correct
71 Correct 583 ms 18788 KB Output is correct
72 Correct 590 ms 18952 KB Output is correct
73 Correct 1136 ms 17372 KB Output is correct
74 Correct 1897 ms 18056 KB Output is correct
75 Correct 739 ms 20076 KB Output is correct
76 Correct 1921 ms 18796 KB Output is correct
77 Correct 1790 ms 39640 KB Output is correct
78 Correct 435 ms 43372 KB Output is correct
79 Correct 593 ms 37356 KB Output is correct
80 Correct 624 ms 40512 KB Output is correct
81 Correct 1156 ms 40360 KB Output is correct
82 Correct 1911 ms 39416 KB Output is correct
83 Correct 747 ms 38520 KB Output is correct
84 Execution timed out 2062 ms 38688 KB Time limit exceeded
85 Halted 0 ms 0 KB -