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 ...
随机推荐
- Maven项目出现Perhaps you are running on a JRE rather than a JDK?
今天 换了一个IDE 然后 运行 maven的时候 报了一个 这个 错误 我记得以前 我遇到过 所以 把解决方法 记下来吧 原因 maven插件需要使用jdk 的 但是 eclipse默认 ...
- vue文件中引入外部js
1.在项目的入口文件中(app.js)定义remoteScript标签 Vue.component('remote-script', { render: function (createElement ...
- ArcGIS AddIN开发之 设置当前工具为Edit Tool
在GIS数据处理中,经常需要选择要素,再进行操作.所以,为了处理的方便,可以将当前工具处理结束后,将当前工具设置为Edit Tool,以方便下一次的选择处理. 相关资料: 1.ArcMap Name ...
- asp.net session锁导致ajax请求阻塞
问:为了可以顺序访问Session的状态值,Session是否提供了锁定机制?答:Session实现了Reader/Writer的锁机制:当页面对Session具有可写功能(即页面有<%@Pag ...
- 有关vue开发的小经验
注册登录已经成为布局的常事,这里为大家推荐两种超级好用的布局display:flex;justify-content:center;这个可以解决水平居中的疑难杂症 align-center:cente ...
- jmeter安装与环境变量配置
因jmeter是java开发的,要想运行java开发的程序,必须先下载JDK一.jdk 1.下载jdk jdk下载地址:https://www.oracle.com/technetwork/java ...
- 程序员调 Bug 的样子,非常真实
程序员调 Bug 的样子,非常真实
- iOS 微信打开第三方应用(Universal Links 和 URL Schemes)
一.前言 项目中时常有这种需求, 是通过链接跳转到应用内部,现在iOS主流的方案有两个 Schema: 常用在于一个应用跳转到另一个应用内部,属于应用间的跳转.当然ios9以下,网页可以通过schem ...
- Android studio中导入SlidingMenu问题
我们导入的library文件夹中的build.gradle 文件里面写的很清楚: android { compileSdkVersion 17 buildToolsVersion &q ...
- python进阶之 进程&线程区别
1.进程创建方式 import time import os from multiprocessing import Process def func (): time.sleep(1) print( ...