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. 安装cmake

    $ sudo apt-get install build-essential$ wget http://www.cmake.org/files/v3.11/cmake-3.11.3.tar.gz$ t ...

  2. 在数据库级别还是在service层进行级联删除

    在数据库配置级联删除的话,父表删除子表也删除.但是应该将维护代码放在一处,不要在service上删除父表,而在数据库层面级联删除子表,应该都在service层上进行删除.

  3. Codeforces Round #466 (Div. 2) -A. Points on the line

    2018-02-25 http://codeforces.com/contest/940/problem/A A. Points on the line time limit per test 1 s ...

  4. 利用JS打印质数

    我爱撸码,撸码使我感到快乐!大家好,我是Counter,今天非常愉快,没有前几天的相对比较复杂的逻辑思维在里面,今天来写写,利用JS打印质数,基本上很多面试,会很经常的考到.那废话不多说,直接上代码: ...

  5. 异常:unity3d ArgumentException: The Assembly System.Configuration is referenced by System.Data.

    异常:ArgumentException: The Assembly System.Configuration is referenced by System.Data. But the dll is ...

  6. 字段值为 null 时,序列化或反序列化成其他值

    using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.Collections.G ...

  7. mysql数据库连接出问题,提示超时 java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.解决办法

    mysql数据库连接出问题,提示超时,后来发现问题在于连接mysql数据库的jar包跟数据库版本不对应导致的,更换jar包一致就解决了.

  8. win平台下Path变量消失问题

    解决方法:2019.01.10文章转载自 李北北:https://www.jianshu.com/p/b89f0c99867e 问题描述:修改了path变量,但是环境变量中path消失,于是想再次打开 ...

  9. [C#]统计文本文件txt中的行数(快速读取)

    快速统计文本文件中的行数( StreamReader.ReadLine() ): 测试代码如下: //读取txt文件中总行数的方法 public static int requestMethod(St ...

  10. LeetCode--015--三元之和(java)

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...