답안 #966951

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
966951 2024-04-20T17:27:58 Z efedmrlr Snake Escaping (JOI18_snake_escaping) C++17
12 / 100
2000 ms 26704 KB
// #pragma GCC optimize("O3,Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
    
using namespace std;
    
    
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
    
    
void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
    
    
const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,q;
vector<int> dp, dpr, a;
int RVSR;
inline void solve() {
    cin>>n>>q;
    dp.assign((1 << n), 0);
    dpr.assign((1 << n), 0);
    a.assign((1 << n), 0);
    RVSR = (1 << n) - 1;
    for(int i = 0; i < (1 << n); i++) {
        char tmp;
        cin >> tmp;
        a[i] = dp[i] = dpr[i ^ RVSR] = tmp - '0';
    }
    
    for(int k = 0; k < n; k++) {
        for(int i = 0; i < (1 << n); i++) {
            if(i & (1 << k)) {
                dp[i] += dp[i ^ (1 << k)];
                dpr[i] += dpr[i ^ (1 << k)];
            }
        }
    }
    // for(int i = 0; i < (1 << n); i++) {
    //     cout << "i:" << i <<" dp:" << dp[i] << " dpr:" << dpr[i] << "\n";
    // }
    REP(z, q) {
        string s;
        cin >> s;
        reverse(all(s));
        int cnt0 = 0, cnt1 = 0;
        for(char c : s) {
            if(c == '0') cnt0++;
            else if(c == '1') cnt1++;
        }
        int ans = 0;
        if(cnt1 + cnt0 >= 14) {
            int x = 0;
            vector<int> occ;
            for(int i = 0; i < n; i++) {
                if(s[i] == '?') occ.pb(i);
                if(s[i] == '1') x += (1 << i);
            }
            for(int mask = 0; mask < (1 << occ.size()); mask++) {
                for(int i = 0; i < occ.size(); i++) {
                    if(mask & (1 << i)) {
                        x += (1 << occ[i]);
                    }
                }
                ans += a[x];
            }
        }
        else if(cnt1 > cnt0) {   // use reverse
            int x = 0;
            vector<int> occ;
            for(int i = 0; i < n; i++) {
                if(s[i] != '1') x += (1 << i);
                if(s[i] == '0') {
                    occ.pb(i);
                }
            }
            for(int mask = 0; mask < (1 << ((int)occ.size())); mask++) {
                int cur = x;
                for(int i = 0; i < occ.size(); i++) {
                    if(mask & (1 << i)) {
                        cur ^= 1 << (occ[i]);
                    }
                }
                if(__popcount(mask) & 1) {
                    ans -= dpr[cur];
                }
                else {
                    ans += dpr[cur];
                }
            }
        }
        else {
            int x = 0;
            vector<int> occ;
            for(int i = 0; i < n; i++) {
                if(s[i] != '0') x += (1 << i);
                if(s[i] == '1') {
                    occ.pb(i);
                }
            }
            for(int mask = 0; mask < (1 << ((int)occ.size())); mask++) {
                int cur = x;
                // cout << "mask:" << mask << " cur:" << cur << "\n";
                for(int i = 0; i < occ.size(); i++) {
                    if(mask & (1 << i)) {
                        cur ^= 1 << (occ[i]);
                    }
                }
                if(__popcount(mask) & 1) {
                    ans -= dp[cur];
                }
                else {
                    ans += dp[cur];
                }
            }
        }
        cout << ans << endl;
    }
    
    
    
}
    
signed main() {
    
    fastio();
    int test = 1;
    //cin>>test;
    while(test--) {
        solve();
    }
    
}

Compilation message

snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:72:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |                 for(int i = 0; i < occ.size(); i++) {
      |                                ~~^~~~~~~~~~~~
snake_escaping.cpp:91:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |                 for(int i = 0; i < occ.size(); i++) {
      |                                ~~^~~~~~~~~~~~
snake_escaping.cpp:116:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |                 for(int i = 0; i < occ.size(); i++) {
      |                                ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1294 ms 4672 KB Output is correct
12 Correct 1323 ms 4196 KB Output is correct
13 Correct 1427 ms 3732 KB Output is correct
14 Correct 1411 ms 3348 KB Output is correct
15 Correct 1349 ms 4640 KB Output is correct
16 Correct 1441 ms 3636 KB Output is correct
17 Correct 1485 ms 3760 KB Output is correct
18 Correct 1305 ms 11052 KB Output is correct
19 Correct 1669 ms 7988 KB Output is correct
20 Correct 1302 ms 9700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1294 ms 4672 KB Output is correct
12 Correct 1323 ms 4196 KB Output is correct
13 Correct 1427 ms 3732 KB Output is correct
14 Correct 1411 ms 3348 KB Output is correct
15 Correct 1349 ms 4640 KB Output is correct
16 Correct 1441 ms 3636 KB Output is correct
17 Correct 1485 ms 3760 KB Output is correct
18 Correct 1305 ms 11052 KB Output is correct
19 Correct 1669 ms 7988 KB Output is correct
20 Correct 1302 ms 9700 KB Output is correct
21 Correct 1321 ms 11608 KB Output is correct
22 Correct 1430 ms 12052 KB Output is correct
23 Correct 1626 ms 10948 KB Output is correct
24 Correct 1552 ms 10628 KB Output is correct
25 Correct 1386 ms 12492 KB Output is correct
26 Correct 1534 ms 11260 KB Output is correct
27 Correct 1556 ms 11576 KB Output is correct
28 Correct 1242 ms 13704 KB Output is correct
29 Execution timed out 2027 ms 9752 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 107 ms 14160 KB Output is correct
12 Runtime error 43 ms 26704 KB Execution killed with signal 11
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1294 ms 4672 KB Output is correct
12 Correct 1323 ms 4196 KB Output is correct
13 Correct 1427 ms 3732 KB Output is correct
14 Correct 1411 ms 3348 KB Output is correct
15 Correct 1349 ms 4640 KB Output is correct
16 Correct 1441 ms 3636 KB Output is correct
17 Correct 1485 ms 3760 KB Output is correct
18 Correct 1305 ms 11052 KB Output is correct
19 Correct 1669 ms 7988 KB Output is correct
20 Correct 1302 ms 9700 KB Output is correct
21 Correct 1321 ms 11608 KB Output is correct
22 Correct 1430 ms 12052 KB Output is correct
23 Correct 1626 ms 10948 KB Output is correct
24 Correct 1552 ms 10628 KB Output is correct
25 Correct 1386 ms 12492 KB Output is correct
26 Correct 1534 ms 11260 KB Output is correct
27 Correct 1556 ms 11576 KB Output is correct
28 Correct 1242 ms 13704 KB Output is correct
29 Execution timed out 2027 ms 9752 KB Time limit exceeded
30 Halted 0 ms 0 KB -