Submission #957941

# Submission time Handle Problem Language Result Execution time Memory
957941 2024-04-04T13:59:33 Z BhavayGoyal Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 42244 KB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset = 
            tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


// -------------------------------------------------- Main Code --------------------------------------------------

const int N = (1<<20)+5;
int n, q, arr[N], dp[N], dp2[N];

int getAns(string &s) {
    int num = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '?') {num = -1; break;}
        else num += (1<<(n-i-1))*(s[i]-'0');
    }
    if (num != -1) {
        return arr[num];
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '?') {
            s[i] = '1';
            ans += getAns(s);
            s[i] = '0';
            ans += getAns(s);
            s[i] = '?';
            break;
        }
    }

    return ans;
}

void sol() {
    cin >> n >> q;
    for (int i = 0; i < (1<<n); i++) {char x; cin >> x; arr[i] = x-'0';}

    for (int i = 0; i < (1<<n); i++) dp[i] = arr[i], dp2[i] = arr[i];
    for (int i = 0; i < n; i++) {
        for (int mask = 0; mask < (1<<n); mask++) {
            if (mask&(1<<i)) dp[mask] += dp[mask^(1<<i)];
            if ((mask&(1<<i)) == 0) dp2[mask] += dp2[mask^(1<<i)];
        }
    }


    while (q--) {
        string s; cin >> s;
        int one = 0, zero = 0, question = 0;
        for (int i = 0; i < n; i++) {
            one += s[i]=='1';
            zero += s[i]=='0';
            question += s[i]=='?';
        }

        if (zero <= 6) {
            vi temp;
            int num = 0;
            reverse(all(s));
            for (int i = 0; i < n; i++) {
                if (s[i] == '0') temp.pb(i);
                if (s[i] == '1') num += (1<<i);
            }

            int ans = 0;
            for (int i = 0; i < (1<<temp.size()); i++) {
                int tempNum = num, ct = 0;
                for (int j = 0; j < temp.size(); j++) {
                    if (!(i&(1<<j))) {
                        tempNum ^= (1<<temp[j]);
                        ct++;
                    }
                }
                if (ct%2 != 0) ans -= dp2[tempNum];
                else ans += dp2[tempNum];
            }

            cout << ans << endl;
        }
        else if (one <= 6) {
            vi temp;
            int num = 0;
            reverse(all(s));
            for (int i = 0; i < n; i++) {
                if (s[i] == '1') temp.pb(i);
                if (s[i] == '1' || s[i] == '?') num += (1<<i);
            }

            int ans = 0;
            for (int i = 0; i < (1<<temp.size()); i++) {
                int tempNum = num, ct = 0;
                for (int j = 0; j < temp.size(); j++) {
                    if (i&(1<<j)) {
                        tempNum ^= (1<<temp[j]);
                        ct++;
                    }
                }
                if (ct%2 != 0) ans -= dp[tempNum];
                else ans += dp[tempNum];
            }

            cout << ans << endl;
        }
        else {
            cout << getAns(s) << endl;
        }
    }
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    // cin >> t; 
    while (t--) {
        sol();
    }
    return 0;
}

Compilation message

snake_escaping.cpp: In function 'void sol()':
snake_escaping.cpp:97:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |                 for (int j = 0; j < temp.size(); j++) {
      |                                 ~~^~~~~~~~~~~~~
snake_escaping.cpp:121:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |                 for (int j = 0; j < temp.size(); j++) {
      |                                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4580 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4660 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4580 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4660 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 199 ms 8468 KB Output is correct
12 Correct 473 ms 8272 KB Output is correct
13 Correct 532 ms 7508 KB Output is correct
14 Correct 377 ms 7508 KB Output is correct
15 Correct 273 ms 8448 KB Output is correct
16 Correct 332 ms 7504 KB Output is correct
17 Correct 403 ms 7620 KB Output is correct
18 Correct 153 ms 9488 KB Output is correct
19 Correct 499 ms 6508 KB Output is correct
20 Correct 207 ms 8172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4580 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4660 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 199 ms 8468 KB Output is correct
12 Correct 473 ms 8272 KB Output is correct
13 Correct 532 ms 7508 KB Output is correct
14 Correct 377 ms 7508 KB Output is correct
15 Correct 273 ms 8448 KB Output is correct
16 Correct 332 ms 7504 KB Output is correct
17 Correct 403 ms 7620 KB Output is correct
18 Correct 153 ms 9488 KB Output is correct
19 Correct 499 ms 6508 KB Output is correct
20 Correct 207 ms 8172 KB Output is correct
21 Correct 250 ms 19664 KB Output is correct
22 Correct 463 ms 19956 KB Output is correct
23 Correct 962 ms 18772 KB Output is correct
24 Correct 594 ms 18688 KB Output is correct
25 Correct 334 ms 20564 KB Output is correct
26 Correct 444 ms 19240 KB Output is correct
27 Correct 523 ms 18904 KB Output is correct
28 Correct 197 ms 21568 KB Output is correct
29 Correct 902 ms 17664 KB Output is correct
30 Correct 244 ms 21064 KB Output is correct
31 Correct 443 ms 21348 KB Output is correct
32 Correct 427 ms 21328 KB Output is correct
33 Correct 336 ms 19796 KB Output is correct
34 Correct 550 ms 20144 KB Output is correct
35 Correct 542 ms 20352 KB Output is correct
36 Correct 161 ms 19024 KB Output is correct
37 Correct 221 ms 21072 KB Output is correct
38 Correct 690 ms 18988 KB Output is correct
39 Correct 451 ms 20104 KB Output is correct
40 Correct 414 ms 20052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4580 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4660 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 50 ms 13672 KB Output is correct
12 Correct 48 ms 14992 KB Output is correct
13 Correct 61 ms 14792 KB Output is correct
14 Correct 80 ms 14796 KB Output is correct
15 Correct 66 ms 14936 KB Output is correct
16 Correct 84 ms 14960 KB Output is correct
17 Correct 86 ms 14772 KB Output is correct
18 Correct 39 ms 15128 KB Output is correct
19 Correct 40 ms 14780 KB Output is correct
20 Correct 45 ms 14672 KB Output is correct
21 Correct 60 ms 14856 KB Output is correct
22 Correct 80 ms 14952 KB Output is correct
23 Correct 61 ms 14928 KB Output is correct
24 Correct 111 ms 14984 KB Output is correct
25 Correct 79 ms 14928 KB Output is correct
26 Correct 39 ms 14784 KB Output is correct
27 Correct 45 ms 14936 KB Output is correct
28 Correct 43 ms 14712 KB Output is correct
29 Correct 61 ms 14928 KB Output is correct
30 Correct 181 ms 14848 KB Output is correct
31 Correct 52 ms 14932 KB Output is correct
32 Correct 108 ms 14892 KB Output is correct
33 Correct 78 ms 14780 KB Output is correct
34 Correct 38 ms 14676 KB Output is correct
35 Correct 70 ms 14928 KB Output is correct
36 Correct 70 ms 14800 KB Output is correct
37 Correct 72 ms 14832 KB Output is correct
38 Correct 85 ms 14932 KB Output is correct
39 Correct 68 ms 14792 KB Output is correct
40 Correct 72 ms 14788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4580 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4660 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 199 ms 8468 KB Output is correct
12 Correct 473 ms 8272 KB Output is correct
13 Correct 532 ms 7508 KB Output is correct
14 Correct 377 ms 7508 KB Output is correct
15 Correct 273 ms 8448 KB Output is correct
16 Correct 332 ms 7504 KB Output is correct
17 Correct 403 ms 7620 KB Output is correct
18 Correct 153 ms 9488 KB Output is correct
19 Correct 499 ms 6508 KB Output is correct
20 Correct 207 ms 8172 KB Output is correct
21 Correct 250 ms 19664 KB Output is correct
22 Correct 463 ms 19956 KB Output is correct
23 Correct 962 ms 18772 KB Output is correct
24 Correct 594 ms 18688 KB Output is correct
25 Correct 334 ms 20564 KB Output is correct
26 Correct 444 ms 19240 KB Output is correct
27 Correct 523 ms 18904 KB Output is correct
28 Correct 197 ms 21568 KB Output is correct
29 Correct 902 ms 17664 KB Output is correct
30 Correct 244 ms 21064 KB Output is correct
31 Correct 443 ms 21348 KB Output is correct
32 Correct 427 ms 21328 KB Output is correct
33 Correct 336 ms 19796 KB Output is correct
34 Correct 550 ms 20144 KB Output is correct
35 Correct 542 ms 20352 KB Output is correct
36 Correct 161 ms 19024 KB Output is correct
37 Correct 221 ms 21072 KB Output is correct
38 Correct 690 ms 18988 KB Output is correct
39 Correct 451 ms 20104 KB Output is correct
40 Correct 414 ms 20052 KB Output is correct
41 Correct 50 ms 13672 KB Output is correct
42 Correct 48 ms 14992 KB Output is correct
43 Correct 61 ms 14792 KB Output is correct
44 Correct 80 ms 14796 KB Output is correct
45 Correct 66 ms 14936 KB Output is correct
46 Correct 84 ms 14960 KB Output is correct
47 Correct 86 ms 14772 KB Output is correct
48 Correct 39 ms 15128 KB Output is correct
49 Correct 40 ms 14780 KB Output is correct
50 Correct 45 ms 14672 KB Output is correct
51 Correct 60 ms 14856 KB Output is correct
52 Correct 80 ms 14952 KB Output is correct
53 Correct 61 ms 14928 KB Output is correct
54 Correct 111 ms 14984 KB Output is correct
55 Correct 79 ms 14928 KB Output is correct
56 Correct 39 ms 14784 KB Output is correct
57 Correct 45 ms 14936 KB Output is correct
58 Correct 43 ms 14712 KB Output is correct
59 Correct 61 ms 14928 KB Output is correct
60 Correct 181 ms 14848 KB Output is correct
61 Correct 52 ms 14932 KB Output is correct
62 Correct 108 ms 14892 KB Output is correct
63 Correct 78 ms 14780 KB Output is correct
64 Correct 38 ms 14676 KB Output is correct
65 Correct 70 ms 14928 KB Output is correct
66 Correct 70 ms 14800 KB Output is correct
67 Correct 72 ms 14832 KB Output is correct
68 Correct 85 ms 14932 KB Output is correct
69 Correct 68 ms 14792 KB Output is correct
70 Correct 72 ms 14788 KB Output is correct
71 Correct 356 ms 39284 KB Output is correct
72 Correct 431 ms 39320 KB Output is correct
73 Correct 736 ms 37872 KB Output is correct
74 Correct 1118 ms 38348 KB Output is correct
75 Correct 575 ms 40312 KB Output is correct
76 Correct 1114 ms 38652 KB Output is correct
77 Correct 1090 ms 38548 KB Output is correct
78 Correct 282 ms 42244 KB Output is correct
79 Correct 235 ms 36284 KB Output is correct
80 Correct 413 ms 39360 KB Output is correct
81 Correct 767 ms 39404 KB Output is correct
82 Correct 1178 ms 38132 KB Output is correct
83 Correct 572 ms 37460 KB Output is correct
84 Correct 1722 ms 38124 KB Output is correct
85 Correct 1210 ms 38596 KB Output is correct
86 Correct 247 ms 36180 KB Output is correct
87 Correct 405 ms 39248 KB Output is correct
88 Correct 306 ms 36304 KB Output is correct
89 Correct 720 ms 37988 KB Output is correct
90 Execution timed out 2049 ms 29836 KB Time limit exceeded
91 Halted 0 ms 0 KB -