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 ...
随机推荐
- canvas和svg的区别
讨论关于canvas和svg的区别.首先canvas是html5提供的新元素<canvas>,而svg存在的历史要比canvas久远,已经有十几年了.svg并不是html5专有的标签,最初 ...
- UGUI 事件穿透规则
UGUI事件分为两大类:点击和拖拽. 点击包括 pointerdown, pointerup. 拖拽包括 begindrag, drag, enddrag. 点击事件无穿透:只会被最上层UI响应,不会 ...
- 使用 phpStorm 开发
苦恼蛋疼的曾哥工作室,让人痛不欲生,缓慢的同步速度,另人恼火的插件配置,让人疯狂的卡.简直是让人用了几天之后就不行了. 废话不多说,一款很好的php IDE. 1. phpStorm 下载 here ...
- 小学生作业V2.0
211606320刘佳&211506332熊哲琛 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Plann ...
- 【Python爬虫】听说你又闹书荒了?豆瓣读书9.0分书籍陪你过五一
说明 五一将至,又到了学习的季节.目前流行的各大书单主打的都是豆瓣8.0评分书籍,却很少有人来聊聊这9.0评分的书籍长什么样子.刚好最近学了学python爬虫,那就拿豆瓣读书来练练手. 爬虫 本来思路 ...
- 79. Word Search (Array; DFS,Back-Track)
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 8-linux 安装 requests 时 pip install 安装不了
安装提示更新:但是必须要sudo才行: sudo pip install --upgrade pip 安装 requests时有报错:这样写可以: sudo python -m pip install ...
- Putty建立隧道的方法[z]
通过上节我们了解了SSH隧道的优点,但是无论是现实中还是网络中,隧道都是有入口和出口的,本节就以Putty隧道为例,详细介绍Putty建立隧道的方法. 模拟案例一: 端口,出口为2012端口,隧道建立 ...
- 利率计算--web版--软件工程
1.客户说:帮我开发一个复利计算软件. 完成复利公式计算程序,并成功PUSH到github上. 截止时间:3.10晚12点之前. 按照这个要求完成了. 演示. 计算:本金为100万,利率或者投资回报率 ...
- Laravel 5 项目部署到生产环境的实践
作者:mrcn链接:https://www.zhihu.com/question/35537084/answer/181734431来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...