BZOJ3456 城市规划 分治NTT

Posted Mychael

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ3456 城市规划 分治NTT相关的知识,希望对你有一定的参考价值。

题目链接

BZOJ3456

题解

据说这题是多项式求逆
我太弱不会QAQ,只能\(O(nlog^2n)\)分治\(NTT\)

\(f[i]\)表示\(i\)个节点的简单无向连通图的数量
考虑转移,直接求不好求,我们知道\(n\)个点无向图的数量是\(2^{{n \choose 2}}\)的,考虑用总数减去不连通的
既然图不连通,那么和\(1\)号点联通的点数一定小于\(n\),我们枚举和\(1\)号点所在联通块大小,就可以得到式子:
\[f[n] = 2^{{n \choose 2}} - \sum\limits_{i = 1}^{n - 1}{n - 1 \choose i - 1}2^{{n - i \choose 2}}f[i]\]
展开组合数变形得:
\[f[n] = 2^{{n \choose 2}} - (n - 1)!\sum\limits_{i = 1}^{n - 1}\frac{f[i] * i}{i!} * \frac{2^{n - i \choose 2}}{(n - i)!}\]
分治NTT即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define cls(s) memset(s,0,sizeof(s))
#define LL long long int
#define res register
using namespace std;
const int maxn = 500000,maxm = 100005,INF = 1000000000,P = 1004535809;
const int G = 3;
int N,f[maxn],fac[maxn],fv[maxn],inv[maxn],C2[maxn];
int A[maxn],B[maxn],R[maxn],w[2][maxn],L,n,m;
inline int qpow(int a,LL b){
    int ans = 1;
    for (; b; b >>= 1,a = 1ll * a * a % P)
        if (b & 1) ans = 1ll * ans * a % P;
    return ans;
}
inline void NTT(int* a,int f){
    for (res int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
    for (res int i = 1; i < n; i <<= 1){
        int gn = w[f][i];
        for (res int j = 0; j < n; j += (i << 1)){
            int g = 1,x,y;
            for (res int k = 0; k < i; k++,g = 1ll * g * gn % P){
                x = a[j + k]; y = 1ll * g * a[j + k + i] % P;
                a[j + k] = (x + y) % P; a[j + k + i] = (x - y + P) % P;
            }
        }
    }
    if (f == 1) return;
    int nv = qpow(n,P - 2);
    for (res int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
void solve(int l,int r){
    if (l == r){
        f[l] = ((C2[l] - 1ll * fac[l - 1] * f[l] % P) % P + P) % P;
        return;
    }
    int mid = l + r >> 1,t;
    solve(l,mid);
    m = (mid - l) + (r - l); L = 0;
    for (n = 1; n <= m; n <<= 1) L++;
    for (int i = 0; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
    for (int i = 0; i < n; i++) A[i] = B[i] = 0;
    t = mid - l + 1;
    for (int i = 0; i < t; i++)
        A[i] = 1ll * f[l + i] * fv[l + i - 1] % P;
    t = r - l;
    B[0] = 0; for (int i = 1; i <= t; i++)
        B[i] = 1ll * C2[i] * fv[i] % P;
    NTT(A,1); NTT(B,1);
    for (int i = 0; i < n; i++) A[i] = 1ll * A[i] * B[i] % P;
    NTT(A,0);
    for (int i = mid + 1; i <= r; i++){
        f[i] = (f[i] + A[i - l]) % P;
    }
    solve(mid + 1,r);
}
int main(){
    scanf("%d",&N);
    fac[0] = fac[1] = inv[0] = inv[1] = fv[0] = fv[1] = 1;
    for (res int i = 2; i <= N; i++){
        fac[i] = 1ll * fac[i - 1] * i % P;
        inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
        fv[i] = 1ll * fv[i - 1] * inv[i] % P;
    }
    for (res int i = 1; i <= N; i++){
        C2[i] = qpow(2,1ll * i * (i - 1) / 2);
    }
    for (res int i = 1; i < maxn; i <<= 1){
        w[1][i] = qpow(G,(P - 1) / (i << 1));
        w[0][i] = qpow(G,(-(P - 1) / (i << 1) % (P - 1) + (P - 1)) % (P - 1));
    }
    solve(1,N);
    printf("%d\n",f[N]);
    return 0;
}

以上是关于BZOJ3456 城市规划 分治NTT的主要内容,如果未能解决你的问题,请参考以下文章

bzoj 3456: 城市规划NTT+多项式求逆

bzoj3456城市规划 容斥原理+NTT+多项式求逆

[bzoj3456]城市规划:多项式,分治

bzoj 3456 城市规划 多项式求逆+分治FFT

[bzoj3456]城市规划——分治FFT

bzoj3456