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\) 确定的情况下的答案:

\[f(edges)=A(n-2,edges-1) \cdot C(m-1,edges-1)\cdot m^{n-edges-1} \cdot (edges+1) \cdot n^{n-edges-2}.
\]

  • 线性预处理 \(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的更多相关文章

  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. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

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

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

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

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

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

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

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

  6. CF1109DSasha and Interesting Fact from Graph Theory(数数)

    题面 传送门 前置芝士 Prufer codes与Generalized Cayley's Formula 题解 不行了脑子已经咕咕了连这么简单的数数题都不会了-- 首先这两个特殊点到底是啥并没有影响 ...

  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. oracle 根据出生日期计算年龄的年月日

    select years,months,abs( trunc( newer_date- add_months( older_date,years*12+months ) ) ) days from ( ...

  2. LOJ #10132. 「一本通 4.4 例 3」异象石

    题目地址 LOJ 题解 神仙思路.思路参考自<算法竞赛进阶指南>. 考虑维护dfs序中相邻两个石头的距离,那么每次?的答案就是sum/2(首尾算相邻) 然后维护一下拿个平衡树/set维护一 ...

  3. Configuration in ASP.NET Core(未完,待续)

    Configuration in ASP.NET Core App configuration in ASP.NET Core is based on key-value pairs establis ...

  4. IdentityServer4授权类型(GrantType)对应的返回类型(ResponseType)

    授权类型(GrantyType) 返回类型(ResponseType) authorization_code code implicit token implicit id_token implici ...

  5. Linux下更新Git

     查看git版本,卸载旧版本(如果没有安装git请直接到下一步) git --version yum remove git  安装依赖软件 yum install curl-devel expat-d ...

  6. es6 class的基本语法

    ES5以及之前的版本,没有类的概念,但是聪明的JavaScript开发者,为了实现面向对象,创建了特殊的近类结构. ES5中创建类的方法:新建一个构造函数,定义一个方法并且赋值给构造函数的原型. 'u ...

  7. 08.vue中样式-class

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Python打包项目为EXE程序

    安装pyinstaller 如果使用了VirtualENV环境,则必须在要打包的项目环境中安装... 否则会找不到项目需求的包和模块 pip install -i https://pypi.douba ...

  9. Spark面试相关

    Spark Core面试篇01 随着Spark技术在企业中应用越来越广泛,Spark成为大数据开发必须掌握的技能.前期分享了很多关于Spark的学习视频和文章,为了进一步巩固和掌握Spark,在原有s ...

  10. 封装axios在vue-cli项目中便捷使用

    首先创建一个vue-cli搭建起来的vue项目这个不用多说了. 安装axios,使用npm install axios --save命令安装依赖,这时候项目的package.json文件中的" ...