답안 #589640

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589640 2022-07-05T03:03:05 Z wiwiho Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 39324 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);
        }
        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;
            if(s[p] != '?'){
                ans += dfs(i + 1);
            }
            else{
                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;
        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;
            if(s[p] != '0'){
                ans += dfs(i + 1);
            }
            else{
                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);
        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;
            if(s[p] != '1'){
                ans += dfs(i + 1);
            }
            else{
                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:151:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |             if(i == pos.size()){
      |                ~~^~~~~~~~~~~~~
snake_escaping.cpp: In lambda function:
snake_escaping.cpp:182:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  182 |             if(i == pos.size()){
      |                ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 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 212 KB Output is correct
7 Correct 1 ms 332 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 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 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 212 KB Output is correct
7 Correct 1 ms 332 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 212 KB Output is correct
11 Correct 262 ms 4332 KB Output is correct
12 Correct 278 ms 3956 KB Output is correct
13 Correct 417 ms 3144 KB Output is correct
14 Correct 502 ms 3452 KB Output is correct
15 Correct 350 ms 4264 KB Output is correct
16 Correct 461 ms 3488 KB Output is correct
17 Correct 467 ms 3468 KB Output is correct
18 Correct 192 ms 5228 KB Output is correct
19 Correct 264 ms 2340 KB Output is correct
20 Correct 281 ms 4004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 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 212 KB Output is correct
7 Correct 1 ms 332 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 212 KB Output is correct
11 Correct 262 ms 4332 KB Output is correct
12 Correct 278 ms 3956 KB Output is correct
13 Correct 417 ms 3144 KB Output is correct
14 Correct 502 ms 3452 KB Output is correct
15 Correct 350 ms 4264 KB Output is correct
16 Correct 461 ms 3488 KB Output is correct
17 Correct 467 ms 3468 KB Output is correct
18 Correct 192 ms 5228 KB Output is correct
19 Correct 264 ms 2340 KB Output is correct
20 Correct 281 ms 4004 KB Output is correct
21 Correct 312 ms 4436 KB Output is correct
22 Correct 320 ms 4496 KB Output is correct
23 Correct 546 ms 7236 KB Output is correct
24 Correct 718 ms 6968 KB Output is correct
25 Correct 423 ms 8844 KB Output is correct
26 Correct 665 ms 7328 KB Output is correct
27 Correct 594 ms 7024 KB Output is correct
28 Correct 215 ms 9508 KB Output is correct
29 Correct 299 ms 5324 KB Output is correct
30 Correct 344 ms 7328 KB Output is correct
31 Correct 503 ms 6980 KB Output is correct
32 Correct 739 ms 6860 KB Output is correct
33 Correct 428 ms 5756 KB Output is correct
34 Correct 619 ms 5792 KB Output is correct
35 Correct 554 ms 6284 KB Output is correct
36 Correct 216 ms 4612 KB Output is correct
37 Correct 297 ms 6464 KB Output is correct
38 Correct 325 ms 4604 KB Output is correct
39 Correct 521 ms 5492 KB Output is correct
40 Correct 704 ms 5280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 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 212 KB Output is correct
7 Correct 1 ms 332 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 212 KB Output is correct
11 Correct 159 ms 13960 KB Output is correct
12 Correct 163 ms 14048 KB Output is correct
13 Correct 201 ms 15948 KB Output is correct
14 Correct 230 ms 15964 KB Output is correct
15 Correct 171 ms 16072 KB Output is correct
16 Correct 237 ms 15948 KB Output is correct
17 Correct 227 ms 15944 KB Output is correct
18 Correct 153 ms 16204 KB Output is correct
19 Correct 161 ms 15816 KB Output is correct
20 Correct 170 ms 16080 KB Output is correct
21 Correct 195 ms 16028 KB Output is correct
22 Correct 230 ms 15984 KB Output is correct
23 Correct 171 ms 15908 KB Output is correct
24 Correct 245 ms 16020 KB Output is correct
25 Correct 230 ms 16036 KB Output is correct
26 Correct 150 ms 15872 KB Output is correct
27 Correct 160 ms 16080 KB Output is correct
28 Correct 171 ms 15828 KB Output is correct
29 Correct 191 ms 15932 KB Output is correct
30 Correct 220 ms 15988 KB Output is correct
31 Correct 180 ms 15964 KB Output is correct
32 Correct 245 ms 16000 KB Output is correct
33 Correct 222 ms 16044 KB Output is correct
34 Correct 150 ms 15832 KB Output is correct
35 Correct 204 ms 16004 KB Output is correct
36 Correct 204 ms 15976 KB Output is correct
37 Correct 199 ms 16040 KB Output is correct
38 Correct 200 ms 16044 KB Output is correct
39 Correct 208 ms 16028 KB Output is correct
40 Correct 200 ms 16076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 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 212 KB Output is correct
7 Correct 1 ms 332 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 212 KB Output is correct
11 Correct 262 ms 4332 KB Output is correct
12 Correct 278 ms 3956 KB Output is correct
13 Correct 417 ms 3144 KB Output is correct
14 Correct 502 ms 3452 KB Output is correct
15 Correct 350 ms 4264 KB Output is correct
16 Correct 461 ms 3488 KB Output is correct
17 Correct 467 ms 3468 KB Output is correct
18 Correct 192 ms 5228 KB Output is correct
19 Correct 264 ms 2340 KB Output is correct
20 Correct 281 ms 4004 KB Output is correct
21 Correct 312 ms 4436 KB Output is correct
22 Correct 320 ms 4496 KB Output is correct
23 Correct 546 ms 7236 KB Output is correct
24 Correct 718 ms 6968 KB Output is correct
25 Correct 423 ms 8844 KB Output is correct
26 Correct 665 ms 7328 KB Output is correct
27 Correct 594 ms 7024 KB Output is correct
28 Correct 215 ms 9508 KB Output is correct
29 Correct 299 ms 5324 KB Output is correct
30 Correct 344 ms 7328 KB Output is correct
31 Correct 503 ms 6980 KB Output is correct
32 Correct 739 ms 6860 KB Output is correct
33 Correct 428 ms 5756 KB Output is correct
34 Correct 619 ms 5792 KB Output is correct
35 Correct 554 ms 6284 KB Output is correct
36 Correct 216 ms 4612 KB Output is correct
37 Correct 297 ms 6464 KB Output is correct
38 Correct 325 ms 4604 KB Output is correct
39 Correct 521 ms 5492 KB Output is correct
40 Correct 704 ms 5280 KB Output is correct
41 Correct 159 ms 13960 KB Output is correct
42 Correct 163 ms 14048 KB Output is correct
43 Correct 201 ms 15948 KB Output is correct
44 Correct 230 ms 15964 KB Output is correct
45 Correct 171 ms 16072 KB Output is correct
46 Correct 237 ms 15948 KB Output is correct
47 Correct 227 ms 15944 KB Output is correct
48 Correct 153 ms 16204 KB Output is correct
49 Correct 161 ms 15816 KB Output is correct
50 Correct 170 ms 16080 KB Output is correct
51 Correct 195 ms 16028 KB Output is correct
52 Correct 230 ms 15984 KB Output is correct
53 Correct 171 ms 15908 KB Output is correct
54 Correct 245 ms 16020 KB Output is correct
55 Correct 230 ms 16036 KB Output is correct
56 Correct 150 ms 15872 KB Output is correct
57 Correct 160 ms 16080 KB Output is correct
58 Correct 171 ms 15828 KB Output is correct
59 Correct 191 ms 15932 KB Output is correct
60 Correct 220 ms 15988 KB Output is correct
61 Correct 180 ms 15964 KB Output is correct
62 Correct 245 ms 16000 KB Output is correct
63 Correct 222 ms 16044 KB Output is correct
64 Correct 150 ms 15832 KB Output is correct
65 Correct 204 ms 16004 KB Output is correct
66 Correct 204 ms 15976 KB Output is correct
67 Correct 199 ms 16040 KB Output is correct
68 Correct 200 ms 16044 KB Output is correct
69 Correct 208 ms 16028 KB Output is correct
70 Correct 200 ms 16076 KB Output is correct
71 Correct 593 ms 19012 KB Output is correct
72 Correct 587 ms 19180 KB Output is correct
73 Correct 1084 ms 20164 KB Output is correct
74 Execution timed out 2025 ms 39324 KB Time limit exceeded
75 Halted 0 ms 0 KB -