QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#219846 | #7150. Random spanning tree | ckiseki# | AC ✓ | 40ms | 3672kb | C++20 | 4.3kb | 2023-10-19 19:05:47 | 2023-10-19 19:05:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(const char *s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
void orange_(const char *s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++f)
cerr << (f ? ", " : "") << *L++;
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define orange(...) safe
#define debug(...) safe
#endif
namespace {
constexpr int N = 8;
constexpr int M = N * (N - 1) / 2;
using i64 = int64_t;
using i128 = __int128;
i128 gcd(i128 x, i128 y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
struct F {
i64 a, b;
F() : a(0), b(1) {}
F(i64 a_, i64 b_ = 1) : a(a_), b(b_) {}
F operator*(const F &rhs) const {
i128 up = i128(a) * rhs.a;
i128 dn = i128(b) * rhs.b;
i128 g = gcd(up, dn);
return F(i64(up / g), i64(dn / g));
}
F operator/(const F &rhs) const {
return *this * F(rhs.b, rhs.a);
}
F operator-() const {
return F(-a, b);
}
F operator+(const F &rhs) const {
i128 up = i128(a) * rhs.b + i128(rhs.a) * b;
i128 dn = i128(b) * rhs.b;
i128 g = gcd(up, dn);
return F(i64(up / g), i64(dn / g));
}
F operator-(const F &rhs) const {
return *this + (-rhs);
}
friend ostream &operator<<(ostream &os, const F &f) {
return os << f.a << "/" << f.b;
}
};
i64 comb[M + 1][M + 1];
void calcComb() {
for (int i = 0; i <= M; ++i) {
comb[i][0] = comb[i][i] = 1;
for (int j = 1; j < i; ++j) {
comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
}
}
}
F intl[M];
void calcIntl(int m) {
for (int k = 0; k < m; ++k) {
for (int i = 0; i <= m - k - 1; ++i) {
bool neg = (m - k - 1 - i) & 1;
i64 c = comb[m - k - 1][i];
if (neg)
c = -c;
intl[k] = intl[k] + F(c, (m - i + 1));
}
}
}
bool g[N][N];
i64 dp1[1 << N][M + 1], dp2[1 << N][M + 1];
void calcDP1(int n, int m) {
const uint32_t n2 = 1u << n;
dp1[0][0] = 1;
for (uint32_t S = 1; S < n2; ++S) {
const uint32_t p = countr_zero(S);
const uint32_t cur = S ^ (1u << p);
vector<pair<uint32_t, int>> v;
auto dfs2 = [&](auto self, size_t i, int c, i64 w) {
if (i == v.size()) {
dp1[S][c] += w;
return;
}
const int sz = int(popcount(v[i].first));
for (int c1 = sz - 1; c1 <= m; ++c1) {
if (dp1[v[i].first][c1] == 0)
break;
for (int c2 = 1; c2 <= v[i].second; ++c2)
self(self, i + 1, c + c1 + c2, w * dp1[v[i].first][c1] * comb[v[i].second][c2]);
}
};
auto dfs = [&](auto self, int i) {
if (i == n) {
dfs2(dfs2, 0, 0, 1);
return;
}
if (((cur >> i) & 1) == 0) {
self(self, i + 1);
return;
}
v.emplace_back(1u << i, g[p][i]);
self(self, i + 1);
v.pop_back();
for (auto &[vi, c] : v) {
vi ^= 1u << i;
c += g[p][i];
self(self, i + 1);
c -= g[p][i];
vi ^= 1u << i;
}
};
dfs(dfs, 0);
}
}
F solve(int n, int m, int U, int V) {
const uint32_t Suv = (1u << U) | (1u << V);
const uint32_t n2 = 1u << n;
for (uint32_t S = 0; S < n2; ++S)
for (int i = 0; i <= m; ++i)
dp2[S][i] = 0;
dp2[0][0] = 1;
for (uint32_t S1 = 0; S1 < n2; ++S1) {
for (int i = 0; i <= m; ++i) {
for (uint32_t S2 = 1; S2 < n2; ++S2) {
if ((S1 & S2) != 0)
continue;
if (S1 != 0 and countr_zero(S1) < countr_zero(S2))
continue;
if ((S2 & Suv) == Suv)
continue;
for (int j = 0; i + j <= m; ++j) {
dp2[S1 | S2][i + j] += dp2[S1][i] * dp1[S2][j];
}
}
}
}
F ret = 0;
for (int i = 0; i < m; ++i) {
ret = ret + intl[i] * dp2[n2 - 1][i];
}
return ret;
}
} // namespace
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
calcComb();
int n, m;
cin >> n >> m;
vector<pair<int, int>> e(m);
for (auto &[u, v] : e) {
cin >> u >> v;
--u, --v;
g[u][v] = g[v][u] = true;
}
calcIntl(m);
calcDP1(n, m);
F ans = 0;
for (auto [u, v] : e)
ans = ans + solve(n, m, u, v);
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3528kb
input:
3 2 1 2 2 3
output:
1/1
result:
ok single line: '1/1'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3468kb
input:
3 3 1 2 2 3 3 1
output:
3/4
result:
ok single line: '3/4'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
2 1 2 1
output:
1/2
result:
ok single line: '1/2'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
2 1 1 2
output:
1/2
result:
ok single line: '1/2'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3492kb
input:
2 1 1 2
output:
1/2
result:
ok single line: '1/2'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
2 1 1 2
output:
1/2
result:
ok single line: '1/2'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3492kb
input:
2 1 1 2
output:
1/2
result:
ok single line: '1/2'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
2 1 1 2
output:
1/2
result:
ok single line: '1/2'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
2 1 1 2
output:
1/2
result:
ok single line: '1/2'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
2 1 1 2
output:
1/2
result:
ok single line: '1/2'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
2 1 1 2
output:
1/2
result:
ok single line: '1/2'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
2 1 1 2
output:
1/2
result:
ok single line: '1/2'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3476kb
input:
3 2 2 1 3 2
output:
1/1
result:
ok single line: '1/1'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
3 3 2 1 1 3 3 2
output:
3/4
result:
ok single line: '3/4'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
3 2 2 3 2 1
output:
1/1
result:
ok single line: '1/1'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
3 2 2 3 1 3
output:
1/1
result:
ok single line: '1/1'
Test #17:
score: 0
Accepted
time: 1ms
memory: 3480kb
input:
3 3 1 3 3 2 2 1
output:
3/4
result:
ok single line: '3/4'
Test #18:
score: 0
Accepted
time: 1ms
memory: 3564kb
input:
3 2 2 3 1 3
output:
1/1
result:
ok single line: '1/1'
Test #19:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
3 3 2 3 2 1 3 1
output:
3/4
result:
ok single line: '3/4'
Test #20:
score: 0
Accepted
time: 0ms
memory: 3492kb
input:
3 3 2 1 2 3 1 3
output:
3/4
result:
ok single line: '3/4'
Test #21:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
3 2 1 2 2 3
output:
1/1
result:
ok single line: '1/1'
Test #22:
score: 0
Accepted
time: 0ms
memory: 3428kb
input:
3 2 1 3 2 3
output:
1/1
result:
ok single line: '1/1'
Test #23:
score: 0
Accepted
time: 0ms
memory: 3500kb
input:
4 3 2 1 1 3 2 4
output:
3/2
result:
ok single line: '3/2'
Test #24:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
4 6 1 4 4 3 1 2 2 3 1 3 2 4
output:
31/35
result:
ok single line: '31/35'
Test #25:
score: 0
Accepted
time: 0ms
memory: 3536kb
input:
4 5 2 4 2 3 4 3 3 1 1 4
output:
31/30
result:
ok single line: '31/30'
Test #26:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
4 3 1 3 4 1 1 2
output:
3/2
result:
ok single line: '3/2'
Test #27:
score: 0
Accepted
time: 0ms
memory: 3500kb
input:
4 6 2 1 4 3 1 3 3 2 2 4 4 1
output:
31/35
result:
ok single line: '31/35'
Test #28:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
4 5 3 1 3 2 4 3 4 2 4 1
output:
31/30
result:
ok single line: '31/30'
Test #29:
score: 0
Accepted
time: 1ms
memory: 3492kb
input:
4 4 4 3 1 2 4 2 4 1
output:
5/4
result:
ok single line: '5/4'
Test #30:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
4 6 4 2 1 2 2 3 3 4 1 4 3 1
output:
31/35
result:
ok single line: '31/35'
Test #31:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
4 5 4 3 1 2 3 1 2 3 1 4
output:
31/30
result:
ok single line: '31/30'
Test #32:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
4 5 4 2 1 3 1 2 2 3 1 4
output:
31/30
result:
ok single line: '31/30'
Test #33:
score: 0
Accepted
time: 1ms
memory: 3576kb
input:
5 5 1 4 4 3 5 2 4 5 3 1
output:
7/4
result:
ok single line: '7/4'
Test #34:
score: 0
Accepted
time: 1ms
memory: 3504kb
input:
5 9 3 4 2 4 3 1 5 2 1 4 5 3 2 3 1 5 5 4
output:
893/840
result:
ok single line: '893/840'
Test #35:
score: 0
Accepted
time: 1ms
memory: 3576kb
input:
5 5 5 2 5 1 1 2 3 1 3 4
output:
7/4
result:
ok single line: '7/4'
Test #36:
score: 0
Accepted
time: 1ms
memory: 3612kb
input:
5 7 3 1 5 1 4 1 4 2 5 3 1 2 3 4
output:
1111/840
result:
ok single line: '1111/840'
Test #37:
score: 0
Accepted
time: 1ms
memory: 3512kb
input:
5 10 5 3 2 3 4 5 1 3 2 1 5 2 1 5 4 2 4 1 4 3
output:
893/924
result:
ok single line: '893/924'
Test #38:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
5 4 1 2 1 4 1 3 5 4
output:
2/1
result:
ok single line: '2/1'
Test #39:
score: 0
Accepted
time: 1ms
memory: 3548kb
input:
5 7 5 4 5 1 1 3 3 5 5 2 3 4 2 4
output:
1111/840
result:
ok single line: '1111/840'
Test #40:
score: 0
Accepted
time: 1ms
memory: 3536kb
input:
5 9 4 5 1 3 4 3 1 5 2 4 5 3 1 2 4 1 2 5
output:
893/840
result:
ok single line: '893/840'
Test #41:
score: 0
Accepted
time: 0ms
memory: 3508kb
input:
5 5 2 1 5 3 5 1 2 4 5 2
output:
7/4
result:
ok single line: '7/4'
Test #42:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
5 4 3 4 1 3 2 3 5 1
output:
2/1
result:
ok single line: '2/1'
Test #43:
score: 0
Accepted
time: 2ms
memory: 3588kb
input:
6 15 1 2 4 1 2 3 4 3 5 3 3 1 5 2 4 2 1 5 4 5 6 2 6 4 1 6 3 6 6 5
output:
278/273
result:
ok single line: '278/273'
Test #44:
score: 0
Accepted
time: 2ms
memory: 3464kb
input:
6 14 6 3 2 3 4 6 1 6 6 2 6 5 4 2 4 1 5 3 5 2 1 3 4 3 1 5 2 1
output:
4448/4095
result:
ok single line: '4448/4095'
Test #45:
score: 0
Accepted
time: 1ms
memory: 3592kb
input:
6 5 1 5 4 2 3 5 4 3 3 6
output:
5/2
result:
ok single line: '5/2'
Test #46:
score: 0
Accepted
time: 1ms
memory: 3556kb
input:
6 10 4 2 4 3 4 6 5 1 3 2 3 5 2 1 6 1 6 3 6 2
output:
453/308
result:
ok single line: '453/308'
Test #47:
score: 0
Accepted
time: 1ms
memory: 3552kb
input:
6 12 5 3 4 1 2 4 3 1 5 6 3 4 2 6 6 4 2 3 1 2 1 6 5 2
output:
29881/24024
result:
ok single line: '29881/24024'
Test #48:
score: 0
Accepted
time: 1ms
memory: 3524kb
input:
6 11 3 5 4 3 4 6 4 5 2 4 6 2 6 5 1 5 2 5 2 3 4 1
output:
1733/1260
result:
ok single line: '1733/1260'
Test #49:
score: 0
Accepted
time: 2ms
memory: 3508kb
input:
6 13 4 1 5 3 2 4 4 6 5 2 2 3 6 2 6 3 4 3 1 5 6 1 2 1 1 3
output:
10799/9240
result:
ok single line: '10799/9240'
Test #50:
score: 0
Accepted
time: 2ms
memory: 3628kb
input:
6 14 3 5 2 3 6 3 5 1 3 1 2 1 2 5 2 6 6 5 4 3 4 2 4 1 5 4 1 6
output:
4448/4095
result:
ok single line: '4448/4095'
Test #51:
score: 0
Accepted
time: 1ms
memory: 3596kb
input:
6 5 2 6 3 2 4 6 6 5 2 1
output:
5/2
result:
ok single line: '5/2'
Test #52:
score: 0
Accepted
time: 2ms
memory: 3524kb
input:
6 13 5 3 1 4 6 2 3 1 5 6 3 4 2 4 5 1 3 2 2 1 3 6 4 6 5 2
output:
34751/30030
result:
ok single line: '34751/30030'
Test #53:
score: 0
Accepted
time: 3ms
memory: 3548kb
input:
7 10 1 7 5 1 2 3 1 6 3 7 4 5 7 2 5 6 1 2 5 3
output:
5263/2520
result:
ok single line: '5263/2520'
Test #54:
score: 0
Accepted
time: 10ms
memory: 3552kb
input:
7 21 1 4 5 4 2 1 3 7 3 2 1 7 3 4 5 7 2 7 1 5 3 5 2 5 6 3 5 6 6 7 4 2 3 1 4 7 6 4 6 1 6 2
output:
30739/29172
result:
ok single line: '30739/29172'
Test #55:
score: 0
Accepted
time: 6ms
memory: 3544kb
input:
7 16 7 3 7 1 7 6 6 2 6 5 3 6 1 3 7 4 6 1 2 4 2 5 3 5 6 4 3 2 1 4 4 3
output:
1049207/765765
result:
ok single line: '1049207/765765'
Test #56:
score: 0
Accepted
time: 3ms
memory: 3596kb
input:
7 10 2 3 5 4 7 1 2 1 2 5 5 1 3 5 1 6 2 6 7 5
output:
89/42
result:
ok single line: '89/42'
Test #57:
score: 0
Accepted
time: 10ms
memory: 3532kb
input:
7 21 7 5 3 7 3 4 4 6 7 6 3 6 2 4 5 1 2 1 3 5 4 5 3 1 2 7 6 5 4 7 1 7 2 6 6 1 5 2 1 4 2 3
output:
30739/29172
result:
ok single line: '30739/29172'
Test #58:
score: 0
Accepted
time: 6ms
memory: 3528kb
input:
7 16 1 2 6 7 3 5 3 7 3 4 7 2 4 6 2 5 1 4 3 1 4 5 6 2 2 4 6 3 5 6 7 4
output:
16668527/12252240
result:
ok single line: '16668527/12252240'
Test #59:
score: 0
Accepted
time: 4ms
memory: 3536kb
input:
7 11 2 1 5 2 7 3 5 6 6 2 4 1 3 1 3 4 1 6 2 7 4 2
output:
7493/3960
result:
ok single line: '7493/3960'
Test #60:
score: 0
Accepted
time: 1ms
memory: 3560kb
input:
7 6 4 1 3 4 6 1 2 7 5 2 4 2
output:
3/1
result:
ok single line: '3/1'
Test #61:
score: 0
Accepted
time: 6ms
memory: 3552kb
input:
7 16 7 1 1 3 2 6 5 3 4 1 6 7 2 7 2 5 6 3 6 1 4 7 3 7 4 3 6 4 2 3 6 5
output:
1049207/765765
result:
ok single line: '1049207/765765'
Test #62:
score: 0
Accepted
time: 2ms
memory: 3524kb
input:
7 8 2 3 6 3 7 4 3 1 4 5 4 1 5 1 7 3
output:
1039/420
result:
ok single line: '1039/420'
Test #63:
score: 0
Accepted
time: 33ms
memory: 3584kb
input:
8 21 8 7 1 8 6 7 4 3 2 1 8 6 3 1 4 6 5 1 8 4 4 7 3 8 5 8 2 6 2 7 6 1 7 3 3 5 4 1 4 2 3 2
output:
110647451/77597520
result:
ok single line: '110647451/77597520'
Test #64:
score: 0
Accepted
time: 30ms
memory: 3640kb
input:
8 20 8 4 6 2 3 8 7 4 2 3 5 1 6 8 6 7 2 1 4 3 5 7 6 5 2 5 2 7 6 1 8 7 4 1 2 8 7 3 5 4
output:
342183313/232792560
result:
ok single line: '342183313/232792560'
Test #65:
score: 0
Accepted
time: 10ms
memory: 3632kb
input:
8 11 2 6 1 8 5 1 6 3 2 4 8 3 5 8 2 8 2 7 6 1 1 7
output:
1549/616
result:
ok single line: '1549/616'
Test #66:
score: 0
Accepted
time: 40ms
memory: 3632kb
input:
8 23 3 8 1 7 1 8 1 4 4 6 8 7 5 6 6 8 7 4 6 7 3 7 2 6 3 5 1 6 8 4 2 5 6 3 7 5 1 2 5 4 2 8 3 1 7 2
output:
22574459/17383860
result:
ok single line: '22574459/17383860'
Test #67:
score: 0
Accepted
time: 15ms
memory: 3672kb
input:
8 14 4 3 4 2 5 8 4 8 6 1 6 3 8 3 5 2 7 1 6 7 7 3 5 6 3 1 7 8
output:
367811/180180
result:
ok single line: '367811/180180'
Test #68:
score: 0
Accepted
time: 13ms
memory: 3612kb
input:
8 13 5 6 6 1 3 2 2 1 8 2 2 6 2 5 5 3 7 3 5 4 4 6 1 5 1 7
output:
273571/120120
result:
ok single line: '273571/120120'
Test #69:
score: 0
Accepted
time: 3ms
memory: 3604kb
input:
8 8 1 3 6 4 7 3 8 6 5 4 1 8 2 1 5 3
output:
22/7
result:
ok single line: '22/7'
Test #70:
score: 0
Accepted
time: 33ms
memory: 3652kb
input:
8 21 4 2 5 3 7 6 8 4 2 3 1 6 1 8 3 7 2 5 8 7 6 8 5 4 8 2 1 4 5 8 6 4 5 1 4 3 4 7 2 7 7 1
output:
11782289/8314020
result:
ok single line: '11782289/8314020'
Test #71:
score: 0
Accepted
time: 9ms
memory: 3600kb
input:
8 11 4 3 2 5 1 2 4 8 8 1 4 2 6 1 7 3 2 8 7 8 1 3
output:
6499/2520
result:
ok single line: '6499/2520'
Test #72:
score: 0
Accepted
time: 17ms
memory: 3672kb
input:
8 15 6 5 7 6 4 6 4 2 5 7 4 8 8 2 8 5 3 1 4 1 7 3 8 3 7 1 3 5 5 4
output:
115279/60060
result:
ok single line: '115279/60060'
Extra Test:
score: 0
Extra Test Passed