(mathcal{Description})
link.
求 (n) 个结点的简单无向连通图个数,对 (1004535809~(479 imes2^{21}+1)) 取模。
(nle1.3 imes10^5)。
(mathcal{Solution})
很简单的一道生成函数题。做完之后可以尝试一下点双和边双连通图计数 w。
令 (f_i) 为 (i) 个结点的简单无向图个数。显然 (f_i=2^{ichoose 2})。则其生成函数 (F(x)) 有:
[F(x)=sum_{i=0}^{+infty}frac{2^{ichoose2}x^i}{i!}
]
考虑任意一个简单无向图肯定是由多个互相独立的简单无向连通图拼接而成。若 (G(x)) 是无向连通图的生成函数,则应有 (F(x)=exp G(x))。那么反过来,(G(x)=ln F(x))。求出 (F(x)),然后多项式求 (ln) 即可。
复杂度 (mathcal O(nlog n))。
(mathcal{Code})
#include <cmath>
#include <cstdio>
const int MAXN = 1 << 18, MOD = 1004535809;
int n, len, inv[MAXN + 5], fac[MAXN + 5], ifac[MAXN + 5], F[MAXN + 5], G[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 int adjust ( const int n ) {
int ret = 0;
for ( int l = 1; l < n; l <<= 1, ++ ret );
return ret;
}
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[MAXN + 5] {};
if ( n == 1 ) return void ( R[0] = qkpow ( A[0], MOD - 2 ) );
int len = 1 << adjust ( n << 1 );
polyInv ( n + 1 >> 1, A, R );
for ( int i = 0; i < n; ++ i ) tmp[i] = A[i];
NTT ( len, tmp, 1 ), NTT ( len, R, 1 );
for ( int i = 0; i < len; ++ i ) R[i] = ( 2 - 1ll * tmp[i] * R[i] % MOD + MOD ) % MOD * R[i] % MOD, tmp[i] = 0;
NTT ( len, R, -1 );
for ( int i = n; i < len; ++ i ) R[i] = 0;
}
inline void polyLn ( const int n, const int* A, int* R ) {
static int tmp[2][MAXN + 5] {};
int len = 1 << adjust ( n << 1 );
polyDer ( n, A, tmp[0] ), polyInv ( n, A, tmp[1] );
NTT ( len, tmp[0], 1 ), NTT ( len, tmp[1], 1 );
for ( int i = 0; i < len; ++ i ) tmp[0][i] = 1ll * tmp[0][i] * tmp[1][i] % MOD;
NTT ( len, tmp[0], -1 ), polyInt ( n, tmp[0], R );
for ( int i = 0; i < len; ++ i ) tmp[0][i] = tmp[1][i] = 0;
for ( int i = n; i < len; ++ i ) R[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 );
int len = 1 << adjust ( n << 1 );
polyExp ( n + 1 >> 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 ( len, tmp, 1 ), NTT ( len, R, 1 );
for ( int i = 0; i < len; ++ i ) R[i] = 1ll * R[i] * tmp[i] % MOD, tmp[i] = 0;
NTT ( len, R, -1 );
for ( int i = n; i < len; ++ i ) R[i] = 0;
}
} // namespace Poly.
inline void init () {
len = 1 << Poly::adjust ( n + 1 );
inv[1] = fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
for ( int i = 2; i <= len << 1; ++ 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;
}
}
int main () {
scanf ( "%d", &n ), init ();
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 );
printf ( "%d
", int ( 1ll * G[n] * fac[n] % MOD ) );
return 0;
}