#include <bits/extc++.h> /** keep-include */
#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
using ll = int;
// To use most bits rather than just the lowest ones:
struct chash { // large odd number for C
const uint64_t C = ll(4e18 * acos(0)) | 71;
ll operator()(ll x) const { return __builtin_bswap64(x * C); }
};
using hash_table = __gnu_pbds::gp_hash_table<int, int, chash>;
template <const int32_t MOD> struct ModInt {
int32_t x;
ModInt() : x(0) {}
ModInt(long long u) : x(u % MOD) {
if (x < 0)
x += MOD;
}
friend bool operator==(const ModInt &a, const ModInt &b) {
return a.x == b.x;
}
friend bool operator!=(const ModInt &a, const ModInt &b) {
return a.x != b.x;
}
friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; }
friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; }
friend bool operator<=(const ModInt &a, const ModInt &b) {
return a.x <= b.x;
}
friend bool operator>=(const ModInt &a, const ModInt &b) {
return a.x >= b.x;
}
static ModInt sign(long long k) {
return ((k & 1) ? ModInt(MOD - 1) : ModInt(1));
}
ModInt &operator+=(const ModInt &m) {
x += m.x;
if (x >= MOD)
x -= MOD;
return *this;
}
ModInt &operator-=(const ModInt &m) {
x -= m.x;
if (x < 0LL)
x += MOD;
return *this;
}
ModInt &operator*=(const ModInt &m) {
x = (1LL * x * m.x) % MOD;
return *this;
}
friend ModInt operator-(const ModInt &a) {
ModInt res(a);
if (res.x)
res.x = MOD - res.x;
return res;
}
friend ModInt operator+(const ModInt &a, const ModInt &b) {
return ModInt(a) += ModInt(b);
}
friend ModInt operator-(const ModInt &a, const ModInt &b) {
return ModInt(a) -= ModInt(b);
}
friend ModInt operator*(const ModInt &a, const ModInt &b) {
return ModInt(a) *= ModInt(b);
}
static long long fp(long long u, long long k) {
long long res = 1LL;
while (k > 0LL) {
if (k & 1LL)
res = (res * u) % MOD;
u = (u * u) % MOD;
k /= 2LL;
}
return res;
}
static constexpr int mod() { return MOD; }
ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
ModInt inv() {
assert(x);
return ModInt(fp(x, MOD - 2));
}
ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); }
friend ModInt operator/(const ModInt &a, const ModInt &b) {
return ModInt(a) *= ModInt(b).inv();
}
friend ostream &operator<<(ostream &out, const ModInt &a) {
return out << a.x;
}
friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; }
friend string to_string(ModInt u) { return to_string(u.x); }
};
// const int MOD = 1e9 + 7;
const int MOD = 998244353;
using Mint = ModInt<MOD>;
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> adj(n);
vector<int> bitAdj(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u] |= 1 << v;
bitAdj[u] |= 1 << v;
bitAdj[v] |= 1 << u;
}
vector<int> areBad(n);
for (int u = 0; u < n; ++u)
for (int v = 0; v < u; ++v)
if (!((1 << v) & bitAdj[u]))
areBad[u] |= 1 << v;
vector<vector<int>> inComplement(1 << n);
for (int msk = 0; msk < (1 << n); ++msk) {
int c = (1 << n) - 1 - msk;
int complement = c;
while (true) {
inComplement[msk].push_back(c);
if (!c)
break;
c = (c - 1) & complement;
}
sort(inComplement[msk].begin(), inComplement[msk].end());
}
vector<hash_table> ids(1 << n);
for (int i = 0; i < (1 << n); ++i)
for (int j = 0; j < (int)inComplement[i].size(); ++j)
ids[i][inComplement[i][j]] = j;
auto getId = [&](int msk, int c) {
// return lower_bound(inComplement[msk].begin(), inComplement[msk].end(), c)
// - inComplement[msk].begin();
return ids[msk][c];
};
vector<vector<Mint>> dpBack(1 << n);
vector<vector<Mint>> dpForward(1 << n);
for (int msk = 0; msk < (1 << n); ++msk) {
dpBack[msk].resize(inComplement[msk].size());
dpForward[msk].resize(inComplement[msk].size());
}
dpBack[0][0] = 1;
for (int added = 0; added < (1 << n); ++added) {
int complement = (1 << n) - 1 - added;
for (int i = 0; i < (int)inComplement[added].size(); ++i) {
int bad = inComplement[added][i];
for (int nxt = 0; nxt < n; ++nxt)
if (!((1 << nxt) & added) and !((1 << nxt) & bad)) {
int nxtBad = bad & (bad ^ bitAdj[nxt]);
nxtBad |= areBad[nxt] & complement;
int j = getId(added ^ (1 << nxt), nxtBad);
dpBack[added ^ (1 << nxt)][j] += dpBack[added][i];
}
}
}
dpForward[(1 << n) - 1][0] = 1;
Mint sol = 0;
for (int added = (1 << n) - 2; added >= 0; --added) {
for (int i = 0; i < (int)inComplement[added].size(); ++i) {
int bad = inComplement[added][i];
for (int nxt = 0; nxt < n; ++nxt)
if (!((1 << nxt) & added) and !((1 << nxt) & bad)) {
int nxtBad = bad & (bad ^ bitAdj[nxt]);
nxtBad |= areBad[nxt] & inComplement[added].back();
int j = getId(added ^ (1 << nxt), nxtBad);
dpForward[added][i] += dpForward[added ^ (1 << nxt)][j];
int cntFlip = __builtin_popcount(adj[nxt] & added);
Mint cnt =
dpForward[added ^ (1 << nxt)][getId(added ^ (1 << nxt), nxtBad)] *
dpBack[added][i];
sol += cnt * cntFlip;
}
}
}
cout << sol << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
504 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
2 ms |
980 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
2 ms |
1108 KB |
Output is correct |
38 |
Correct |
2 ms |
1108 KB |
Output is correct |
39 |
Correct |
5 ms |
2516 KB |
Output is correct |
40 |
Correct |
5 ms |
2516 KB |
Output is correct |
41 |
Correct |
5 ms |
2516 KB |
Output is correct |
42 |
Correct |
5 ms |
2544 KB |
Output is correct |
43 |
Correct |
4 ms |
2516 KB |
Output is correct |
44 |
Correct |
4 ms |
2516 KB |
Output is correct |
45 |
Correct |
5 ms |
2516 KB |
Output is correct |
46 |
Correct |
5 ms |
2492 KB |
Output is correct |
47 |
Correct |
5 ms |
2516 KB |
Output is correct |
48 |
Correct |
5 ms |
2492 KB |
Output is correct |
49 |
Correct |
5 ms |
2516 KB |
Output is correct |
50 |
Correct |
4 ms |
2516 KB |
Output is correct |
51 |
Correct |
4 ms |
2516 KB |
Output is correct |
52 |
Correct |
14 ms |
6996 KB |
Output is correct |
53 |
Correct |
14 ms |
6976 KB |
Output is correct |
54 |
Correct |
14 ms |
7048 KB |
Output is correct |
55 |
Correct |
14 ms |
7044 KB |
Output is correct |
56 |
Correct |
13 ms |
6972 KB |
Output is correct |
57 |
Correct |
13 ms |
6964 KB |
Output is correct |
58 |
Correct |
13 ms |
7040 KB |
Output is correct |
59 |
Correct |
13 ms |
7008 KB |
Output is correct |
60 |
Correct |
13 ms |
6972 KB |
Output is correct |
61 |
Correct |
17 ms |
6996 KB |
Output is correct |
62 |
Correct |
13 ms |
6996 KB |
Output is correct |
63 |
Correct |
14 ms |
7040 KB |
Output is correct |
64 |
Correct |
13 ms |
7044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
504 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
2 ms |
980 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
2 ms |
1108 KB |
Output is correct |
38 |
Correct |
2 ms |
1108 KB |
Output is correct |
39 |
Correct |
5 ms |
2516 KB |
Output is correct |
40 |
Correct |
5 ms |
2516 KB |
Output is correct |
41 |
Correct |
5 ms |
2516 KB |
Output is correct |
42 |
Correct |
5 ms |
2544 KB |
Output is correct |
43 |
Correct |
4 ms |
2516 KB |
Output is correct |
44 |
Correct |
4 ms |
2516 KB |
Output is correct |
45 |
Correct |
5 ms |
2516 KB |
Output is correct |
46 |
Correct |
5 ms |
2492 KB |
Output is correct |
47 |
Correct |
5 ms |
2516 KB |
Output is correct |
48 |
Correct |
5 ms |
2492 KB |
Output is correct |
49 |
Correct |
5 ms |
2516 KB |
Output is correct |
50 |
Correct |
4 ms |
2516 KB |
Output is correct |
51 |
Correct |
4 ms |
2516 KB |
Output is correct |
52 |
Correct |
14 ms |
6996 KB |
Output is correct |
53 |
Correct |
14 ms |
6976 KB |
Output is correct |
54 |
Correct |
14 ms |
7048 KB |
Output is correct |
55 |
Correct |
14 ms |
7044 KB |
Output is correct |
56 |
Correct |
13 ms |
6972 KB |
Output is correct |
57 |
Correct |
13 ms |
6964 KB |
Output is correct |
58 |
Correct |
13 ms |
7040 KB |
Output is correct |
59 |
Correct |
13 ms |
7008 KB |
Output is correct |
60 |
Correct |
13 ms |
6972 KB |
Output is correct |
61 |
Correct |
17 ms |
6996 KB |
Output is correct |
62 |
Correct |
13 ms |
6996 KB |
Output is correct |
63 |
Correct |
14 ms |
7040 KB |
Output is correct |
64 |
Correct |
13 ms |
7044 KB |
Output is correct |
65 |
Correct |
1420 ms |
528464 KB |
Output is correct |
66 |
Correct |
1412 ms |
528524 KB |
Output is correct |
67 |
Correct |
1385 ms |
528580 KB |
Output is correct |
68 |
Correct |
1332 ms |
528468 KB |
Output is correct |
69 |
Correct |
1532 ms |
528472 KB |
Output is correct |
70 |
Correct |
1526 ms |
528448 KB |
Output is correct |
71 |
Correct |
1449 ms |
528464 KB |
Output is correct |
72 |
Correct |
1436 ms |
528560 KB |
Output is correct |
73 |
Correct |
1454 ms |
528472 KB |
Output is correct |
74 |
Correct |
1406 ms |
528464 KB |
Output is correct |
75 |
Correct |
1418 ms |
528472 KB |
Output is correct |
76 |
Correct |
1415 ms |
528364 KB |
Output is correct |
77 |
Runtime error |
769 ms |
1048576 KB |
Execution killed with signal 9 |
78 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
504 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
2 ms |
980 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
2 ms |
1108 KB |
Output is correct |
38 |
Correct |
2 ms |
1108 KB |
Output is correct |
39 |
Correct |
5 ms |
2516 KB |
Output is correct |
40 |
Correct |
5 ms |
2516 KB |
Output is correct |
41 |
Correct |
5 ms |
2516 KB |
Output is correct |
42 |
Correct |
5 ms |
2544 KB |
Output is correct |
43 |
Correct |
4 ms |
2516 KB |
Output is correct |
44 |
Correct |
4 ms |
2516 KB |
Output is correct |
45 |
Correct |
5 ms |
2516 KB |
Output is correct |
46 |
Correct |
5 ms |
2492 KB |
Output is correct |
47 |
Correct |
5 ms |
2516 KB |
Output is correct |
48 |
Correct |
5 ms |
2492 KB |
Output is correct |
49 |
Correct |
5 ms |
2516 KB |
Output is correct |
50 |
Correct |
4 ms |
2516 KB |
Output is correct |
51 |
Correct |
4 ms |
2516 KB |
Output is correct |
52 |
Correct |
14 ms |
6996 KB |
Output is correct |
53 |
Correct |
14 ms |
6976 KB |
Output is correct |
54 |
Correct |
14 ms |
7048 KB |
Output is correct |
55 |
Correct |
14 ms |
7044 KB |
Output is correct |
56 |
Correct |
13 ms |
6972 KB |
Output is correct |
57 |
Correct |
13 ms |
6964 KB |
Output is correct |
58 |
Correct |
13 ms |
7040 KB |
Output is correct |
59 |
Correct |
13 ms |
7008 KB |
Output is correct |
60 |
Correct |
13 ms |
6972 KB |
Output is correct |
61 |
Correct |
17 ms |
6996 KB |
Output is correct |
62 |
Correct |
13 ms |
6996 KB |
Output is correct |
63 |
Correct |
14 ms |
7040 KB |
Output is correct |
64 |
Correct |
13 ms |
7044 KB |
Output is correct |
65 |
Correct |
1420 ms |
528464 KB |
Output is correct |
66 |
Correct |
1412 ms |
528524 KB |
Output is correct |
67 |
Correct |
1385 ms |
528580 KB |
Output is correct |
68 |
Correct |
1332 ms |
528468 KB |
Output is correct |
69 |
Correct |
1532 ms |
528472 KB |
Output is correct |
70 |
Correct |
1526 ms |
528448 KB |
Output is correct |
71 |
Correct |
1449 ms |
528464 KB |
Output is correct |
72 |
Correct |
1436 ms |
528560 KB |
Output is correct |
73 |
Correct |
1454 ms |
528472 KB |
Output is correct |
74 |
Correct |
1406 ms |
528464 KB |
Output is correct |
75 |
Correct |
1418 ms |
528472 KB |
Output is correct |
76 |
Correct |
1415 ms |
528364 KB |
Output is correct |
77 |
Runtime error |
769 ms |
1048576 KB |
Execution killed with signal 9 |
78 |
Halted |
0 ms |
0 KB |
- |