답안 #1094864

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1094864 2024-09-30T17:10:44 Z cpptowin Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
918 ms 54608 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 5000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
    x += k;
    x %= mod;
    if(x < 0) x += mod;
}
void del(int &x, int k)
{
    x -= k;
    x %= mod;
    if(x < 0) x += mod;
}
vi aa,bb,cc;
int n,q,a[maxn],sub[maxn],super[maxn];
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> q;
    int m = (1 << n) - 1;
    fo(i,0,m)
    {
        char t;
        cin >> t;
        a[i] = t - '0';
        sub[i] = a[i];
        super[i] = a[i];
    }
    fo(i,0,n - 1) fod(mask,m,0) if(!bit(mask,i)) super[mask] += super[offbit(mask,i)];
    fo(i,0,n - 1) fo(mask,0,m) if(bit(mask,i)) sub[mask] += sub[offbit(mask,i)];
    while(q--)
    {
        string s;
        cin >> s;
        reverse(all(s));
        aa.clear();
        bb.clear();
        cc.clear();
        fo(i,0,ss(s) - 1) 
        {
            if(s[i] == '0') aa.pb(i);
            else if(s[i] == '1') bb.pb(i);
            else if(s[i] == '?') cc.pb(i);
        }
        int ans = 0;
        if(ss(cc) <= 6)
        {
            int vv = 0;
            for(int it : bb) vv = onbit(vv,it);
            fo(mask,0,(1 << ss(cc)) - 1)
            {
                int v1 = vv;
                fo(i,0,ss(cc) - 1) if(bit(mask,i)) 
                {
                    v1 = onbit(v1,cc[i]);
                }
                ans += a[v1];
            }
        }
        else if(ss(aa) <= 6)
        {
            int vv = 0;
            for(int it : bb) vv = onbit(vv,it);
            fo(mask,0,(1 << ss(aa)) - 1)
            {
                int v1 = vv;
                fo(i,0,ss(aa) - 1) if(bit(mask,i))
                {
                    v1 = onbit(v1,aa[i]);
                }
                if(bitcount(mask) & 1) ans -= super[v1];
                else ans += super[v1];
            }
        }
        else if(ss(bb) <= 6)
        {
            int vv = 0;
            for(int it : cc) vv = onbit(vv,it);
            fo(mask,0,(1 << ss(bb)) - 1)
            {
                int v1 = vv;
                fo(i,0,ss(bb) - 1) if(bit(mask,i))
                {
                    v1 = onbit(v1,bb[i]);
                }
                if(bitcount(mask) % 2 == ss(bb) % 2) ans += sub[v1];
                else ans -= sub[v1];
            }
        }
        cout << ans;en;
    }
}

Compilation message

snake_escaping.cpp:59:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   59 | main()
      | ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 288 ms 4472 KB Output is correct
12 Correct 327 ms 3924 KB Output is correct
13 Correct 199 ms 3408 KB Output is correct
14 Correct 189 ms 3488 KB Output is correct
15 Correct 550 ms 4432 KB Output is correct
16 Correct 254 ms 3668 KB Output is correct
17 Correct 258 ms 3624 KB Output is correct
18 Correct 109 ms 5332 KB Output is correct
19 Correct 142 ms 2388 KB Output is correct
20 Correct 326 ms 4016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 288 ms 4472 KB Output is correct
12 Correct 327 ms 3924 KB Output is correct
13 Correct 199 ms 3408 KB Output is correct
14 Correct 189 ms 3488 KB Output is correct
15 Correct 550 ms 4432 KB Output is correct
16 Correct 254 ms 3668 KB Output is correct
17 Correct 258 ms 3624 KB Output is correct
18 Correct 109 ms 5332 KB Output is correct
19 Correct 142 ms 2388 KB Output is correct
20 Correct 326 ms 4016 KB Output is correct
21 Correct 174 ms 4652 KB Output is correct
22 Correct 508 ms 4784 KB Output is correct
23 Correct 296 ms 3768 KB Output is correct
24 Correct 247 ms 3920 KB Output is correct
25 Correct 212 ms 5460 KB Output is correct
26 Correct 368 ms 4180 KB Output is correct
27 Correct 355 ms 3916 KB Output is correct
28 Correct 129 ms 6528 KB Output is correct
29 Correct 187 ms 2636 KB Output is correct
30 Correct 331 ms 4792 KB Output is correct
31 Correct 586 ms 4640 KB Output is correct
32 Correct 340 ms 4584 KB Output is correct
33 Correct 198 ms 3324 KB Output is correct
34 Correct 256 ms 3668 KB Output is correct
35 Correct 353 ms 3920 KB Output is correct
36 Correct 114 ms 2640 KB Output is correct
37 Correct 539 ms 4584 KB Output is correct
38 Correct 181 ms 2640 KB Output is correct
39 Correct 278 ms 3668 KB Output is correct
40 Correct 242 ms 3668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 57 ms 26116 KB Output is correct
12 Correct 62 ms 27220 KB Output is correct
13 Correct 69 ms 27088 KB Output is correct
14 Correct 92 ms 27128 KB Output is correct
15 Correct 63 ms 27216 KB Output is correct
16 Correct 92 ms 27220 KB Output is correct
17 Correct 81 ms 27220 KB Output is correct
18 Correct 54 ms 27680 KB Output is correct
19 Correct 59 ms 27216 KB Output is correct
20 Correct 60 ms 27380 KB Output is correct
21 Correct 72 ms 27216 KB Output is correct
22 Correct 87 ms 27080 KB Output is correct
23 Correct 61 ms 27224 KB Output is correct
24 Correct 82 ms 27220 KB Output is correct
25 Correct 80 ms 27216 KB Output is correct
26 Correct 55 ms 27020 KB Output is correct
27 Correct 57 ms 27332 KB Output is correct
28 Correct 61 ms 27116 KB Output is correct
29 Correct 70 ms 27312 KB Output is correct
30 Correct 82 ms 27132 KB Output is correct
31 Correct 63 ms 27216 KB Output is correct
32 Correct 96 ms 27368 KB Output is correct
33 Correct 83 ms 27132 KB Output is correct
34 Correct 52 ms 27132 KB Output is correct
35 Correct 76 ms 27220 KB Output is correct
36 Correct 75 ms 27220 KB Output is correct
37 Correct 78 ms 27116 KB Output is correct
38 Correct 77 ms 27216 KB Output is correct
39 Correct 77 ms 27288 KB Output is correct
40 Correct 76 ms 27272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 288 ms 4472 KB Output is correct
12 Correct 327 ms 3924 KB Output is correct
13 Correct 199 ms 3408 KB Output is correct
14 Correct 189 ms 3488 KB Output is correct
15 Correct 550 ms 4432 KB Output is correct
16 Correct 254 ms 3668 KB Output is correct
17 Correct 258 ms 3624 KB Output is correct
18 Correct 109 ms 5332 KB Output is correct
19 Correct 142 ms 2388 KB Output is correct
20 Correct 326 ms 4016 KB Output is correct
21 Correct 174 ms 4652 KB Output is correct
22 Correct 508 ms 4784 KB Output is correct
23 Correct 296 ms 3768 KB Output is correct
24 Correct 247 ms 3920 KB Output is correct
25 Correct 212 ms 5460 KB Output is correct
26 Correct 368 ms 4180 KB Output is correct
27 Correct 355 ms 3916 KB Output is correct
28 Correct 129 ms 6528 KB Output is correct
29 Correct 187 ms 2636 KB Output is correct
30 Correct 331 ms 4792 KB Output is correct
31 Correct 586 ms 4640 KB Output is correct
32 Correct 340 ms 4584 KB Output is correct
33 Correct 198 ms 3324 KB Output is correct
34 Correct 256 ms 3668 KB Output is correct
35 Correct 353 ms 3920 KB Output is correct
36 Correct 114 ms 2640 KB Output is correct
37 Correct 539 ms 4584 KB Output is correct
38 Correct 181 ms 2640 KB Output is correct
39 Correct 278 ms 3668 KB Output is correct
40 Correct 242 ms 3668 KB Output is correct
41 Correct 57 ms 26116 KB Output is correct
42 Correct 62 ms 27220 KB Output is correct
43 Correct 69 ms 27088 KB Output is correct
44 Correct 92 ms 27128 KB Output is correct
45 Correct 63 ms 27216 KB Output is correct
46 Correct 92 ms 27220 KB Output is correct
47 Correct 81 ms 27220 KB Output is correct
48 Correct 54 ms 27680 KB Output is correct
49 Correct 59 ms 27216 KB Output is correct
50 Correct 60 ms 27380 KB Output is correct
51 Correct 72 ms 27216 KB Output is correct
52 Correct 87 ms 27080 KB Output is correct
53 Correct 61 ms 27224 KB Output is correct
54 Correct 82 ms 27220 KB Output is correct
55 Correct 80 ms 27216 KB Output is correct
56 Correct 55 ms 27020 KB Output is correct
57 Correct 57 ms 27332 KB Output is correct
58 Correct 61 ms 27116 KB Output is correct
59 Correct 70 ms 27312 KB Output is correct
60 Correct 82 ms 27132 KB Output is correct
61 Correct 63 ms 27216 KB Output is correct
62 Correct 96 ms 27368 KB Output is correct
63 Correct 83 ms 27132 KB Output is correct
64 Correct 52 ms 27132 KB Output is correct
65 Correct 76 ms 27220 KB Output is correct
66 Correct 75 ms 27220 KB Output is correct
67 Correct 78 ms 27116 KB Output is correct
68 Correct 77 ms 27216 KB Output is correct
69 Correct 77 ms 27288 KB Output is correct
70 Correct 76 ms 27272 KB Output is correct
71 Correct 329 ms 51540 KB Output is correct
72 Correct 379 ms 51540 KB Output is correct
73 Correct 531 ms 50256 KB Output is correct
74 Correct 918 ms 50616 KB Output is correct
75 Correct 427 ms 52456 KB Output is correct
76 Correct 859 ms 50896 KB Output is correct
77 Correct 732 ms 50768 KB Output is correct
78 Correct 221 ms 54608 KB Output is correct
79 Correct 306 ms 48464 KB Output is correct
80 Correct 369 ms 51696 KB Output is correct
81 Correct 727 ms 51552 KB Output is correct
82 Correct 888 ms 50424 KB Output is correct
83 Correct 377 ms 49780 KB Output is correct
84 Correct 839 ms 50516 KB Output is correct
85 Correct 783 ms 50692 KB Output is correct
86 Correct 201 ms 48484 KB Output is correct
87 Correct 315 ms 51540 KB Output is correct
88 Correct 320 ms 48464 KB Output is correct
89 Correct 545 ms 50260 KB Output is correct
90 Correct 789 ms 50512 KB Output is correct
91 Correct 405 ms 49644 KB Output is correct
92 Correct 890 ms 50920 KB Output is correct
93 Correct 739 ms 50628 KB Output is correct
94 Correct 201 ms 48424 KB Output is correct
95 Correct 621 ms 50512 KB Output is correct
96 Correct 618 ms 50532 KB Output is correct
97 Correct 643 ms 50548 KB Output is correct
98 Correct 649 ms 50596 KB Output is correct
99 Correct 693 ms 50732 KB Output is correct
100 Correct 638 ms 50528 KB Output is correct