题面

传送门

前置芝士

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}\)

综上,答案为

\[Ans=\sum_{i=1}^{n-1}A_{n-2}^{i-1}{m-1\choose i-1}m^{n-1-i}(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(数数)的更多相关文章

  1. 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} 然后就 ...

  2. CF1109D Sasha and Interesting Fact from Graph Theory

    CF1109D Sasha and Interesting Fact from Graph Theory 这个 \(D\) 题比赛切掉的人基本上是 \(C\) 题的 \(5,6\) 倍...果然数学计 ...

  3. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

    Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...

  4. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 排列组合,Prufer编码

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1109D.html 题意 所有边权都是 [1,m] 中的整数的所有 n 个点的树中,点 a 到点 b 的距离 ...

  5. Codeforces1113F. Sasha and Interesting Fact from Graph Theory(组合数学 计数 广义Cayley定理)

    题目链接:传送门 思路: 计数.树的结构和边权的计数可以分开讨论. ①假设从a到b的路径上有e条边,那么路径上就有e-1个点.构造这条路径上的点有$A_{n-2}^{e-1}$种方案: ②这条路径的权 ...

  6. Sasha and Interesting Fact from Graph Theory CodeForces - 1109D (图论,计数,Caylay定理)

    大意: 求a->b最短路长度为m的n节点树的个数, 边权全部不超过m 枚举$a$与$b$之间的边数, 再由拓展$Caylay$定理分配其余结点 拓展$Caylay$定理 $n$个有标号节点生成k ...

  7. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  8. HDU6029 Graph Theory 2017-05-07 19:04 40人阅读 评论(0) 收藏

    Graph Theory                                                                 Time Limit: 2000/1000 M ...

  9. Graph Theory

    Description Little Q loves playing with different kinds of graphs very much. One day he thought abou ...

随机推荐

  1. Tomcat域名映射和端口设置

    1.打开tomcat主目录  --->  打开conf目录 ---> 找到并打开server.xml文件 2.修改tomcat的监听端口为80端口 在文件中找到: <Connecto ...

  2. SQL优化的部分内容

    为什么要优化:      随着实际项目的启动,数据库经过一段时间的运行,最初的数据库设置,会与实际数据库运行性能会有一些差异,这时我们         就需要做一个优化调整.   数据库优化这个课题较 ...

  3. Bean标签的常用属性

    -----------------siwuxie095 Bean 标签的常用属性 1.id 属性:Bean 的唯一标识名,必须以字母开头,且不能 包含特殊字符 2.class 属性:用来定义类的全限定 ...

  4. 拒绝用户登录:/bin/false和/usr/sbin/nologin

    要拒绝系统用户登录,可以将其shell设置为/usr/sbin/nologin或者/bin/false 1 # usermod -s | --shell /usr/sbin/nologin usern ...

  5. R学习笔记-安装R和RStudio,注意RStudio的版本需要与操作系统版本匹配

    1.安装步骤:先安装R,再安装RStudio RStudio是R的集成开发工具,本身不带R环境. 2.从当前R的官网和RStudio下载的R和RStudio的版本分别为: A .For Windows ...

  6. Git学习笔记——从一台电脑上传文件到Github上

    目标:从一台电脑上传文件到Github上 前提: 1.这里假定已在Github上创建了仓库,建立了仓库 2.已在这台电脑上安装了Git客户端 实验环境: 1.Windows 10 64位,已安装了Gi ...

  7. 如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。

    在配置文件中添加 <identity   impersonate= "true "   userName= "Administrator "   pass ...

  8. radio单选框

    1.写 <!DOCTYPE html> <html> <head> <title></title> <script language= ...

  9. windows10 查看进程端口的情况

    以程序 winnfsd.exe 为例: 1 查看进程号 PID C:\Users\leo>tasklist|findstr winnfsd.exe winnfsd.exe             ...

  10. DataStage 七、在DS中使用配置文件分配资源

    DataStage序列文章 DataStage 一.安装 DataStage 二.InfoSphere Information Server进程的启动和停止 DataStage 三.配置ODBC Da ...