Submission #817534

# Submission time Handle Problem Language Result Execution time Memory
817534 2023-08-09T13:21:10 Z kwongweng Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 36312 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ll, int> li;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
 
int main(){
  ios::sync_with_stdio(false);
  if (fopen("input.txt", "r")){
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  int L, Q; cin>>L>>Q;
  string s; cin >> s;
  int dp[1<<L]; FOR(i,0,(1<<L)) dp[i]=s[i]-'0';
  int dp2[1<<L]; FOR(i,0,(1<<L)) dp2[i]=s[(1<<L)-1-i]-'0';
  FOR(i,0,L){
    FOR(mask,0,(1<<L)){
      if (mask & (1<<i)) {dp[mask] += dp[mask-(1<<i)]; dp2[mask] += dp2[mask-(1<<i)];}
    }
  }
  FOR(i,0,Q){
    string t; cin >> t; reverse(t.begin(), t.end());
    int cnt1=0, cnt2=0; FOR(j,0,L) {cnt1 += (t[j]=='?'); cnt2 += (t[j]=='1');}
    if (cnt1 <= 6){
      int ans = 0;
      vi a; int cur = 0;
      FOR(j,0,L){
        if (t[j]=='?') a.pb(j);
        else cur += (1<<j)*(t[j]-'0');
      }
      FOR(j,0,(1<<cnt1)){
        int val=0; FOR(k,0,cnt1){
          if (j & (1<<k)) val ^= (1<<a[k]);
        }
        //cout<<cur+val<<" ";
        ans+=s[cur+val]-'0';
      }
      cout<<ans<<'\n'; continue;
    }
    if (cnt2 <= 6){
      vi a; int cur=0;
      FOR(j,0,L){
        if (t[j]=='1') {a.pb(j); cur += (1<<j);}
        if (t[j]=='?') cur += (1<<j);
      }
      int ans = 0;
      FOR(j,0,(1<<cnt2)){
        int val=0; int num=0; FOR(k,0,cnt2){
          if (j & (1<<k)) {val ^= (1<<a[k]); num^=1;}
        }
        if (num) ans -= dp[cur-val];
        else ans += dp[cur-val];
      }
      cout<<ans<<'\n'; continue;
    }
    int cnt3=L-cnt1-cnt2; vi a; int cur=0;
    FOR(j,0,L){
      if (t[j] != '1') cur+= (1<<j);
      if (t[j] == '0') a.pb(j);
    }
    int ans=0;
    FOR(j,0,(1<<cnt3)){
      int val=0,num=0; FOR(k,0,cnt3){
        if (j & (1<<k)) {val ^= (1<<a[k]); num^=1;}
      }
      if (num) ans-=dp2[cur-val];
      else ans+=dp2[cur-val];
    }
    cout<<ans<<'\n';
  }
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 2 ms 212 KB Output is correct
6 Correct 2 ms 212 KB Output is correct
7 Correct 2 ms 212 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 2 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 2 ms 212 KB Output is correct
6 Correct 2 ms 212 KB Output is correct
7 Correct 2 ms 212 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 2 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 1565 ms 4492 KB Output is correct
12 Correct 1506 ms 3876 KB Output is correct
13 Correct 1304 ms 3108 KB Output is correct
14 Correct 1406 ms 3228 KB Output is correct
15 Correct 1929 ms 4292 KB Output is correct
16 Correct 1416 ms 3472 KB Output is correct
17 Correct 1387 ms 3400 KB Output is correct
18 Correct 1144 ms 5228 KB Output is correct
19 Correct 1137 ms 2296 KB Output is correct
20 Correct 1517 ms 3916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 2 ms 212 KB Output is correct
6 Correct 2 ms 212 KB Output is correct
7 Correct 2 ms 212 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 2 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 1565 ms 4492 KB Output is correct
12 Correct 1506 ms 3876 KB Output is correct
13 Correct 1304 ms 3108 KB Output is correct
14 Correct 1406 ms 3228 KB Output is correct
15 Correct 1929 ms 4292 KB Output is correct
16 Correct 1416 ms 3472 KB Output is correct
17 Correct 1387 ms 3400 KB Output is correct
18 Correct 1144 ms 5228 KB Output is correct
19 Correct 1137 ms 2296 KB Output is correct
20 Correct 1517 ms 3916 KB Output is correct
21 Correct 1883 ms 4308 KB Output is correct
22 Correct 1468 ms 18212 KB Output is correct
23 Correct 1416 ms 17148 KB Output is correct
24 Correct 1384 ms 17076 KB Output is correct
25 Correct 1387 ms 19148 KB Output is correct
26 Correct 1514 ms 17644 KB Output is correct
27 Correct 1501 ms 17484 KB Output is correct
28 Correct 1161 ms 20132 KB Output is correct
29 Correct 1146 ms 15968 KB Output is correct
30 Correct 1736 ms 18224 KB Output is correct
31 Correct 1955 ms 18188 KB Output is correct
32 Correct 1592 ms 18108 KB Output is correct
33 Correct 1316 ms 16952 KB Output is correct
34 Correct 1386 ms 17056 KB Output is correct
35 Correct 1494 ms 17540 KB Output is correct
36 Correct 1134 ms 16048 KB Output is correct
37 Correct 1946 ms 18132 KB Output is correct
38 Correct 1153 ms 16124 KB Output is correct
39 Correct 1443 ms 17248 KB Output is correct
40 Correct 1389 ms 17068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 2 ms 212 KB Output is correct
6 Correct 2 ms 212 KB Output is correct
7 Correct 2 ms 212 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 2 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 94 ms 9908 KB Output is correct
12 Correct 100 ms 9852 KB Output is correct
13 Correct 113 ms 9812 KB Output is correct
14 Correct 130 ms 9836 KB Output is correct
15 Correct 105 ms 9884 KB Output is correct
16 Correct 124 ms 9840 KB Output is correct
17 Correct 128 ms 9968 KB Output is correct
18 Correct 89 ms 10060 KB Output is correct
19 Correct 91 ms 9720 KB Output is correct
20 Correct 98 ms 9836 KB Output is correct
21 Correct 113 ms 9872 KB Output is correct
22 Correct 139 ms 9796 KB Output is correct
23 Correct 105 ms 9720 KB Output is correct
24 Correct 130 ms 9820 KB Output is correct
25 Correct 123 ms 9760 KB Output is correct
26 Correct 88 ms 9664 KB Output is correct
27 Correct 97 ms 9880 KB Output is correct
28 Correct 89 ms 9692 KB Output is correct
29 Correct 109 ms 9804 KB Output is correct
30 Correct 130 ms 9804 KB Output is correct
31 Correct 108 ms 9828 KB Output is correct
32 Correct 127 ms 9864 KB Output is correct
33 Correct 119 ms 9824 KB Output is correct
34 Correct 89 ms 9692 KB Output is correct
35 Correct 117 ms 10196 KB Output is correct
36 Correct 114 ms 9796 KB Output is correct
37 Correct 110 ms 9852 KB Output is correct
38 Correct 133 ms 9812 KB Output is correct
39 Correct 112 ms 9804 KB Output is correct
40 Correct 130 ms 9808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 2 ms 212 KB Output is correct
6 Correct 2 ms 212 KB Output is correct
7 Correct 2 ms 212 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 2 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 1565 ms 4492 KB Output is correct
12 Correct 1506 ms 3876 KB Output is correct
13 Correct 1304 ms 3108 KB Output is correct
14 Correct 1406 ms 3228 KB Output is correct
15 Correct 1929 ms 4292 KB Output is correct
16 Correct 1416 ms 3472 KB Output is correct
17 Correct 1387 ms 3400 KB Output is correct
18 Correct 1144 ms 5228 KB Output is correct
19 Correct 1137 ms 2296 KB Output is correct
20 Correct 1517 ms 3916 KB Output is correct
21 Correct 1883 ms 4308 KB Output is correct
22 Correct 1468 ms 18212 KB Output is correct
23 Correct 1416 ms 17148 KB Output is correct
24 Correct 1384 ms 17076 KB Output is correct
25 Correct 1387 ms 19148 KB Output is correct
26 Correct 1514 ms 17644 KB Output is correct
27 Correct 1501 ms 17484 KB Output is correct
28 Correct 1161 ms 20132 KB Output is correct
29 Correct 1146 ms 15968 KB Output is correct
30 Correct 1736 ms 18224 KB Output is correct
31 Correct 1955 ms 18188 KB Output is correct
32 Correct 1592 ms 18108 KB Output is correct
33 Correct 1316 ms 16952 KB Output is correct
34 Correct 1386 ms 17056 KB Output is correct
35 Correct 1494 ms 17540 KB Output is correct
36 Correct 1134 ms 16048 KB Output is correct
37 Correct 1946 ms 18132 KB Output is correct
38 Correct 1153 ms 16124 KB Output is correct
39 Correct 1443 ms 17248 KB Output is correct
40 Correct 1389 ms 17068 KB Output is correct
41 Correct 94 ms 9908 KB Output is correct
42 Correct 100 ms 9852 KB Output is correct
43 Correct 113 ms 9812 KB Output is correct
44 Correct 130 ms 9836 KB Output is correct
45 Correct 105 ms 9884 KB Output is correct
46 Correct 124 ms 9840 KB Output is correct
47 Correct 128 ms 9968 KB Output is correct
48 Correct 89 ms 10060 KB Output is correct
49 Correct 91 ms 9720 KB Output is correct
50 Correct 98 ms 9836 KB Output is correct
51 Correct 113 ms 9872 KB Output is correct
52 Correct 139 ms 9796 KB Output is correct
53 Correct 105 ms 9720 KB Output is correct
54 Correct 130 ms 9820 KB Output is correct
55 Correct 123 ms 9760 KB Output is correct
56 Correct 88 ms 9664 KB Output is correct
57 Correct 97 ms 9880 KB Output is correct
58 Correct 89 ms 9692 KB Output is correct
59 Correct 109 ms 9804 KB Output is correct
60 Correct 130 ms 9804 KB Output is correct
61 Correct 108 ms 9828 KB Output is correct
62 Correct 127 ms 9864 KB Output is correct
63 Correct 119 ms 9824 KB Output is correct
64 Correct 89 ms 9692 KB Output is correct
65 Correct 117 ms 10196 KB Output is correct
66 Correct 114 ms 9796 KB Output is correct
67 Correct 110 ms 9852 KB Output is correct
68 Correct 133 ms 9812 KB Output is correct
69 Correct 112 ms 9804 KB Output is correct
70 Correct 130 ms 9808 KB Output is correct
71 Correct 1382 ms 36220 KB Output is correct
72 Correct 1488 ms 36312 KB Output is correct
73 Correct 1688 ms 34928 KB Output is correct
74 Execution timed out 2061 ms 34768 KB Time limit exceeded
75 Halted 0 ms 0 KB -