HDU 5552 Bus Routes(NTT+分治)
题意
给定 \(n\) 个点,任意连边,每条边有 \(m\) 种颜色可选,求带环连通图的方案数。
\(1\leq n\leq 10000\)
\(1\leq m < 2^{31}\)
思路
直接求带环连通图显然比较难求,正难则反,考虑容斥。用连通图的个数减去无环连通图(树)的个数。
\(n\) 个节点的无根树,每个节点有区别,可以直接套用公式 \(n^{n-2}\) 。而再考虑边的颜色,就是 \(m^{n-1}n^{n-2}\) 。
我们设 \(n\) 个点,考虑边的颜色,构成不同连通图的方案数为 \(f(n)\) 。
直接求连通图还是不方便,那么我们再容斥:用图的个数减不连通图的个数,\(n\) 个点,考虑边的颜色,可以有 \((m+1)^{n(n+1)\over2}\) 种情况,设之为 \(g(n)\)。
有一个小 \(\text{trick}\) ,我们固定一个点,选一些点和它构成一个连通块,剩下的点任意构图,显然这样是可以不重不漏的,转移式如下
\]
化简得
\]
这样就是一个 \(n^2\) 的 \(dp\) 式,并且形式上满足多项式乘法的形式,只是 \(f\) 在右边出现了。
那我们只能考虑左边对右边的转移,不难想到\(\text{CDQ}\)分治。
void CDQ(int l,int r)
{
if(l==r){/*转移常量给dp[l]*/return;}
int mid=(l+r)>>1;
CDQ(l,mid);
/*处理[l,mid]的多项式和转移给[mid+1,r]的多项式*/
_Polynomial::multiply(/**/);
/*转移结果给dp[mid+1,r]*/
CDQ(mid+1,r);
return;
}
代码流程如上,在分治过程中考虑左边转移给右边,需保证在转移前,左边的值以计算完毕。
\(\text{dp}\)式一般写成 \(dp_i=A_i\cdot \sum dp_jf_{i-j}+B_i\) 看的会比较清晰。
代码
#include<bits/stdc++.h>
#define FOR(i,x,y) for(int i=(x),i##END=(y);i<=i##END;++i)
#define DOR(i,x,y) for(int i=(x),i##END=(y);i>=i##END;--i)
using namespace std;
template<typename T,typename _T>inline bool chk_min(T &x,const _T y){return y<x?x=y,1:0;}
template<typename T,typename _T>inline bool chk_max(T &x,const _T y){return x<y?x=y,1:0;}
typedef long long ll;
const int P=152076289;
const int N=1<<14|5;
namespace _Maths
{
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b){x=1,y=0;return;}
exgcd(b,a%b,y,x),y-=a/b*x;
}
ll Pow(ll a,ll p,ll P)
{
ll res=1;
for(;p>0;p>>=1,(a*=a)%=P)if(p&1)(res*=a)%=P;
return res;
}
ll inv(ll a,ll P){ll x,y;exgcd(a,P,x,y);return (x%P+P)%P;}
};
using namespace _Maths;
namespace _Polynomial
{
const int g=106;
int A[N<<1],B[N<<1];
int w[N<<1],r[N<<1];
void DFT(int *a,int op,int n)
{
FOR(i,0,n-1)if(i<r[i])swap(a[i],a[r[i]]);
for(int i=2;i<=n;i<<=1)
for(int j=0;j<n;j+=i)
for(int k=0;k<i/2;k++)
{
int u=a[j+k],t=(ll)w[op==1?n/i*k:(n-n/i*k)&(n-1)]*a[j+k+i/2]%P;
a[j+k]=(u+t)%P,a[j+k+i/2]=(u-t)%P;
}
if(op==-1)
{
int I=inv(n,P);
FOR(i,0,n-1)a[i]=(ll)a[i]*I%P;
}
}
void multiply(const int *a,const int *b,int *c,int n1,int n2)
{
int n=1;
while(n<n1+n2-1)n<<=1;
FOR(i,0,n1-1)A[i]=a[i];
FOR(i,0,n2-1)B[i]=b[i];
FOR(i,n1,n-1)A[i]=0;
FOR(i,n2,n-1)B[i]=0;
FOR(i,0,n-1)r[i]=(r[i>>1]>>1)|((i&1)*(n>>1));
w[0]=1,w[1]=Pow(g,(P-1)/n,P);
FOR(i,2,n-1)w[i]=(ll)w[i-1]*w[1]%P;
DFT(A,1,n),DFT(B,1,n);
FOR(i,0,n-1)A[i]=(ll)A[i]*B[i]%P;
DFT(A,-1,n);
FOR(i,0,n1+n2-2)c[i]=(A[i]+P)%P;
}
};
int A[N],B[N],C[N<<1];
int fac[N],ifac[N],f[N],g[N];
int n;ll m;
void CDQ(int l,int r)
{
if(l==r){f[l]=(g[l]-(ll)fac[l-1]*f[l]%P)%P;return;}
int mid=(l+r)>>1;
CDQ(l,mid);
FOR(i,l,mid)A[(i)-l]=(ll)f[i]*ifac[i-1]%P;
FOR(i,1,r-l)B[(i)-1]=(ll)g[i]*ifac[i]%P;
_Polynomial::multiply(A,B,C,mid-l+1,r-l);
FOR(i,mid+1,r)f[i]=((ll)f[i]+C[(i)-l-1])%P;
CDQ(mid+1,r);
}
int main()
{
fac[0]=fac[1]=1;FOR(i,2,N-1)fac[i]=(ll)fac[i-1]*i%P;
ifac[0]=ifac[1]=1;FOR(i,2,N-1)ifac[i]=(ll)(P-P/i)*ifac[P%i]%P;
FOR(i,2,N-1)ifac[i]=(ll)ifac[i-1]*ifac[i]%P;
int T;
scanf("%d",&T);
FOR(Ti,1,T)
{
scanf("%d%lld",&n,&m);
FOR(i,1,n)f[i]=0;
FOR(i,1,n)g[i]=Pow(m+1,(ll)i*(i-1)/2,P);
CDQ(1,n);
printf("Case #%d: %lld\n",Ti,(((ll)f[n]-Pow(n,n-2,P)*Pow(m,n-1,P))%P+P)%P);
}
return 0;
}
HDU 5552 Bus Routes(NTT+分治)的更多相关文章
- hdu 5552 Bus Routes
hdu 5552 Bus Routes 考虑有环的图不方便,可以考虑无环连通图的数量,然后用连通图的数量减去就好了. 无环连通图的个数就是树的个数,又 prufer 序我们知道是 $ n^{n-2} ...
- HDU 5552 Bus Routes(2015合肥现场赛A,计数,分治NTT)
题意 给定n个点,任意两点之间可以不连边也可以连边.如果连边的话可以染上m种颜色. 求最后形成的图,是一个带环连通图的方案数. 首先答案是n个点的图减去n个点能形成的树. n个点能形成的树的方案数比 ...
- HDU 5322 Hope ——NTT 分治 递推
发现可以推出递推式.(并不会) 然后化简一下,稍有常识的人都能看出这是一个NTT+分治的情况. 然而还有更巧妙的方法,直接化简一下递推就可以了. 太过巧妙,此处不表,建议大家找到那篇博客. 自行抄写 ...
- URAL 1137 Bus Routes(欧拉回路路径)
1137. Bus Routes Time limit: 1.0 secondMemory limit: 64 MB Several bus routes were in the city of Fi ...
- hdu 3842 Machine Works(cdq分治维护凸壳)
题目链接:hdu 3842 Machine Works 详细题解: HDU 3842 Machine Works cdq分治 斜率优化 细节比较多,好好体会一下. 在维护斜率的时候要考虑x1与x2是否 ...
- [LeetCode] Bus Routes 公交线路
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...
- [Swift]LeetCode815. 公交路线 | Bus Routes
We have a list of bus routes. Each routes[i]is a bus route that the i-th bus repeats forever. For ex ...
- LeetCode解题报告—— Bus Routes
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...
- [LeetCode] 815. Bus Routes 公交路线
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...
随机推荐
- vue里v-for下的key的作用
将v-for的元素赋予唯一的key属性,则会打破‘就地复用原则’: 这个就地复用原则是指 如果数据项的顺序被改变,Vue 将不会移动 DOM 元素来匹配数据项的顺序, 而是简单复用此处每个元素,并且确 ...
- mui 窗口管理及窗口之间的数据传递
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 几个dos命令
- javascript的数组之sort()
sort()方法用in-place的算法对原数组进行排序,但不会产生新的数组.这个方法不是一个稳定的排序,默认采用的是安字符串Unicode码点进行排序的. let fruit = ['cherri ...
- HTML、CSS知识点,面试开发都会需要--No.4 内容布局
No.4 内容布局 1.列举场景 同一行布局三个元素.三个元素等比显示,并且其他元素不会围绕这三个元素.如下要让下面的三个column等比显示在一行: 2.通过Float属性实现 (1)float:l ...
- InnoDB中锁的算法(2)
Ⅰ.上节回顾 session1: (root@localhost) [test]> select * from l; +---+------+------+------+ | a | b | c ...
- Coroutines declared with async/await syntax is the preferred way of writing asyncio applications. For example, the following snippet of code (requires Python 3.7+) prints “hello”, waits 1 second, and
小结: 1.异步io 协程 Coroutines and Tasks — Python 3.7.3 documentation https://docs.python.org/3/library/a ...
- datagridview的一些设置
1.自动调整列宽 this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMo ...
- mongodb 3.2 分片 + 副本集
从图中可以看到有四个组件:mongos.config server.shard.replica set. mongos,数据库集群请求的入口,所有的请求都通过mongos进行协调,不需要在应用程序添加 ...
- 代码中特殊的注释技术——TODO、FIXME和XXX的用处(转)
1.声明 本篇转自博客:http://blog.csdn.net/reille/ 2.转载内容 2.1.前言 今天在阅读 Qt Creator 的源代码时,发现一些注释中有 FIXME 英文单词,用英 ...