답안 #1073514

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1073514 2024-08-24T15:36:32 Z Requiem Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 25204 KB
#include<bits/stdc++.h>
#define int long long
#define TASKNAME "snake"
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
#define fi first
#define fast ios_base::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL)
#define se second
#define pb push_back
#define MASK(i) (1LL << i) //2^i
#define BIT(x, i) (((x) >> (i))&1) //lay bit thu i cua i

using namespace std;
typedef pair<int, int> ii;
/**
Cho n = 2^L - 1 va q truy van:
Voi truy van thu i. Ta duoc 1 chuoi str gom ?, 0, 1:
1 mask goi la khop neu khi ta so tung vi tri thi str[i] bang '?' hoac mask[i] == str[i].

subtask 1: L <= 10, n <= 1000.
subtask 2: L <= 13.
subtask 3: L <= 15
subtask 4: Q <= 50000
subtask 5: L <= 20, Q <= 1000000

Gọi X là số dấu ?
Nếu X <= 10 thì ta bruteforce.
Nếu X > 10 thì ta sẽ có tối đa 9 số. Thì ta sẽ dùng bao hàm bù trừ kết hợp dp SOS để tính nhanh.
**/

const int MAX = MASK(20);
int dp[MAX], val[MAX];
char query[21];
int lengthBit;
int numQuery;
namespace subtask5{
    bool check(){
        return true;
    }
    vector<int> hidden;
    vector<int> number;
    int ans = 0, mask1 = 0, mask2 = 0;

    void bruteforce(int pos){
        if (pos == hidden.size()){
            ans += val[mask1];
            return;
        }

        bruteforce(pos + 1);

        mask1 ^= (1LL << hidden[pos]);
        bruteforce(pos + 1);
        mask1 ^= (1LL << hidden[pos]);
    }

    void calc2(int pos, int val){
        if (pos == number.size()){
            ans += dp[mask2] * ((val&1) ? (-1) : (1));
            return;
        }
        calc2(pos + 1, val + 1);

        mask2 ^= (1LL << number[pos]);
        calc2(pos + 1, val);
        mask2 ^= (1LL << number[pos]);
    }
    void solve(){
        FOR(i, 0, numQuery - 1){
            mask1 = 0, mask2 = 0, ans = 0;
            FORD(j, lengthBit - 1, 0){
                cin >> query[j];
                if (query[j] == '?') hidden.pb(j), mask2 ^= (1LL << j);
                else if (query[j] == '1') number.pb(j), mask1 ^= (1 << j);

            }
            if (hidden.size() <= lengthBit / 2){
                bruteforce(0);
                printf("%lld\n", ans);
            }
            else {
                calc2(0, 0);
                printf("%lld\n", ans);
            }
            number.clear();
            hidden.clear();
        }
    }
}
main(){
    fast;
    if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin);
        freopen(TASKNAME".out","w",stdout);
    }
    cin >> lengthBit >> numQuery;
    FOR(i, 0, MASK(lengthBit) - 1){
        char c;
        cin >> c;
        val[i] = (c - '0');
        dp[i] += (c - '0');
    }

    FOR(j, 0, lengthBit - 1){
        FOR(i, 0, MASK(lengthBit) - 1){
            if (i & (1 << j)) dp[i] += dp[i ^ (1 << j)];
        }
    }

//    FOR(i, 0, MASK(lengthBit) - 1) printf("%lld\n", dp[i]);

    if (subtask5::check()) return subtask5::solve(), 0;

}

Compilation message

snake_escaping.cpp: In function 'void subtask5::bruteforce(long long int)':
snake_escaping.cpp:45:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |         if (pos == hidden.size()){
      |             ~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp: In function 'void subtask5::calc2(long long int, long long int)':
snake_escaping.cpp:58:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         if (pos == number.size()){
      |             ~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp: In function 'void subtask5::solve()':
snake_escaping.cpp:77:31: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   77 |             if (hidden.size() <= lengthBit / 2){
      |                 ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp: At global scope:
snake_escaping.cpp:90:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   90 | main(){
      | ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 347 ms 6480 KB Output is correct
12 Correct 267 ms 5968 KB Output is correct
13 Correct 250 ms 5456 KB Output is correct
14 Correct 278 ms 5308 KB Output is correct
15 Correct 244 ms 6488 KB Output is correct
16 Correct 293 ms 5456 KB Output is correct
17 Correct 299 ms 5460 KB Output is correct
18 Correct 142 ms 14080 KB Output is correct
19 Correct 202 ms 15124 KB Output is correct
20 Correct 306 ms 16720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 347 ms 6480 KB Output is correct
12 Correct 267 ms 5968 KB Output is correct
13 Correct 250 ms 5456 KB Output is correct
14 Correct 278 ms 5308 KB Output is correct
15 Correct 244 ms 6488 KB Output is correct
16 Correct 293 ms 5456 KB Output is correct
17 Correct 299 ms 5460 KB Output is correct
18 Correct 142 ms 14080 KB Output is correct
19 Correct 202 ms 15124 KB Output is correct
20 Correct 306 ms 16720 KB Output is correct
21 Correct 598 ms 20144 KB Output is correct
22 Correct 378 ms 20204 KB Output is correct
23 Correct 362 ms 19244 KB Output is correct
24 Correct 360 ms 19284 KB Output is correct
25 Correct 321 ms 21244 KB Output is correct
26 Correct 435 ms 19792 KB Output is correct
27 Correct 413 ms 19524 KB Output is correct
28 Correct 170 ms 22092 KB Output is correct
29 Correct 272 ms 18256 KB Output is correct
30 Correct 479 ms 20304 KB Output is correct
31 Correct 601 ms 20052 KB Output is correct
32 Correct 464 ms 20048 KB Output is correct
33 Correct 348 ms 19028 KB Output is correct
34 Correct 401 ms 19280 KB Output is correct
35 Correct 419 ms 19520 KB Output is correct
36 Correct 175 ms 18176 KB Output is correct
37 Correct 551 ms 20052 KB Output is correct
38 Correct 289 ms 18256 KB Output is correct
39 Correct 377 ms 19256 KB Output is correct
40 Correct 375 ms 19228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 386 ms 16892 KB Output is correct
12 Correct 144 ms 17232 KB Output is correct
13 Correct 64 ms 16900 KB Output is correct
14 Correct 93 ms 16980 KB Output is correct
15 Correct 45 ms 16980 KB Output is correct
16 Correct 128 ms 16864 KB Output is correct
17 Correct 116 ms 16872 KB Output is correct
18 Correct 35 ms 17232 KB Output is correct
19 Correct 54 ms 16916 KB Output is correct
20 Correct 183 ms 16880 KB Output is correct
21 Correct 382 ms 17008 KB Output is correct
22 Correct 94 ms 16976 KB Output is correct
23 Correct 61 ms 16980 KB Output is correct
24 Correct 72 ms 16976 KB Output is correct
25 Correct 113 ms 16960 KB Output is correct
26 Correct 33 ms 16784 KB Output is correct
27 Correct 366 ms 16880 KB Output is correct
28 Correct 46 ms 16884 KB Output is correct
29 Correct 68 ms 16976 KB Output is correct
30 Correct 78 ms 16888 KB Output is correct
31 Correct 45 ms 16984 KB Output is correct
32 Correct 126 ms 16980 KB Output is correct
33 Correct 118 ms 16892 KB Output is correct
34 Correct 45 ms 16872 KB Output is correct
35 Correct 122 ms 16980 KB Output is correct
36 Correct 122 ms 16976 KB Output is correct
37 Correct 123 ms 16864 KB Output is correct
38 Correct 122 ms 16976 KB Output is correct
39 Correct 124 ms 16940 KB Output is correct
40 Correct 139 ms 16976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 347 ms 6480 KB Output is correct
12 Correct 267 ms 5968 KB Output is correct
13 Correct 250 ms 5456 KB Output is correct
14 Correct 278 ms 5308 KB Output is correct
15 Correct 244 ms 6488 KB Output is correct
16 Correct 293 ms 5456 KB Output is correct
17 Correct 299 ms 5460 KB Output is correct
18 Correct 142 ms 14080 KB Output is correct
19 Correct 202 ms 15124 KB Output is correct
20 Correct 306 ms 16720 KB Output is correct
21 Correct 598 ms 20144 KB Output is correct
22 Correct 378 ms 20204 KB Output is correct
23 Correct 362 ms 19244 KB Output is correct
24 Correct 360 ms 19284 KB Output is correct
25 Correct 321 ms 21244 KB Output is correct
26 Correct 435 ms 19792 KB Output is correct
27 Correct 413 ms 19524 KB Output is correct
28 Correct 170 ms 22092 KB Output is correct
29 Correct 272 ms 18256 KB Output is correct
30 Correct 479 ms 20304 KB Output is correct
31 Correct 601 ms 20052 KB Output is correct
32 Correct 464 ms 20048 KB Output is correct
33 Correct 348 ms 19028 KB Output is correct
34 Correct 401 ms 19280 KB Output is correct
35 Correct 419 ms 19520 KB Output is correct
36 Correct 175 ms 18176 KB Output is correct
37 Correct 551 ms 20052 KB Output is correct
38 Correct 289 ms 18256 KB Output is correct
39 Correct 377 ms 19256 KB Output is correct
40 Correct 375 ms 19228 KB Output is correct
41 Correct 386 ms 16892 KB Output is correct
42 Correct 144 ms 17232 KB Output is correct
43 Correct 64 ms 16900 KB Output is correct
44 Correct 93 ms 16980 KB Output is correct
45 Correct 45 ms 16980 KB Output is correct
46 Correct 128 ms 16864 KB Output is correct
47 Correct 116 ms 16872 KB Output is correct
48 Correct 35 ms 17232 KB Output is correct
49 Correct 54 ms 16916 KB Output is correct
50 Correct 183 ms 16880 KB Output is correct
51 Correct 382 ms 17008 KB Output is correct
52 Correct 94 ms 16976 KB Output is correct
53 Correct 61 ms 16980 KB Output is correct
54 Correct 72 ms 16976 KB Output is correct
55 Correct 113 ms 16960 KB Output is correct
56 Correct 33 ms 16784 KB Output is correct
57 Correct 366 ms 16880 KB Output is correct
58 Correct 46 ms 16884 KB Output is correct
59 Correct 68 ms 16976 KB Output is correct
60 Correct 78 ms 16888 KB Output is correct
61 Correct 45 ms 16984 KB Output is correct
62 Correct 126 ms 16980 KB Output is correct
63 Correct 118 ms 16892 KB Output is correct
64 Correct 45 ms 16872 KB Output is correct
65 Correct 122 ms 16980 KB Output is correct
66 Correct 122 ms 16976 KB Output is correct
67 Correct 123 ms 16864 KB Output is correct
68 Correct 122 ms 16976 KB Output is correct
69 Correct 124 ms 16940 KB Output is correct
70 Correct 139 ms 16976 KB Output is correct
71 Execution timed out 2004 ms 25204 KB Time limit exceeded
72 Halted 0 ms 0 KB -