답안 #966957

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
966957 2024-04-20T17:38:29 Z efedmrlr Snake Escaping (JOI18_snake_escaping) C++17
12 / 100
2000 ms 25304 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 int INF = 1e9+500;
const int N = 20;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,q;
array<int, (1 << N)> a, dp, dpr;
int RVSR;
inline void solve() {
    cin>>n>>q;
    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);
                else 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();
    solve();
    
}

Compilation message

snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:68:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |                 for(int i = 0; i < occ.size(); i++) {
      |                                ~~^~~~~~~~~~~~
snake_escaping.cpp:87:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |                 for(int i = 0; i < occ.size(); i++) {
      |                                ~~^~~~~~~~~~~~
snake_escaping.cpp:112:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |                 for(int i = 0; i < occ.size(); i++) {
      |                                ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4440 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4440 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 1347 ms 8400 KB Output is correct
12 Correct 1324 ms 8400 KB Output is correct
13 Correct 1447 ms 7456 KB Output is correct
14 Correct 1471 ms 7460 KB Output is correct
15 Correct 1431 ms 8512 KB Output is correct
16 Correct 1474 ms 7608 KB Output is correct
17 Correct 1462 ms 7692 KB Output is correct
18 Correct 1287 ms 9772 KB Output is correct
19 Correct 1590 ms 6508 KB Output is correct
20 Correct 1350 ms 8168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4440 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 1347 ms 8400 KB Output is correct
12 Correct 1324 ms 8400 KB Output is correct
13 Correct 1447 ms 7456 KB Output is correct
14 Correct 1471 ms 7460 KB Output is correct
15 Correct 1431 ms 8512 KB Output is correct
16 Correct 1474 ms 7608 KB Output is correct
17 Correct 1462 ms 7692 KB Output is correct
18 Correct 1287 ms 9772 KB Output is correct
19 Correct 1590 ms 6508 KB Output is correct
20 Correct 1350 ms 8168 KB Output is correct
21 Correct 1368 ms 8740 KB Output is correct
22 Correct 1397 ms 8680 KB Output is correct
23 Correct 1653 ms 7860 KB Output is correct
24 Correct 1558 ms 7412 KB Output is correct
25 Correct 1427 ms 9856 KB Output is correct
26 Correct 1553 ms 8200 KB Output is correct
27 Correct 1595 ms 7892 KB Output is correct
28 Correct 1305 ms 10484 KB Output is correct
29 Correct 1918 ms 6428 KB Output is correct
30 Correct 1386 ms 8708 KB Output is correct
31 Correct 1492 ms 8776 KB Output is correct
32 Correct 1567 ms 8508 KB Output is correct
33 Correct 1491 ms 7316 KB Output is correct
34 Correct 1588 ms 7584 KB Output is correct
35 Correct 1625 ms 7992 KB Output is correct
36 Correct 1288 ms 6564 KB Output is correct
37 Correct 1391 ms 8792 KB Output is correct
38 Execution timed out 2023 ms 6688 KB Time limit exceeded
39 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4440 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 136 ms 12768 KB Output is correct
12 Runtime error 43 ms 25304 KB Execution killed with signal 11
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4440 KB Output is correct
4 Correct 2 ms 4440 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 1347 ms 8400 KB Output is correct
12 Correct 1324 ms 8400 KB Output is correct
13 Correct 1447 ms 7456 KB Output is correct
14 Correct 1471 ms 7460 KB Output is correct
15 Correct 1431 ms 8512 KB Output is correct
16 Correct 1474 ms 7608 KB Output is correct
17 Correct 1462 ms 7692 KB Output is correct
18 Correct 1287 ms 9772 KB Output is correct
19 Correct 1590 ms 6508 KB Output is correct
20 Correct 1350 ms 8168 KB Output is correct
21 Correct 1368 ms 8740 KB Output is correct
22 Correct 1397 ms 8680 KB Output is correct
23 Correct 1653 ms 7860 KB Output is correct
24 Correct 1558 ms 7412 KB Output is correct
25 Correct 1427 ms 9856 KB Output is correct
26 Correct 1553 ms 8200 KB Output is correct
27 Correct 1595 ms 7892 KB Output is correct
28 Correct 1305 ms 10484 KB Output is correct
29 Correct 1918 ms 6428 KB Output is correct
30 Correct 1386 ms 8708 KB Output is correct
31 Correct 1492 ms 8776 KB Output is correct
32 Correct 1567 ms 8508 KB Output is correct
33 Correct 1491 ms 7316 KB Output is correct
34 Correct 1588 ms 7584 KB Output is correct
35 Correct 1625 ms 7992 KB Output is correct
36 Correct 1288 ms 6564 KB Output is correct
37 Correct 1391 ms 8792 KB Output is correct
38 Execution timed out 2023 ms 6688 KB Time limit exceeded
39 Halted 0 ms 0 KB -