Submission #589638

# Submission time Handle Problem Language Result Execution time Memory
589638 2022-07-05T02:57:35 Z wiwiho Snake Escaping (JOI18_snake_escaping) C++14
12 / 100
2000 ms 14144 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);

    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);
        }
        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[i] != '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 = solve0(s);
        cout << ans << "\n";
    }


    return 0;
}

Compilation message

snake_escaping.cpp: In lambda function:
snake_escaping.cpp:120:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |             if(i == pos.size()){
      |                ~~^~~~~~~~~~~~~
snake_escaping.cpp: In lambda function:
snake_escaping.cpp:148:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |             if(i == pos.size()){
      |                ~~^~~~~~~~~~~~~
snake_escaping.cpp: In lambda function:
snake_escaping.cpp:178:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  178 |             if(i == pos.size()){
      |                ~~^~~~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:209:13: warning: unused variable 'mn' [-Wunused-variable]
  209 |         int mn = min({o, z, qm});
      |             ^~
snake_escaping.cpp:112:10: warning: variable 'solveq' set but not used [-Wunused-but-set-variable]
  112 |     auto solveq = [&](string& s){
      |          ^~~~~~
snake_escaping.cpp:169:10: warning: variable 'solve1' set but not used [-Wunused-but-set-variable]
  169 |     auto solve1 = [&](string& s){
      |          ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 212 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 212 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 211 ms 4232 KB Output is correct
12 Correct 1385 ms 4096 KB Output is correct
13 Correct 902 ms 3356 KB Output is correct
14 Correct 585 ms 3348 KB Output is correct
15 Correct 356 ms 4400 KB Output is correct
16 Correct 441 ms 3404 KB Output is correct
17 Correct 650 ms 3408 KB Output is correct
18 Correct 178 ms 7564 KB Output is correct
19 Correct 874 ms 6124 KB Output is correct
20 Correct 220 ms 7756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 212 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 211 ms 4232 KB Output is correct
12 Correct 1385 ms 4096 KB Output is correct
13 Correct 902 ms 3356 KB Output is correct
14 Correct 585 ms 3348 KB Output is correct
15 Correct 356 ms 4400 KB Output is correct
16 Correct 441 ms 3404 KB Output is correct
17 Correct 650 ms 3408 KB Output is correct
18 Correct 178 ms 7564 KB Output is correct
19 Correct 874 ms 6124 KB Output is correct
20 Correct 220 ms 7756 KB Output is correct
21 Correct 242 ms 8068 KB Output is correct
22 Execution timed out 2078 ms 6332 KB Time limit exceeded
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 212 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 157 ms 13916 KB Output is correct
12 Execution timed out 2073 ms 14144 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 212 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 211 ms 4232 KB Output is correct
12 Correct 1385 ms 4096 KB Output is correct
13 Correct 902 ms 3356 KB Output is correct
14 Correct 585 ms 3348 KB Output is correct
15 Correct 356 ms 4400 KB Output is correct
16 Correct 441 ms 3404 KB Output is correct
17 Correct 650 ms 3408 KB Output is correct
18 Correct 178 ms 7564 KB Output is correct
19 Correct 874 ms 6124 KB Output is correct
20 Correct 220 ms 7756 KB Output is correct
21 Correct 242 ms 8068 KB Output is correct
22 Execution timed out 2078 ms 6332 KB Time limit exceeded
23 Halted 0 ms 0 KB -