CF1109DSasha and Interesting Fact from Graph Theory(数数)
题面
前置芝士
Prufer codes与Generalized Cayley's Formula
题解
不行了脑子已经咕咕了连这么简单的数数题都不会了……
首先这两个特殊点到底是啥并没有影响,我们假设它们为\(1,2\)好了
首先,我们需要枚举\(1,2\)之间的边数\(i\)
我们需要考虑这中间的\(i-1\)个点是哪些点,而且它们的顺序对答案有影响,方案数乘上\(A_{n-2}^{i-1}\)
这\(i\)条边的的和要为\(m\),根据隔板法,方案数要乘上\({m-1\choose i-1}\)
剩下的边取值随便,方案数乘上\(m^{n-1-i}\)
我们要把\(n\)个点分成\(i\)棵树,且如果把中间的点依次标号为\(3,4,...,i+1\),它们所在的树要互不相同,根据\(Generalized\ Cayley's\ Formula\),方案数为\((i+1)n^{n-i-2}\)
综上,答案为
\]
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
const int N=1e6+5,P=1e9+7;
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
R int res=1;
for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;
return res;
}
int fac[N],ifac[N],n,m,p,invn,invm,rn,rm,res;
inline int C(R int n,R int m){return 1ll*fac[n]*ifac[m]%P*ifac[n-m]%P;}
inline int A(R int n,R int m){return mul(fac[n],ifac[n-m]);}
int main(){
// freopen("testdata.in","r",stdin);
scanf("%d%d",&n,&m),p=max(n,m);
fac[0]=ifac[0]=1;fp(i,1,p)fac[i]=mul(fac[i-1],i);
ifac[p]=ksm(fac[p],P-2);fd(i,p-1,1)ifac[i]=mul(ifac[i+1],i+1);
invn=ksm(n,P-2),invm=ksm(m,P-2),p=min(n-1,m),rn=rm=1;
fp(i,1,n-2)rn=mul(rn,n),rm=mul(rm,m);rn=mul(rn,invn);
fp(i,1,p)res=add(res,1ll*A(n-2,i-1)*C(m-1,i-1)%P*rn%P*rm%P*(i+1)%P),rn=mul(rn,invn),rm=mul(rm,invm);
printf("%d\n",res);
return 0;
}
CF1109DSasha and Interesting Fact from Graph Theory(数数)的更多相关文章
- Codeforces 1109D Sasha and Interesting Fact from Graph Theory (看题解) 组合数学
Sasha and Interesting Fact from Graph Theory n 个 点形成 m 个有标号森林的方案数为 F(n, m) = m * n ^ {n - 1 - m} 然后就 ...
- CF1109D Sasha and Interesting Fact from Graph Theory
CF1109D Sasha and Interesting Fact from Graph Theory 这个 \(D\) 题比赛切掉的人基本上是 \(C\) 题的 \(5,6\) 倍...果然数学计 ...
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory
Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 排列组合,Prufer编码
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1109D.html 题意 所有边权都是 [1,m] 中的整数的所有 n 个点的树中,点 a 到点 b 的距离 ...
- Codeforces1113F. Sasha and Interesting Fact from Graph Theory(组合数学 计数 广义Cayley定理)
题目链接:传送门 思路: 计数.树的结构和边权的计数可以分开讨论. ①假设从a到b的路径上有e条边,那么路径上就有e-1个点.构造这条路径上的点有$A_{n-2}^{e-1}$种方案: ②这条路径的权 ...
- Sasha and Interesting Fact from Graph Theory CodeForces - 1109D (图论,计数,Caylay定理)
大意: 求a->b最短路长度为m的n节点树的个数, 边权全部不超过m 枚举$a$与$b$之间的边数, 再由拓展$Caylay$定理分配其余结点 拓展$Caylay$定理 $n$个有标号节点生成k ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- HDU6029 Graph Theory 2017-05-07 19:04 40人阅读 评论(0) 收藏
Graph Theory Time Limit: 2000/1000 M ...
- Graph Theory
Description Little Q loves playing with different kinds of graphs very much. One day he thought abou ...
随机推荐
- fft 远程服务器返回错误 550返回码
"远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)"时,可能是如下原因: 1.URL路径不对,看看有没有多加空格,或者大小写问题 2.权限是否足 3.需 ...
- http://www.5xcg.com/bbs/forum.php?mod=viewthread&tid=51143&extra=page%3D1
http://www.5xcg.com/bbs/forum.php?mod=viewthread&tid=51143&extra=page%3D1 因为身在酒店设备有限,只能尽量把文字 ...
- cluster DNS
[root@mhc1 dns]# pwd/root/test/k8s/kubernetes/cluster/addons/dns [root@mhc1 dns]# export DNS_SERVER_ ...
- Linux centos下php安装cphalcon扩展的方法
说明: 操作系统:CentOS php安装目录:/usr/local/php php.ini配置文件路径:/usr/local/php/etc/php.ini 1.安装cphalcon cd /usr ...
- springmvc 请求无法到达controller,出现404
今天在配置SpringMVC时,访问项目一直出现404,无法访问. 报错: The origin server did not find a current representation for th ...
- Cocoa Touch(三):图形界面UIKit、Core Animation、Core Graphics
UIKit 视图树模型 1.视图树模型 计算机图形实际上是一个视图树模型,每个视图都有一个本地坐标系.每个本地坐标系的组成部分是:原点在父坐标系中的位置,每个基在父坐标系中的位置,由此就可以根据向量的 ...
- spring boot 1
1.创建项目. meven可以配置阿里云meven镜像 <mirror> <id>nexus-aliyun</id> <mirrorOf>central ...
- 初次使用Xcode遇到的问题及解决方法
使用的是Xcode 5.1.1 版本 1.调整字体 点击左上角的Xcode->Preference->Font & colors .需要注意到是,只有选择下图中黑色框框里面的一行 ...
- nvidia 驱动下载地址
http://www.nvidia.com/Download/index.aspx?lang=en-us
- sublime主题选择
Sublime Text是一个强大的令人难以置信的编辑器. 它不仅具有大量的功能,而且还有很多主题,让他看起来很漂亮. 我们以前整理过 2014年的最好的主题 ; 让我们看看最新的Sublime Te ...