BZOJ 3456: City Planning — Solution
Problem Statement
Note: on BZOJ this one is a members-only problem; I did it on JZOJ.
Having just solved the power network problem, Ali is stumped once again by a task from the leadership.
As mentioned before, Ali's country has \(n\) cities. Now the country needs to build some trade routes between certain pairs of cities, so that any two cities in the whole country are connected, directly or indirectly.
To save money, there can be at most one direct trade route between any two cities. For two plans of building routes, if there exists a pair of cities for which whether a route is built differs between the two plans, then the two plans are different; otherwise they are the same. Now you need to find how many different plans there are in total.
Okay, that is the problem troubling Ali. In other words, you need to find the number of simple (no multiple edges, no self-loops) undirected connected graphs on \(n\) vertices.
Since this number may be very large, you only need to output the number of plans mod 1004535809(479 * 2 ^21 + 1).
Main Idea
Actually, the formula for this problem is easy to derive; the hard part is that it needs polynomial inversion and the like:
\[ f(n) = g(n) - \sum_{i = 1}^{n - 1} f(i) {n - 1 \choose i - 1} g(n - i) \]
Here, \(f(n)\) is the number of undirected connected graphs of size \(n\), and \(g(n)\) is the number of undirected graphs (that is, disconnected cases are allowed), \(g(n) = 2^{\frac{n(n-1)}{2}}\). Transforming it slightly:
\[ \sum_{i = 1}^n f(i) {n - 1 \choose i - 1} g(n - i) = g(n) \]
Expand the binomial coefficient:
\[ \begin{gathered} \sum_{i = 1}^n \frac{f(i)g(n-i)(n-1)!}{(i-1)!(n-i)!} = g(n) \\ \sum_{i = 1}^n \frac{f(i)g(n-i)}{(i-1)!(n-i)!} = \frac{g(n)}{(n-1)!} \end{gathered} \]
Split some parts apart:
\[ \sum_{i = 1}^n \frac{f(i)}{(i-1)!} \frac{g(n-i)}{(n-i)!} = \frac{g(n)}{(n-1)!} \]
It turns out to be in the form of a convolution. Consider setting up generating functions:
\[ \begin{gathered} G(x) = \sum_{i = 0}^{\infty}\frac{g(i)}{i!} x^n \\ C(x) = \sum_{i = 0}^{\infty} \frac{g(i)}{(i-1)!} x^n \\ F(x) = \sum_{i = 0}^{\infty} \frac{f(i)}{(i-1)!} x^n \end{gathered} \]
From the problem, this can be written as:
\[ F(x)G(x) \equiv C(x) \ (mod \ x^n) \]
The benefit of polynomials in problem solving is this: you can ignore the variable of the polynomial and focus only on its coefficients, then describe the relations between those coefficients from the polynomial point of view, and then solve the problem with polynomial algorithms.
\[ F(x) \equiv C(x)G^{-1}(x) \ (mod \ x^n) \]
Then in the end \(f(n)\) is just the \(n\)-th coefficient of the polynomial on the right, multiplied by \((n-1)!\), and that's it.
Code
// C.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 1004535809, G = 3, MAX_N = 1 << 18;
int n, rev[MAX_N], Gi;
ll tmp[MAX_N], level[MAX_N], level_inv[MAX_N], gx[MAX_N], CX[MAX_N], GX[MAX_N], invG[MAX_N];
ll quick_pow(ll bas, ll tim)
{
ll ans = 1;
bas %= mod;
while (tim)
{
if (tim & 1)
ans = ans * bas % mod;
bas = bas * bas % mod;
tim >>= 1;
}
return ans;
}
inline void ntt(ll *arr, int limit, int dft)
{
for (int i = 0; i < limit; i++)
if (i < rev[i])
swap(arr[i], arr[rev[i]]);
for (int step = 1; step < limit; step <<= 1)
{
ll omega_n = quick_pow(dft == 1 ? G : Gi, (mod - 1) / (step << 1));
for (int j = 0; j < limit; j += (step << 1))
{
ll omega_nk = 1;
for (int k = j; k < j + step; k++, omega_nk = (omega_nk * omega_n % mod))
{
ll x = arr[k], y = omega_nk * arr[k + step] % mod;
arr[k] = (x + y) % mod, arr[k + step] = (x - y + mod) % mod;
}
}
}
if (dft != 1)
{
ll inv = quick_pow(limit, mod - 2);
for (int i = 0; i < limit; i++)
arr[i] = (arr[i] * inv % mod);
}
}
void poly_inverse(int deg, ll *a, ll *b)
{
if (deg == 1)
return (void)(b[0] = quick_pow(a[0], mod - 2));
poly_inverse((deg + 1) >> 1, a, b);
ll limit = 2, mx_bit = 1;
while ((deg << 1) > limit)
limit <<= 1, mx_bit++;
for (int i = 1; i < limit; i++)
rev[i] = ((rev[i >> 1] >> 1) | ((i & 1) << (mx_bit - 1)));
for (int i = 0; i < deg; i++)
tmp[i] = a[i];
for (int i = deg; i < limit; i++)
tmp[i] = 0;
ntt(tmp, limit, 1), ntt(b, limit, 1);
for (int i = 0; i < limit; i++)
b[i] = 1LL * ((2LL - tmp[i] * b[i] % mod + mod) % mod) * b[i] % mod;
ntt(b, limit, -1);
for (int i = deg; i < limit; i++)
b[i] = 0;
}
int main()
{
Gi = quick_pow(G, mod - 2);
scanf("%d", &n);
level[1] = level[0] = 1, level_inv[0] = 1;
for (int i = 2; i <= n; i++)
level[i] = 1LL * level[i - 1] * i % mod;
level_inv[n] = quick_pow(level[n], mod - 2);
for (int i = n - 1; i >= 1; i--)
level_inv[i] = 1LL * level_inv[i + 1] * (i + 1) % mod;
gx[1] = gx[0] = 1;
for (int i = 2; i <= n; i++)
gx[i] = quick_pow(2, ((1LL * i * (i - 1)) >> 1) % (mod - 1));
for (int i = 0; i <= n; i++)
GX[i] = 1LL * gx[i] * level_inv[i] % mod;
for (int i = 1; i <= n; i++)
CX[i] = 1LL * gx[i] * level_inv[i - 1] % mod;
poly_inverse(n + 1, GX, invG);
ll limit = 2, mx_bit = 1;
while (((n + 1) << 1) > limit)
limit <<= 1, mx_bit++;
for (int i = 1; i < limit; i++)
rev[i] = ((rev[i >> 1] >> 1) | ((i & 1) << (mx_bit - 1)));
ntt(invG, limit, 1), ntt(CX, limit, 1);
for (int i = 0; i < limit; i++)
invG[i] = (invG[i] * CX[i]) % mod;
ntt(invG, limit, -1);
ll ans = invG[n] * quick_pow(level_inv[n - 1], mod - 2) % mod;
while (ans < 0)
ans += mod;
ans %= mod;
printf("%lld", ans);
return 0;
}