Solution -「洛谷 P5827」边双连通图计数
\(\mathcal{Description}\)
link.
求包含 \(n\) 个点的边双连通图的个数。
\(n\le10^5\)。
\(\mathcal{Solution}\)
类似于这道题,仍令 \(D(x)\) 为有根无向连通图的 \(\text{EGF}\),\(B(x)\) 为边双连通图的 \(\text{EGF}\),考虑用 \(B\) 表示 \(D\)。显然,根仅存在于一个边双连通分量中。我们可以在这个连通分量的任意一个点上挂一个有根连通图,这显然不会影响当前的边双,\(\text{EGF}\) 为 \(nD(x)\)(\(n\) 为根所在边双大小。)事实上可以挂许多图,所以处根所在边双以外的 \(\text{EGF}\) 为 \(\exp\left(nD(x)\right)\)。枚举根所在边双大小,就有:
\[D(x)=\sum_{i=1}^{+\infty}\frac{b_ix^i\exp\left(iD(x)\right)}{i!}=B\left(x\exp D(x)\right)
\]
令 \(F(x)=x\exp D(x)\),\(F^{-1}(x)\) 为其复合逆。将 \(F^{-1}(x)\) 代入上式得:
\[B(x)=D\left(F^{-1}(x)\right)
\]
扩展拉格朗日反演:
\[[x^n]B(x)=\frac{1}n[x^{n-1}]D'(x)\left(\frac{x}{F(x)}\right)^n
\]
重新展开 \(F\),化简得:
\[[x^n]B(x)=\frac{1}n[x^{n-1}]D'(x)\exp\left(-nD(x)\right)
\]
求出 \(D\),继而求出 \([x^n]B(x)\)。复杂度 \(\mathcal O(n\log n)\)。
\(\mathcal{Code}\)
#include <cmath>
#include <cstdio>
const int MAXN = 1 << 18, MOD = 998244353;
int n, fac[MAXN + 5], ifac[MAXN + 5], inv[MAXN + 5];
int F[MAXN + 5], G[MAXN + 5], H[MAXN + 5], T[MAXN + 5];
inline int qkpow ( int a, int b, const int p = MOD ) {
int ret = 1;
for ( ; b; a = 1ll * a * a % p, b >>= 1 ) ret = 1ll * ret * ( b & 1 ? a : 1 ) % p;
return ret;
}
namespace Poly {
const int G = 3;
inline void NTT ( const int n, int* A, const int tp ) {
static int lstn = -1, rev[MAXN + 5] {};
if ( lstn ^ n ) {
int lgn = log ( n ) / log ( 2 ) + 0.5;
for ( int i = 0; i < n; ++ i ) rev[i] = ( rev[i >> 1] >> 1 ) | ( ( i & 1 ) << lgn >> 1 );
lstn = n;
}
for ( int i = 0; i < n; ++ i ) if ( i < rev[i] ) A[i] ^= A[rev[i]] ^= A[i] ^= A[rev[i]];
for ( int i = 2, stp = 1; i <= n; i <<= 1, stp <<= 1 ) {
int w = qkpow ( G, ( MOD - 1 ) / i );
if ( ! ~ tp ) w = qkpow ( w, MOD - 2 );
for ( int j = 0; j < n; j += i ) {
for ( int k = j, r = 1; k < j + stp; ++ k, r = 1ll * r * w % MOD ) {
int ev = A[k], ov = 1ll * r * A[k + stp] % MOD;
A[k] = ( ev + ov ) % MOD, A[k + stp] = ( ev - ov + MOD ) % MOD;
}
}
}
if ( ! ~ tp ) for ( int i = 0; i < n; ++ i ) A[i] = 1ll * A[i] * inv[n] % MOD;
}
inline void polyDer ( const int n, const int* A, int* R ) {
for ( int i = 1; i < n; ++ i ) R[i - 1] = 1ll * i * A[i] % MOD;
R[n - 1] = 0;
}
inline void polyInt ( const int n, const int* A, int* R ) {
for ( int i = n - 1; ~ i; -- i ) R[i + 1] = 1ll * inv[i + 1] * A[i] % MOD;
R[0] = 0;
}
inline void polyInv ( const int n, const int* A, int* R ) {
static int tmp[2][MAXN + 5] {};
if ( n == 1 ) return void ( R[0] = qkpow ( A[0], MOD - 2 ) );
polyInv ( n >> 1, A, R );
for ( int i = 0; i < n; ++ i ) tmp[0][i] = A[i], tmp[1][i] = R[i];
NTT ( n << 1, tmp[0], 1 ), NTT ( n << 1, tmp[1], 1 );
for ( int i = 0; i < n << 1; ++ i ) tmp[0][i] = 1ll * tmp[0][i] * tmp[1][i] % MOD * tmp[1][i] % MOD;
NTT ( n << 1, tmp[0], -1 );
for ( int i = 0; i < n; ++ i ) R[i] = ( 2ll * R[i] % MOD - tmp[0][i] + MOD ) % MOD;
for ( int i = 0; i < n << 1; ++ i ) tmp[0][i] = tmp[1][i] = 0;
}
inline void polyLn ( const int n, const int* A, int* R ) {
static int tmp[2][MAXN + 5] {};
polyDer ( n, A, tmp[0] ), polyInv ( n, A, tmp[1] );
NTT ( n << 1, tmp[0], 1 ), NTT ( n << 1, tmp[1], 1 );
for ( int i = 0; i < n << 1; ++ i ) tmp[0][i] = 1ll * tmp[0][i] * tmp[1][i] % MOD;
NTT ( n << 1, tmp[0], -1 ), polyInt ( n << 1, tmp[0], R );
for ( int i = 0; i < n << 1; ++ i ) tmp[0][i] = tmp[1][i] = 0;
}
inline void polyExp ( const int n, const int* A, int* R ) {
static int tmp[MAXN + 5] {};
if ( n == 1 ) return void ( R[0] = 1 );
polyExp ( n >> 1, A, R ), polyLn ( n, R, tmp );
tmp[0] = ( A[0] + 1 - tmp[0] + MOD ) % MOD;
for ( int i = 1; i < n; ++ i ) tmp[i] = ( A[i] - tmp[i] + MOD ) % MOD;
NTT ( n << 1, tmp, 1 ), NTT ( n << 1, R, 1 );
for ( int i = 0; i < n << 1; ++ i ) R[i] = 1ll * R[i] * tmp[i] % MOD;
NTT ( n << 1, R, -1 );
for ( int i = n; i < n << 1; ++ i ) R[i] = tmp[i] = 0;
}
} // namespace Poly.
inline void init () {
int len = MAXN >> 1;
inv[1] = fac[0] = ifac[0] = fac[1] = ifac[1] = 1;
for ( int i = 2; i <= MAXN; ++ i ) {
fac[i] = 1ll * i * fac[i - 1] % MOD;
inv[i] = 1ll * ( MOD - MOD / i ) * inv[MOD % i] % MOD;
ifac[i] = 1ll * inv[i] * ifac[i - 1] % MOD;
}
for ( int i = 0; i < len; ++ i ) F[i] = 1ll * qkpow ( 2, ( i * ( i - 1ll ) >> 1 ) % ( MOD - 1 ) ) * ifac[i] % MOD;
Poly::polyLn ( len, F, G );
for ( int i = 0; i < len; ++ i ) G[i] = 1ll * G[i] * i % MOD;
Poly::polyDer ( len, G, H ), Poly::NTT ( MAXN, H, 1 );
}
inline void solve () {
int len = MAXN >> 1;
if ( n == 1 ) return void ( puts ( "1" ) );
for ( int i = 0; i < MAXN; ++ i ) F[i] = T[i] = 0;
for ( int i = 0; i < len; ++ i ) F[i] = 1ll * ( MOD - n ) % MOD * G[i] % MOD;
Poly::polyExp ( len, F, T ), Poly::NTT ( MAXN, T, 1 );
for ( int i = 0; i < MAXN; ++ i ) F[i] = 1ll * T[i] * H[i] % MOD;
Poly::NTT ( MAXN, F, -1 );
printf ( "%d\n", int ( 1ll * inv[n] * fac[n - 1] % MOD * F[n - 1] % MOD ) );
}
int main () {
init ();
for ( int i = 1; i <= 5; ++ i ) scanf ( "%d", &n ), solve ();
return 0;
}