Submission #486716

# Submission time Handle Problem Language Result Execution time Memory
486716 2021-11-12T13:12:24 Z XII Amusement Park (CEOI19_amusementpark) C++17
100 / 100
302 ms 39388 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "CEOI19_amusementpark"
void Fi(){
    if(fopen(PROB".inp", "r")){
        freopen(PROB".inp", "r", stdin);
        freopen(PROB".out", "w", stdout);
    }
}

const int N = 18;
const int MOD = 998244353;
int n, m;
int edge[N * N];

int f[1 << N][N + 1]; /// number of not connected graph
int dp[1 << N][N + 1]; /// total number of graph

bool noEdge(const int &mask){
    FOR(i, 0, m) if((mask & edge[i]) == edge[i]) return false;
    return true;
}

void addto(int &a, const int &b){
    a = (a + b >= MOD ? a + b - MOD : a + b);
}

void subto(int &a, const int &b){
    a = (a - b < 0 ? a - b + MOD : a - b);
}

int mul(const int &a, const int &b){
    return (1LL * a * b) % MOD;
}

int main(){
    IOS;
    Fi();
    cin >> n >> m;
    FOR(i, 0, m){
        int u, v; cin >> u >> v;
        --u, --v;
        edge[i] = (1 << u) | (1 << v);
    }

    FOR(mask, 0, 1 << n) if(noEdge(mask)){
        int nbit = __builtin_popcount(mask);
        f[mask][nbit] = ((nbit & 1) ? 1 : MOD - 1);
    } /// init the no edge mask

    for(int i = 1; i < (1 << n); i <<= 1){ /// can remove bit 0...(log2(i))
        for(int j = 0; j < (1 << n); j += (i << 1)){
            FOR(k, 0, i) FORU(p, 0, n) addto(f[j | i | k][p], f[j | k][p]);
        }
    }
    /// calculate f[mask][i]: number of distinct subset of [mask] have [i] bit and not connected
    /// use -1 with odd and even +1 to use inclusive and exclusive for calculate dp later

    FOR(mask, 0, 1 << n){
        dp[mask][0] = 1;
        FORU(i, 1, n) FOR(j, 0, i){
            addto(dp[mask][i], mul(dp[mask][j], f[mask][i - j]));
        }
    }
    /// calculate dp[mask][i]: total number of distinct subset of [mask] have [i] bit

    for(int i = 1; i < (1 << n); i <<= 1){
        for(int j = 0; j < (1 << n); j += (i << 1)){
            FOR(k, 0, i) FORU(p, 0, n) subto(dp[j | i | k][p], dp[j | k][p]);
        }
    }
    /// subtract to get the answer for each distinct set

    int ans = dp[(1 << n) - 1][n];
    ans = mul(ans, m); /// there are m edges
    ans = mul(ans, (MOD + 1) / 2); /// devide by 2 (the not reverse edges)
    cout << ans;

    return 0;
}
/// Idea stolen

Compilation message

amusementpark.cpp: In function 'void Fi()':
amusementpark.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen(PROB".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
amusementpark.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen(PROB".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 252 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 0 ms 332 KB Output is correct
38 Correct 0 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 284 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 0 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 0 ms 332 KB Output is correct
51 Correct 0 ms 332 KB Output is correct
52 Correct 1 ms 460 KB Output is correct
53 Correct 1 ms 460 KB Output is correct
54 Correct 1 ms 460 KB Output is correct
55 Correct 1 ms 460 KB Output is correct
56 Correct 1 ms 460 KB Output is correct
57 Correct 1 ms 460 KB Output is correct
58 Correct 1 ms 460 KB Output is correct
59 Correct 1 ms 460 KB Output is correct
60 Correct 1 ms 460 KB Output is correct
61 Correct 1 ms 460 KB Output is correct
62 Correct 1 ms 460 KB Output is correct
63 Correct 1 ms 460 KB Output is correct
64 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 252 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 0 ms 332 KB Output is correct
38 Correct 0 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 284 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 0 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 0 ms 332 KB Output is correct
51 Correct 0 ms 332 KB Output is correct
52 Correct 1 ms 460 KB Output is correct
53 Correct 1 ms 460 KB Output is correct
54 Correct 1 ms 460 KB Output is correct
55 Correct 1 ms 460 KB Output is correct
56 Correct 1 ms 460 KB Output is correct
57 Correct 1 ms 460 KB Output is correct
58 Correct 1 ms 460 KB Output is correct
59 Correct 1 ms 460 KB Output is correct
60 Correct 1 ms 460 KB Output is correct
61 Correct 1 ms 460 KB Output is correct
62 Correct 1 ms 460 KB Output is correct
63 Correct 1 ms 460 KB Output is correct
64 Correct 1 ms 460 KB Output is correct
65 Correct 10 ms 2744 KB Output is correct
66 Correct 9 ms 2716 KB Output is correct
67 Correct 10 ms 2764 KB Output is correct
68 Correct 10 ms 2764 KB Output is correct
69 Correct 10 ms 2764 KB Output is correct
70 Correct 11 ms 2764 KB Output is correct
71 Correct 13 ms 2764 KB Output is correct
72 Correct 12 ms 2764 KB Output is correct
73 Correct 10 ms 2728 KB Output is correct
74 Correct 10 ms 2764 KB Output is correct
75 Correct 11 ms 2764 KB Output is correct
76 Correct 10 ms 2740 KB Output is correct
77 Correct 21 ms 5084 KB Output is correct
78 Correct 21 ms 5088 KB Output is correct
79 Correct 25 ms 5136 KB Output is correct
80 Correct 23 ms 5200 KB Output is correct
81 Correct 21 ms 5196 KB Output is correct
82 Correct 22 ms 5204 KB Output is correct
83 Correct 22 ms 5156 KB Output is correct
84 Correct 29 ms 5080 KB Output is correct
85 Correct 23 ms 5156 KB Output is correct
86 Correct 21 ms 5128 KB Output is correct
87 Correct 22 ms 5112 KB Output is correct
88 Correct 22 ms 5204 KB Output is correct
89 Correct 24 ms 5316 KB Output is correct
90 Correct 22 ms 5116 KB Output is correct
91 Correct 21 ms 5180 KB Output is correct
92 Correct 21 ms 5180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 252 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 0 ms 332 KB Output is correct
38 Correct 0 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 284 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 0 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 0 ms 332 KB Output is correct
51 Correct 0 ms 332 KB Output is correct
52 Correct 1 ms 460 KB Output is correct
53 Correct 1 ms 460 KB Output is correct
54 Correct 1 ms 460 KB Output is correct
55 Correct 1 ms 460 KB Output is correct
56 Correct 1 ms 460 KB Output is correct
57 Correct 1 ms 460 KB Output is correct
58 Correct 1 ms 460 KB Output is correct
59 Correct 1 ms 460 KB Output is correct
60 Correct 1 ms 460 KB Output is correct
61 Correct 1 ms 460 KB Output is correct
62 Correct 1 ms 460 KB Output is correct
63 Correct 1 ms 460 KB Output is correct
64 Correct 1 ms 460 KB Output is correct
65 Correct 10 ms 2744 KB Output is correct
66 Correct 9 ms 2716 KB Output is correct
67 Correct 10 ms 2764 KB Output is correct
68 Correct 10 ms 2764 KB Output is correct
69 Correct 10 ms 2764 KB Output is correct
70 Correct 11 ms 2764 KB Output is correct
71 Correct 13 ms 2764 KB Output is correct
72 Correct 12 ms 2764 KB Output is correct
73 Correct 10 ms 2728 KB Output is correct
74 Correct 10 ms 2764 KB Output is correct
75 Correct 11 ms 2764 KB Output is correct
76 Correct 10 ms 2740 KB Output is correct
77 Correct 21 ms 5084 KB Output is correct
78 Correct 21 ms 5088 KB Output is correct
79 Correct 25 ms 5136 KB Output is correct
80 Correct 23 ms 5200 KB Output is correct
81 Correct 21 ms 5196 KB Output is correct
82 Correct 22 ms 5204 KB Output is correct
83 Correct 22 ms 5156 KB Output is correct
84 Correct 29 ms 5080 KB Output is correct
85 Correct 23 ms 5156 KB Output is correct
86 Correct 21 ms 5128 KB Output is correct
87 Correct 22 ms 5112 KB Output is correct
88 Correct 22 ms 5204 KB Output is correct
89 Correct 24 ms 5316 KB Output is correct
90 Correct 22 ms 5116 KB Output is correct
91 Correct 21 ms 5180 KB Output is correct
92 Correct 21 ms 5180 KB Output is correct
93 Correct 249 ms 39276 KB Output is correct
94 Correct 242 ms 39236 KB Output is correct
95 Correct 242 ms 39236 KB Output is correct
96 Correct 255 ms 39276 KB Output is correct
97 Correct 239 ms 39212 KB Output is correct
98 Correct 247 ms 39260 KB Output is correct
99 Correct 47 ms 10056 KB Output is correct
100 Correct 54 ms 9972 KB Output is correct
101 Correct 50 ms 10024 KB Output is correct
102 Correct 48 ms 10040 KB Output is correct
103 Correct 112 ms 19784 KB Output is correct
104 Correct 108 ms 19808 KB Output is correct
105 Correct 111 ms 19780 KB Output is correct
106 Correct 112 ms 19768 KB Output is correct
107 Correct 240 ms 39264 KB Output is correct
108 Correct 276 ms 39244 KB Output is correct
109 Correct 243 ms 39256 KB Output is correct
110 Correct 255 ms 39232 KB Output is correct
111 Correct 66 ms 10012 KB Output is correct
112 Correct 47 ms 10052 KB Output is correct
113 Correct 47 ms 10052 KB Output is correct
114 Correct 47 ms 10060 KB Output is correct
115 Correct 47 ms 10024 KB Output is correct
116 Correct 48 ms 10056 KB Output is correct
117 Correct 47 ms 10052 KB Output is correct
118 Correct 47 ms 9948 KB Output is correct
119 Correct 47 ms 10052 KB Output is correct
120 Correct 48 ms 10052 KB Output is correct
121 Correct 53 ms 10056 KB Output is correct
122 Correct 110 ms 19796 KB Output is correct
123 Correct 130 ms 19688 KB Output is correct
124 Correct 121 ms 19792 KB Output is correct
125 Correct 113 ms 19792 KB Output is correct
126 Correct 116 ms 19696 KB Output is correct
127 Correct 113 ms 19780 KB Output is correct
128 Correct 115 ms 19932 KB Output is correct
129 Correct 126 ms 19796 KB Output is correct
130 Correct 124 ms 19792 KB Output is correct
131 Correct 110 ms 19736 KB Output is correct
132 Correct 112 ms 19784 KB Output is correct
133 Correct 142 ms 19920 KB Output is correct
134 Correct 116 ms 19792 KB Output is correct
135 Correct 108 ms 19800 KB Output is correct
136 Correct 115 ms 19800 KB Output is correct
137 Correct 115 ms 19820 KB Output is correct
138 Correct 112 ms 19780 KB Output is correct
139 Correct 129 ms 19804 KB Output is correct
140 Correct 121 ms 19780 KB Output is correct
141 Correct 108 ms 19780 KB Output is correct
142 Correct 265 ms 39200 KB Output is correct
143 Correct 293 ms 39236 KB Output is correct
144 Correct 244 ms 39260 KB Output is correct
145 Correct 260 ms 39260 KB Output is correct
146 Correct 266 ms 39352 KB Output is correct
147 Correct 269 ms 39260 KB Output is correct
148 Correct 263 ms 39236 KB Output is correct
149 Correct 260 ms 39244 KB Output is correct
150 Correct 259 ms 39324 KB Output is correct
151 Correct 288 ms 39244 KB Output is correct
152 Correct 255 ms 39280 KB Output is correct
153 Correct 271 ms 39364 KB Output is correct
154 Correct 258 ms 39364 KB Output is correct
155 Correct 259 ms 39280 KB Output is correct
156 Correct 246 ms 39168 KB Output is correct
157 Correct 257 ms 39356 KB Output is correct
158 Correct 302 ms 39344 KB Output is correct
159 Correct 249 ms 39208 KB Output is correct
160 Correct 267 ms 39304 KB Output is correct
161 Correct 255 ms 39236 KB Output is correct
162 Correct 259 ms 39388 KB Output is correct
163 Correct 255 ms 39364 KB Output is correct
164 Correct 248 ms 39332 KB Output is correct
165 Correct 263 ms 39364 KB Output is correct
166 Correct 250 ms 39236 KB Output is correct
167 Correct 270 ms 39276 KB Output is correct