[BZOJ3456]城市规划:DP+NTT+多项式求逆
写在前面的话
昨天听吕老板讲课,数数题感觉十分的神仙。
于是,ErkkiErkko这个小蒟蒻也要去学数数题了。
分析
带标号无向连通图计数。
\(f(x)\)表示\(x\)个点的带标号无向连通图的个数。弱化限制条件,令\(g(x)\)表示\(x\)个点的带标号无向图的个数(不要求连通)。
考虑每条边是否出现,显然有:
\]
考虑编号为\(1\)的结点所在连通块的大小,有:
\]
把组合数拆开,
\]
于是就可以用多项式求逆求出\(f(n)\)了。
代码
#include <bits/stdc++.h>
#define rin(i,a,b) for(register int i=(a);i<=(b);++i)
#define irin(i,a,b) for(register int i=(a);i>=(b);--i)
#define trav(i,a) for(register int i=head[a];i;i=e[i].nxt)
typedef long long LL;
using std::cin;
using std::cout;
using std::endl;
inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int MAXN=130005;
const int NTT=1048576;
const LL MOD=1004535809;
const LL G=3;
const LL INVG=334845270;
int N,n,m,len;
LL w[NTT+5],iw[NTT+5];
LL fac[MAXN],invf[MAXN];
LL g[MAXN];
LL rev[MAXN<<2],A[MAXN<<2],B[MAXN<<2];
inline LL qpow(LL x,LL y){
LL ret=1,tt=x%MOD;
while(y){
if(y&1) ret=ret*tt%MOD;
tt=tt*tt%MOD;
y>>=1;
}
return ret;
}
void ntt(LL *c,int dft){
rin(i,0,n-1)
if(i<rev[i])
std::swap(c[i],c[rev[i]]);
for(register int mid=1;mid<n;mid<<=1){
int r=(mid<<1),u=NTT/r;
for(register int l=0;l<n;l+=r){
int v=0;
for(register int i=0;i<mid;++i,v+=u){
LL x=c[l+i],y=c[l+mid+i]*(dft>0?w[v]:iw[v])%MOD;
c[l+i]=x+y<MOD?x+y:x+y-MOD;
c[l+mid+i]=x-y>=0?x-y:x-y+MOD;
}
}
}
if(dft<0){
LL invn=qpow(n,MOD-2);
rin(i,0,n-1) c[i]=c[i]*invn%MOD;
}
}
void getinv(int mdx){
if(mdx==1){
A[0]=qpow(g[0],MOD-2);
return;
}
getinv((mdx+1)>>1);
m=(mdx-1)+((((mdx+1)>>1)-1)<<1),len=0;
for(n=1;n<=m;n<<=1) ++len;
rin(i,1,n-1) rev[i]=((rev[i>>1]>>1)|((i&1)<<(len-1)));
rin(i,0,n-1) B[i]=i<mdx?g[i]:0;
ntt(A,1);ntt(B,1);
rin(i,0,n-1) A[i]=(2*A[i]-B[i]*A[i]%MOD*A[i]%MOD+MOD)%MOD;
ntt(A,-1);
rin(i,mdx,n-1) A[i]=0;
}
void init(){
LL v=qpow(G,(MOD-1)/NTT),iv=qpow(INVG,(MOD-1)/NTT);
w[0]=iw[0]=1;
rin(i,1,NTT-1) w[i]=w[i-1]*v%MOD,iw[i]=iw[i-1]*iv%MOD;
fac[0]=1;
rin(i,1,N) fac[i]=fac[i-1]*i%MOD;
invf[N]=qpow(fac[N],MOD-2);
irin(i,N-1,0) invf[i]=invf[i+1]*(i+1)%MOD;
}
int main(){
N=read();
if(N==0){
printf("1\n");
return 0;
}
init();
g[0]=1;
rin(i,1,N) g[i]=qpow(2,1ll*i*(i-1)/2)*invf[i]%MOD;
getinv(N+1);
m=(N<<1),len=0;
for(n=1;n<=m;n<<=1) ++len;
rin(i,0,n-1) rev[i]=((rev[i>>1]>>1)|((i&1)<<(len-1)));
B[0]=0;
rin(i,1,N) B[i]=qpow(2,1ll*i*(i-1)/2)*invf[i-1]%MOD;
ntt(A,1);ntt(B,1);
rin(i,0,n-1) A[i]=A[i]*B[i]%MOD;
ntt(A,-1);
printf("%lld\n",A[N]*fac[N-1]%MOD);
}
[BZOJ3456]城市规划:DP+NTT+多项式求逆的更多相关文章
- 【bzoj3456】城市规划 容斥原理+NTT+多项式求逆
题目描述 求出n个点的简单(无重边无自环)无向连通图数目mod 1004535809(479 * 2 ^ 21 + 1). 输入 仅一行一个整数n(<=130000) 输出 仅一行一个整数, 为 ...
- 【BZOJ 3456】 3456: 城市规划 (NTT+多项式求逆)
3456: 城市规划 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 658 Solved: 364 Description 刚刚解决完电力网络的问题 ...
- [bzoj3456] 城市规划 [递推+多项式求逆]
题面 bzoj权限题面 离线题面 思路 orz Miskcoo ! 先考虑怎么算这个图的数量 设$f(i)$表示$i$个点的联通有标号无向图个数,$g(i)$表示$n$个点的有标号无向图个数(可以不连 ...
- bzoj 3456: 城市规划【NTT+多项式求逆】
参考:http://blog.miskcoo.com/2015/05/bzoj-3456 首先推出递推式(上面的blog讲的挺清楚的),大概过程是正难则反,设g为n个点的简单(无重边无自环)无向图数目 ...
- BZOJ 3456 城市规划 ( NTT + 多项式求逆 )
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3456 题意: 求出\(n\)个点的简单(无重边无自环)无向连通图的个数.(\(n< ...
- bzoj 3456 城市规划——分治FFT / 多项式求逆 / 多项式求ln
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3456 分治FFT: 设 dp[ i ] 表示 i 个点时连通的方案数. 考虑算补集:连通的方 ...
- bzoj 3456 城市规划 —— 分治FFT / 多项式求逆 / 指数型生成函数(多项式求ln)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3456 首先考虑DP做法,正难则反,考虑所有情况减去不连通的情况: 而不连通的情况就是那个经典 ...
- P4233-射命丸文的笔记【NTT,多项式求逆】
正题 题目链接:https://www.luogu.com.cn/problem/P4233 题目大意 随机选择一条有哈密顿回路的\(n\)个点的竞赛图,求选出图的哈密顿回路的期望个数. 对于每个\( ...
- NTT+多项式求逆+多项式开方(BZOJ3625)
定义多项式$h(x)$的每一项系数$h_i$,为i在c[1]~c[n]中的出现次数. 定义多项式$f(x)$的每一项系数$f_i$,为权值为i的方案数. 通过简单的分析我们可以发现:$f(x)=\fr ...
随机推荐
- MySQL -2- 体系结构
1. 体系结构 1.1 C/S(客户端/服务端)模型介绍 image TCP/IP方式(远程.本地): mysql -uroot -poldboy123 -h 10.0.0.51 -P3306 S ...
- D - 卿学姐与魔法
卿学姐与魔法 Time Limit: 1200/800MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...
- php Excel 导入
php Excel 导入 public function storeSql() { $file = input('file.excel'); $path = ROOT_PATH . 'public' ...
- HDU 1052 Tian Ji -- The Horse Racing (贪心)(转载有修改)
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- Vue中的组件直接的通信是如何实现的
组件关系可分为父子组件通信.兄弟组件通信 1.父组件传子组件 通过props属性来实现 2.子组件传父组件 子组件用$emit()来触发事件,父组件用$on()来监听子组件的事件 3.兄弟之间的通信 ...
- Redis【4】Java Jedis 操作 Redis~
package redis.redis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * 描 ...
- Struts2.5以上版本There is no Action mapped for namespace [/] and action name [userAction_login] associated with context path []
分析:Struts2在2.5版本后添加strict-method-invocation(严格方法访问),默认为true,不能使用动态方法调用功能,故需设为false struts.xml设置如下: & ...
- 在控制台编译运行java程序详细指导
控制台编译运行.java文件 首先在cmd中输入java –version确定java环境变量是否已经配好 其次在cmd中输入javac –version 确定javac环境变量是否已经配好 在用cd ...
- 2019长安大学ACM校赛网络同步赛 L XOR (规律,数位DP)
链接:https://ac.nowcoder.com/acm/contest/897/L 来源:牛客网 XOR 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- hdu 1003 最大连续子串
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...