题面

传送门

前置芝士

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. 关于document.cookie的使用

    设置cookie每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie:document.cookie="userId=828";如果要一次 ...

  2. nodejs API(二)

    官网所在位置:https://nodejs.org/dist/latest-v6.x/docs/api/querystring.html querystring.escape(str) 转义   qu ...

  3. CentOS Linux最常用命令及快捷键整理

    最近一直在对CentOS系统进行各种体验,为方便自己也方便他人,整理了Linux常用命令及快捷键,不过其实大多和DOS是一样的,只是命令的表达上可能有点儿不一样. 常用Linux命令: 文件和目录: ...

  4. 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序

    实现EXCEL导入功能:web网站,excel导入用OLEDB同样会报错:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 报错信息: Exception caught. ...

  5. 【bzoj1096】仓库建设 斜率优化dp

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1096 [题解] 设输入的三个数组为a,b,c sumb维护b数组的前缀和,sumab维护a ...

  6. react-navigation 3.x版本的使用

    安装配置请看: react-navigation 3.x版本的安装以及react-native-gesture-handler配置 2.0以前版本: StackNavigator - 一次只渲染一个页 ...

  7. Java常用的输出调试技巧

    --------siwuxie095                 Eclipse 开发中常用的输出调试技巧:     先在左侧的 Package Explorer,右键->New->J ...

  8. Gcc And MakeFile Level1

    简单介绍gcc And make 的使用 基本编译 gcc a.c b.c -o exeName 分步编译 gcc -c a.c -o a.o gcc a.o b.c -o main.o 使用Make ...

  9. Java面向对象理解_代码块_继承_多态_抽象_接口

    面线对象: /* 成员变量和局部变量的区别? A:在类中的位置不同 成员变量:在类中方法外 局部变量:在方法定义中或者方法声明上 B:在内存中的位置不同 成员变量:在堆内存 局部变量:在栈内存 C:生 ...

  10. ubuntu 出来菜单栏和任务栏

    http://blog.csdn.net/terence1212/article/details/51340595 命令行输入:sudo apt-get install compizconfig-se ...