Submission #43297

# Submission time Handle Problem Language Result Execution time Memory
43297 2018-03-13T08:51:09 Z funcsr Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 65536 KB
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int L, Q;
char IN[(1<<20)+1];
int A[1<<20], B[1<<20], C[1<<20];
char qs[21];
int sub[20];

signed main() {
  scanf("%d %d", &L, &Q);
  scanf("%s", IN);
  rep(i, 1<<L) A[i] = (int)(IN[i]-'0');
  rep(i, 1<<L) B[i] = A[i], C[((1<<L)-1)^i] = A[i];
  rep(add, L) rep(S, 1<<L) if (((S>>add)&1)==0) B[S|(1<<add)] += B[S];
  rep(add, L) rep(S, 1<<L) if (((S>>add)&1)==0) C[S|(1<<add)] += C[S];

  rep(q, Q) {
    scanf("%s", qs);
    rep(i, L/2) swap(qs[i], qs[L-1-i]);
    int zero = 0, one = 0, any = 0;
    rep(i, L) {
      if      (qs[i] == '0') zero++;
      else if (qs[i] == '1') one++;
      else                   any++;
    }
    int s = 0;
    int minsz = min(zero, min(one, any));

    if (any == minsz) {
      int sz = 0, mask = 0;
      rep(i, L) {
        if (qs[i] == '?') sub[sz++] = 1<<i;
        else if (qs[i] == '1') mask |= 1<<i;
      }
      rep(w, 1<<sz) {
        int id = mask;
        rep(i, sz) if ((w>>i)&1) id |= sub[i];
        s += A[id];
      }
    }
    else if (one == minsz) {
      int sz = 0, mask = 0;
      rep(i, L) {
        if (qs[i] == '1') sub[sz++] = 1<<i;
        else if (qs[i] == '?') mask |= 1<<i;
      }
      rep(w, 1<<sz) {
        int id = mask, sgn = (sz-__builtin_popcount(w))&1;
        rep(i, sz) if ((w>>i)&1) id |= sub[i];
        if (sgn>0) s -= B[id];
        else s += B[id];
      }
    }
    else {
      int sz = 0, mask = 0;
      rep(i, L) {
        if (qs[i] == '0') sub[sz++] = 1<<i;
        else if (qs[i] == '?') mask |= 1<<i;
      }
      rep(w, 1<<sz) {
        int id = mask, sgn = (sz-__builtin_popcount(w))&1;
        rep(i, sz) if ((w>>i)&1) id |= sub[i];
        if (sgn>0) s -= C[id];
        else s += C[id];
      }
    }
    printf("%d\n", s);
  }
  return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:33:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &L, &Q);
                         ^
snake_escaping.cpp:34:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", IN);
                  ^
snake_escaping.cpp:41:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", qs);
                    ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 532 KB Output is correct
4 Correct 2 ms 624 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 2 ms 624 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 532 KB Output is correct
4 Correct 2 ms 624 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 2 ms 624 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 644 KB Output is correct
11 Correct 325 ms 15264 KB Output is correct
12 Correct 375 ms 25768 KB Output is correct
13 Correct 428 ms 35832 KB Output is correct
14 Correct 478 ms 46720 KB Output is correct
15 Correct 414 ms 58452 KB Output is correct
16 Correct 488 ms 65536 KB Output is correct
17 Correct 486 ms 65536 KB Output is correct
18 Correct 289 ms 65536 KB Output is correct
19 Correct 392 ms 65536 KB Output is correct
20 Correct 400 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 532 KB Output is correct
4 Correct 2 ms 624 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 2 ms 624 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 644 KB Output is correct
11 Correct 325 ms 15264 KB Output is correct
12 Correct 375 ms 25768 KB Output is correct
13 Correct 428 ms 35832 KB Output is correct
14 Correct 478 ms 46720 KB Output is correct
15 Correct 414 ms 58452 KB Output is correct
16 Correct 488 ms 65536 KB Output is correct
17 Correct 486 ms 65536 KB Output is correct
18 Correct 289 ms 65536 KB Output is correct
19 Correct 392 ms 65536 KB Output is correct
20 Correct 400 ms 65536 KB Output is correct
21 Correct 502 ms 65536 KB Output is correct
22 Correct 444 ms 65536 KB Output is correct
23 Correct 555 ms 65536 KB Output is correct
24 Correct 612 ms 65536 KB Output is correct
25 Correct 494 ms 65536 KB Output is correct
26 Correct 616 ms 65536 KB Output is correct
27 Correct 597 ms 65536 KB Output is correct
28 Correct 334 ms 65536 KB Output is correct
29 Correct 450 ms 65536 KB Output is correct
30 Correct 482 ms 65536 KB Output is correct
31 Correct 569 ms 65536 KB Output is correct
32 Correct 675 ms 65536 KB Output is correct
33 Correct 474 ms 65536 KB Output is correct
34 Correct 622 ms 65536 KB Output is correct
35 Correct 584 ms 65536 KB Output is correct
36 Correct 338 ms 65536 KB Output is correct
37 Correct 484 ms 65536 KB Output is correct
38 Correct 462 ms 65536 KB Output is correct
39 Correct 562 ms 65536 KB Output is correct
40 Correct 602 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 532 KB Output is correct
4 Correct 2 ms 624 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 2 ms 624 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 644 KB Output is correct
11 Correct 102 ms 65536 KB Output is correct
12 Correct 100 ms 65536 KB Output is correct
13 Correct 115 ms 65536 KB Output is correct
14 Correct 152 ms 65536 KB Output is correct
15 Correct 104 ms 65536 KB Output is correct
16 Correct 165 ms 65536 KB Output is correct
17 Correct 144 ms 65536 KB Output is correct
18 Correct 80 ms 65536 KB Output is correct
19 Correct 93 ms 65536 KB Output is correct
20 Correct 100 ms 65536 KB Output is correct
21 Correct 125 ms 65536 KB Output is correct
22 Correct 166 ms 65536 KB Output is correct
23 Correct 100 ms 65536 KB Output is correct
24 Correct 161 ms 65536 KB Output is correct
25 Correct 149 ms 65536 KB Output is correct
26 Correct 86 ms 65536 KB Output is correct
27 Correct 97 ms 65536 KB Output is correct
28 Correct 99 ms 65536 KB Output is correct
29 Correct 113 ms 65536 KB Output is correct
30 Correct 133 ms 65536 KB Output is correct
31 Correct 103 ms 65536 KB Output is correct
32 Correct 176 ms 65536 KB Output is correct
33 Correct 142 ms 65536 KB Output is correct
34 Correct 81 ms 65536 KB Output is correct
35 Correct 131 ms 65536 KB Output is correct
36 Correct 145 ms 65536 KB Output is correct
37 Correct 145 ms 65536 KB Output is correct
38 Correct 133 ms 65536 KB Output is correct
39 Correct 130 ms 65536 KB Output is correct
40 Correct 155 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 480 KB Output is correct
3 Correct 2 ms 532 KB Output is correct
4 Correct 2 ms 624 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 2 ms 624 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 644 KB Output is correct
11 Correct 325 ms 15264 KB Output is correct
12 Correct 375 ms 25768 KB Output is correct
13 Correct 428 ms 35832 KB Output is correct
14 Correct 478 ms 46720 KB Output is correct
15 Correct 414 ms 58452 KB Output is correct
16 Correct 488 ms 65536 KB Output is correct
17 Correct 486 ms 65536 KB Output is correct
18 Correct 289 ms 65536 KB Output is correct
19 Correct 392 ms 65536 KB Output is correct
20 Correct 400 ms 65536 KB Output is correct
21 Correct 502 ms 65536 KB Output is correct
22 Correct 444 ms 65536 KB Output is correct
23 Correct 555 ms 65536 KB Output is correct
24 Correct 612 ms 65536 KB Output is correct
25 Correct 494 ms 65536 KB Output is correct
26 Correct 616 ms 65536 KB Output is correct
27 Correct 597 ms 65536 KB Output is correct
28 Correct 334 ms 65536 KB Output is correct
29 Correct 450 ms 65536 KB Output is correct
30 Correct 482 ms 65536 KB Output is correct
31 Correct 569 ms 65536 KB Output is correct
32 Correct 675 ms 65536 KB Output is correct
33 Correct 474 ms 65536 KB Output is correct
34 Correct 622 ms 65536 KB Output is correct
35 Correct 584 ms 65536 KB Output is correct
36 Correct 338 ms 65536 KB Output is correct
37 Correct 484 ms 65536 KB Output is correct
38 Correct 462 ms 65536 KB Output is correct
39 Correct 562 ms 65536 KB Output is correct
40 Correct 602 ms 65536 KB Output is correct
41 Correct 102 ms 65536 KB Output is correct
42 Correct 100 ms 65536 KB Output is correct
43 Correct 115 ms 65536 KB Output is correct
44 Correct 152 ms 65536 KB Output is correct
45 Correct 104 ms 65536 KB Output is correct
46 Correct 165 ms 65536 KB Output is correct
47 Correct 144 ms 65536 KB Output is correct
48 Correct 80 ms 65536 KB Output is correct
49 Correct 93 ms 65536 KB Output is correct
50 Correct 100 ms 65536 KB Output is correct
51 Correct 125 ms 65536 KB Output is correct
52 Correct 166 ms 65536 KB Output is correct
53 Correct 100 ms 65536 KB Output is correct
54 Correct 161 ms 65536 KB Output is correct
55 Correct 149 ms 65536 KB Output is correct
56 Correct 86 ms 65536 KB Output is correct
57 Correct 97 ms 65536 KB Output is correct
58 Correct 99 ms 65536 KB Output is correct
59 Correct 113 ms 65536 KB Output is correct
60 Correct 133 ms 65536 KB Output is correct
61 Correct 103 ms 65536 KB Output is correct
62 Correct 176 ms 65536 KB Output is correct
63 Correct 142 ms 65536 KB Output is correct
64 Correct 81 ms 65536 KB Output is correct
65 Correct 131 ms 65536 KB Output is correct
66 Correct 145 ms 65536 KB Output is correct
67 Correct 145 ms 65536 KB Output is correct
68 Correct 133 ms 65536 KB Output is correct
69 Correct 130 ms 65536 KB Output is correct
70 Correct 155 ms 65536 KB Output is correct
71 Correct 761 ms 65536 KB Output is correct
72 Correct 728 ms 65536 KB Output is correct
73 Correct 1143 ms 65536 KB Output is correct
74 Execution timed out 2048 ms 65536 KB Time limit exceeded
75 Halted 0 ms 0 KB -