Submission #1003659

# Submission time Handle Problem Language Result Execution time Memory
1003659 2024-06-20T14:45:59 Z GrindMachine Amusement Park (CEOI19_amusementpark) C++17
100 / 100
1237 ms 201100 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

/*

read the solution a long time ago and remember some ideas from there

*/

const int MOD = 998244353;
const int N = 20 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

// https://gist.github.com/Chillee/3bd6ee4429abb4a2e7c19959fb1490ae#file-hash-table-cpp
struct chash {
    const int RANDOM = (long long)(make_unique<char>().get()) ^ chrono::high_resolution_clock::now().time_since_epoch().count();
    static unsigned long long hash_f(unsigned long long x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    static unsigned hash_combine(unsigned a, unsigned b) { return a * 31 + b; }
    int operator()(int x) const { return hash_f(x)^RANDOM; }
};

vector<pii> adj[N];

void solve(int test_case)
{
    int n,m; cin >> n >> m;
    rep1(i,m){
        int u,v; cin >> u >> v;
        u--, v--;
        adj[u].pb({v,0}), adj[v].pb({u,1});
    }

    gp_hash_table<int,int,chash> dp[1<<n];
    dp[0][0] = 1;

    rep(mask,1<<n){
        ll unset = ((1<<n)-1)^mask;
        for(auto [bad,x] : dp[mask]){
            rep(u,n){
                if(mask&(1<<u) or bad&(1<<u)) conts;
                int mask2 = mask|(1<<u);
                int bad2 = bad;
                bad2 |= (1<<u)-1;
                bad2 &= unset;

                for(auto [v,t] : adj[u]){
                    if(mask&(1<<v)) conts;
                    if(bad2&(1<<v)){
                        bad2 ^= (1<<v);
                    }
                }

                dp[mask2][bad2] += x;
                dp[mask2][bad2] %= MOD;
            }
        }
    }

    ll ans = dp[(1<<n)-1][0];
    ans = ans*m%MOD*((MOD+1)/2)%MOD;
    cout << ans << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 412 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 552 KB Output is correct
52 Correct 0 ms 604 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 1 ms 604 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 1 ms 604 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 1 ms 604 KB Output is correct
63 Correct 1 ms 604 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 412 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 552 KB Output is correct
52 Correct 0 ms 604 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 1 ms 604 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 1 ms 604 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 1 ms 604 KB Output is correct
63 Correct 1 ms 604 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
65 Correct 25 ms 6492 KB Output is correct
66 Correct 35 ms 7272 KB Output is correct
67 Correct 33 ms 6996 KB Output is correct
68 Correct 38 ms 6868 KB Output is correct
69 Correct 9 ms 5212 KB Output is correct
70 Correct 13 ms 4956 KB Output is correct
71 Correct 12 ms 4856 KB Output is correct
72 Correct 10 ms 4700 KB Output is correct
73 Correct 13 ms 4696 KB Output is correct
74 Correct 13 ms 4700 KB Output is correct
75 Correct 13 ms 4724 KB Output is correct
76 Correct 13 ms 4740 KB Output is correct
77 Correct 48 ms 13764 KB Output is correct
78 Correct 66 ms 14420 KB Output is correct
79 Correct 85 ms 16724 KB Output is correct
80 Correct 72 ms 14740 KB Output is correct
81 Correct 89 ms 15592 KB Output is correct
82 Correct 86 ms 14416 KB Output is correct
83 Correct 81 ms 14112 KB Output is correct
84 Correct 68 ms 11600 KB Output is correct
85 Correct 17 ms 10076 KB Output is correct
86 Correct 27 ms 9956 KB Output is correct
87 Correct 24 ms 9820 KB Output is correct
88 Correct 25 ms 9308 KB Output is correct
89 Correct 32 ms 9560 KB Output is correct
90 Correct 31 ms 9552 KB Output is correct
91 Correct 30 ms 9564 KB Output is correct
92 Correct 28 ms 9512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 412 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 552 KB Output is correct
52 Correct 0 ms 604 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 1 ms 604 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 1 ms 604 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 1 ms 604 KB Output is correct
63 Correct 1 ms 604 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
65 Correct 25 ms 6492 KB Output is correct
66 Correct 35 ms 7272 KB Output is correct
67 Correct 33 ms 6996 KB Output is correct
68 Correct 38 ms 6868 KB Output is correct
69 Correct 9 ms 5212 KB Output is correct
70 Correct 13 ms 4956 KB Output is correct
71 Correct 12 ms 4856 KB Output is correct
72 Correct 10 ms 4700 KB Output is correct
73 Correct 13 ms 4696 KB Output is correct
74 Correct 13 ms 4700 KB Output is correct
75 Correct 13 ms 4724 KB Output is correct
76 Correct 13 ms 4740 KB Output is correct
77 Correct 48 ms 13764 KB Output is correct
78 Correct 66 ms 14420 KB Output is correct
79 Correct 85 ms 16724 KB Output is correct
80 Correct 72 ms 14740 KB Output is correct
81 Correct 89 ms 15592 KB Output is correct
82 Correct 86 ms 14416 KB Output is correct
83 Correct 81 ms 14112 KB Output is correct
84 Correct 68 ms 11600 KB Output is correct
85 Correct 17 ms 10076 KB Output is correct
86 Correct 27 ms 9956 KB Output is correct
87 Correct 24 ms 9820 KB Output is correct
88 Correct 25 ms 9308 KB Output is correct
89 Correct 32 ms 9560 KB Output is correct
90 Correct 31 ms 9552 KB Output is correct
91 Correct 30 ms 9564 KB Output is correct
92 Correct 28 ms 9512 KB Output is correct
93 Correct 251 ms 81680 KB Output is correct
94 Correct 103 ms 63056 KB Output is correct
95 Correct 75 ms 58840 KB Output is correct
96 Correct 325 ms 80328 KB Output is correct
97 Correct 311 ms 79956 KB Output is correct
98 Correct 259 ms 80980 KB Output is correct
99 Correct 62 ms 23376 KB Output is correct
100 Correct 112 ms 29668 KB Output is correct
101 Correct 169 ms 35580 KB Output is correct
102 Correct 193 ms 36632 KB Output is correct
103 Correct 30 ms 29264 KB Output is correct
104 Correct 34 ms 29016 KB Output is correct
105 Correct 76 ms 34640 KB Output is correct
106 Correct 243 ms 59732 KB Output is correct
107 Correct 56 ms 57680 KB Output is correct
108 Correct 172 ms 72248 KB Output is correct
109 Correct 504 ms 127824 KB Output is correct
110 Correct 1125 ms 201100 KB Output is correct
111 Correct 202 ms 36320 KB Output is correct
112 Correct 166 ms 31120 KB Output is correct
113 Correct 199 ms 30316 KB Output is correct
114 Correct 39 ms 20312 KB Output is correct
115 Correct 55 ms 20052 KB Output is correct
116 Correct 57 ms 19796 KB Output is correct
117 Correct 57 ms 18768 KB Output is correct
118 Correct 74 ms 19536 KB Output is correct
119 Correct 69 ms 19288 KB Output is correct
120 Correct 68 ms 19280 KB Output is correct
121 Correct 66 ms 19284 KB Output is correct
122 Correct 322 ms 65620 KB Output is correct
123 Correct 474 ms 80468 KB Output is correct
124 Correct 490 ms 76368 KB Output is correct
125 Correct 456 ms 70992 KB Output is correct
126 Correct 454 ms 64808 KB Output is correct
127 Correct 347 ms 50948 KB Output is correct
128 Correct 247 ms 39684 KB Output is correct
129 Correct 193 ms 32200 KB Output is correct
130 Correct 93 ms 29016 KB Output is correct
131 Correct 88 ms 41044 KB Output is correct
132 Correct 126 ms 40448 KB Output is correct
133 Correct 117 ms 40016 KB Output is correct
134 Correct 108 ms 37716 KB Output is correct
135 Correct 148 ms 39504 KB Output is correct
136 Correct 138 ms 39252 KB Output is correct
137 Correct 147 ms 39272 KB Output is correct
138 Correct 134 ms 38480 KB Output is correct
139 Correct 154 ms 37456 KB Output is correct
140 Correct 154 ms 38512 KB Output is correct
141 Correct 145 ms 38484 KB Output is correct
142 Correct 1043 ms 173284 KB Output is correct
143 Correct 1097 ms 163920 KB Output is correct
144 Correct 933 ms 127324 KB Output is correct
145 Correct 768 ms 105696 KB Output is correct
146 Correct 474 ms 73040 KB Output is correct
147 Correct 194 ms 57684 KB Output is correct
148 Correct 1062 ms 166228 KB Output is correct
149 Correct 1086 ms 163656 KB Output is correct
150 Correct 1237 ms 180860 KB Output is correct
151 Correct 216 ms 72656 KB Output is correct
152 Correct 230 ms 76104 KB Output is correct
153 Correct 313 ms 80464 KB Output is correct
154 Correct 312 ms 80464 KB Output is correct
155 Correct 304 ms 79588 KB Output is correct
156 Correct 279 ms 77828 KB Output is correct
157 Correct 294 ms 79696 KB Output is correct
158 Correct 288 ms 79440 KB Output is correct
159 Correct 346 ms 76116 KB Output is correct
160 Correct 328 ms 77908 KB Output is correct
161 Correct 310 ms 78160 KB Output is correct
162 Correct 384 ms 75860 KB Output is correct
163 Correct 366 ms 74908 KB Output is correct
164 Correct 376 ms 75856 KB Output is correct
165 Correct 338 ms 75816 KB Output is correct
166 Correct 327 ms 75856 KB Output is correct
167 Correct 342 ms 77652 KB Output is correct