CF1109D Sasha and Interesting Fact from Graph Theory
CF1109D Sasha and Interesting Fact from Graph Theory
这个 \(D\) 题比赛切掉的人基本上是 \(C\) 题的 \(5,6\) 倍...果然数学计数问题比数据结构更受欢迎...
- 以下大致翻译自官方题解.
- 枚举 \(a\to b\) 路径上边的数目,记为 \(edges\) .
- 先来考虑给定的两个点路径上的 \(edges-1\) 个点(不含 \(a,b\) )和 \(edge\) 条边.
- 节点有\(edges-1\)个,顺序不同则最后的树不同,所以方案数为 \(A(n-2,edges-1)\) .
- 边有 \(edges\) 条,边权 \(v\) 需满足\(v \in \mathbb{N_+},v_1+v_2+...+v_{edges-1}+v_{edges}=m\).用隔板法可知方案数,即解的组数为 \(C(m-1,edges-1)\).
- 再来考虑其它的 \(n-edges-1\) 个点和 \(n-edges-1\) 条边.
- 由于其它边的边权显然不影响合法性,可以随意赋 \([1,m]\) 内的整数值,方案数为 \(m^{n-edges-1}\).
- 剩下的点我们需要使它们形成一个森林,并将每颗树挂在 \(a\to b\) 这 \(edges+1\) 个点上.这等价于所有的 \(n\) 个点形成一个 \(edges+1\) 颗树的森林,那 \(edges+1\) 个点都属于不同的树,然后将这 \(edges+1\) 个点连接起来.根据广义\(Cayley\)定理,方案数为 \((edges+1) \cdot n^{n-edges-2}\) .
广义 \(Cayley\) 定理:
\(n\) 个标号节点形成一个有 \(k\) 颗树的森林,使得给定的 \(k\) 个点没有两个点属于同一颗树的方案数为\(k\cdot n^{n-k-1}.\)
证明可以用归纳法,对 \(n\) 归纳,枚举节点 \(1\) 的邻居即可得递推式,进而得出证明.
- 那么我们就得到了在 \(edges\) 确定的情况下的答案:
\]
- 线性预处理 \(m,n\) 的幂,阶乘及阶乘逆元,枚举 \(edges\) 统计答案,时间复杂度为 \(O(n+m)\).
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline int read()
{
int x=0;
bool pos=1;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
pos=0;
for(;isdigit(ch);ch=getchar())
x=x*10+ch-'0';
return pos?x:-x;
}
const int P=1e9+7;
inline int add(int a,int b)
{
return (a + b) % P;
}
inline int mul(int a,int b)
{
return 1LL * a * b % P;
}
int fpow(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
res=mul(res,a);
a=mul(a,a);
b>>=1;
}
return res;
}
const int MAXN=1e6+10;
int fac[MAXN],invfac[MAXN],mpow[MAXN],npow[MAXN];
void init(int n,int m)
{
int mx=max(n,m);
fac[0]=1;
for(int i=1;i<=mx;++i)
fac[i]=mul(fac[i-1],i);
invfac[mx]=fpow(fac[mx],P-2);
for(int i=mx-1;i>=0;--i)
invfac[i]=mul(invfac[i+1],i+1);
mpow[0]=npow[0]=1;
for(int i=1;i<=n;++i)
mpow[i]=mul(mpow[i-1],m);
for(int i=1;i<=n;++i)
npow[i]=mul(npow[i-1],n);
}
int A(int n,int m)
{
if(n<m || n<0 || m<0)
return 0;
return mul(fac[n],invfac[n-m]);
}
int C(int n,int m)
{
if(n<m || n<0 || m<0)
return 0;
return mul(fac[n],mul(invfac[n-m],invfac[m]));
}
int main()
{
int n=read(),m=read();
int a=read(),b=read();
init(n,m);
int ans=0;
for(int edges=1;edges<n;++edges)
{
int tmp=mul(A(n-2,edges-1),C(m-1,edges-1));
tmp=mul(tmp,mpow[n-edges-1]);
tmp=mul(tmp,edges==n-1?1:mul(edges+1,npow[n-edges-2]));
ans=add(ans,tmp);
}
printf("%d\n",ans);
return 0;
}
CF1109D Sasha 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} 然后就 ...
- 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 ...
- CF1109DSasha and Interesting Fact from Graph Theory(数数)
题面 传送门 前置芝士 Prufer codes与Generalized Cayley's Formula 题解 不行了脑子已经咕咕了连这么简单的数数题都不会了-- 首先这两个特殊点到底是啥并没有影响 ...
- 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 ...
随机推荐
- iOS UIAlertController在iPhone与iPad上的区别
很简单的一段代码: // 首先声明一个UIAlertController对象 private var alertController: UIAlertController! // 初始化UIAlert ...
- Apache web服务器(LAMP架构)
Apache web服务器(LAMP架构) apache介绍 1).世界上使用率最高的网站服务器,最高时可达70%:官方网站:apache.org 2).http 超文本协议 HTML 超文本标记语言 ...
- 《Java程序设计》win10系统学前准备
<Java程序设计>win10系统学前准备 Git的安装 在https://gitforwindows.org/中下载git for windows,下载完成后进行安装.当安装进行到这一步 ...
- Python ORM框架之SQLAlchemy
前言: Django的ORM虽然强大,但是毕竟局限在Django,而SQLAlchemy是Python中的ORM框架: SQLAlchemy的作用是:类/对象--->SQL语句--->通过 ...
- redis基础学习总结
学习目标: 1.redis特点及安装 2.redis键值操作 3.redis数据类型[string, link,set,orderset,hash] 4.事务 5.消息 ...
- vscode plugins
├─ .obsolete├─ .wlck├─ .wtid├─ abeyuhang.vscode-lesslint-0.0.1├─ abierbaum.vscode-file-peek-1.0.1├─ ...
- trap(陷井)
if True: x = 15 print(x)print(x) # 可见 if 语句,不是一个代码块,因为代码块有独立的作用域,代码块结束时,会释放变量 l1 = [1,2,3,4]print(id ...
- Python—装饰器详解
装饰器:(语法糖) 本质是函数,它是赋予函数新功能,但是不改变函数的源代码及调用方式 原则: 1.不能修改被装饰函数的源代码 2.不能修改被装饰函数的调用方式 3.函数的返回值也不变 这两点简而言 ...
- ubuntu14.04安装 Apache2 并配置https
一.安装 Apache2 sudo apt-get update sudo apt-get install apache2 安装完apache2,默认根目录在/var/www/html 下,点击其下的 ...
- spring+springMvc+struts的SSH框架整合
1.建立一个web项目 2.导入SSH框架所需jar包 3.配置web.xml文件 <?xml version="1.0" encoding="UTF-8" ...