Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

解题思路

这题我根本不会做,是周指导带飞我.

首先对于当前已经有 \(m\) 个联通块的有标号生成树的数量是

\[n^{m-2}\prod_{i=1}^msize_i
\]

其中 \(size_i\) 是第 \(i\) 个联通块的大小.

原理就是考虑 \(prufer\) 编码,先把每个联通块看成一个点,那么序列中每出现一个第 \(i\) 联通块缩成的点,能连的边的数量是 \(size[i]\) ,所以序列每一位的方案数是 \(\sum size[i]=n\),考虑每一个点的度数是在序列中的出现次数\(+1\),所以对于每一个联通块还要补上一条连边的方案数.

然后这个题相当于就是确定了一条链,在剩下 \(n-i-2\) 个联通块的基础上求有标号生产树数量,其中 \(i\) 是 \(a,b​\) 之间的点数,根据上面的式子,可以得到答案的式子

\[ans = \sum_{i=0}^{n-2}\binom{m-1}{i}\binom{n-2}{i}\times i!\times n^{n-i-3}\times (i+2)\times m^{n-i-2}
\]

code

/*program by mangoyang*/
#include <bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int ch = 0, f = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define int ll
const int N = 10000005, mod = 1e9+7;
int js[N], inv[N], n, m, a, b, ans;
inline int Pow(int a, int b){
if(b == -1) b = mod - 2;
int ans = 1;
for(; b; b >>= 1, a = a * a % mod)
if(b & 1) ans = ans * a % mod;
return ans;
}
inline int C(int x, int y){
if(x < y) return 0;
return js[x] * inv[y] % mod * inv[x-y] % mod;
}
signed main(){
read(n), read(m), read(a), read(b);
js[0] = inv[0] = 1;
for(int i = 1; i <= max(n, m); i++)
js[i] = js[i-1] * i % mod, inv[i] = Pow(js[i], mod - 2);
for(int i = 0; i <= n - 2; i++)
(ans += C(m - 1, i) * C(n - 2, i) % mod * js[i] % mod * Pow(n, n - i - 3) % mod * (i + 2) % mod * Pow(m, n - i - 2) % mod) %= mod;
cout << ans << endl;
return 0;
}

Codeforces 1109D. 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 排列组合,Prufer编码

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

  3. CF1109D Sasha and Interesting Fact from Graph Theory

    CF1109D Sasha and Interesting Fact from Graph Theory 这个 \(D\) 题比赛切掉的人基本上是 \(C\) 题的 \(5,6\) 倍...果然数学计 ...

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

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

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

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

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

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

  7. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  8. Codeforces 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

  9. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

随机推荐

  1. text-align: justify;不能均匀分布问题

    本文地址:http://www.cnblogs.com/veinyin/p/7617610.html  对于text-align来说,一般我们都是使用居中这个属性值,突然今天需要达到这样一个效果: 对 ...

  2. 引用类型 ( 对象定义 )——Date 类型

    本文地址:http://www.cnblogs.com/veinyin/p/7607743.html  1 创建日期对象 var date = new Date(); 2 可以给日期对象传值 2.1 ...

  3. vue去除地址栏上的'#'号

    const router = new VueRouter({ routes:[], mode :"history"//除去#号 }

  4. 在字符串S1中删除字符串S2中所包含的字符【转】

    转自:http://www.cnblogs.com/tolimit/p/4202959.html /************************************************** ...

  5. c json实战引擎六 , 感觉还行

    前言 看到六, 自然有 一二三四五 ... 为什么还要写呢.  可能是它还需要活着 : ) 挣扎升级中 . c json 上面代码也存在于下面项目中(维护的最及时) structc json 这次版本 ...

  6. Python中如何Debug

    debug是编码是非常重要的调试技巧,通过在运行过程中设置断点,帮助开发人员更好的理解运行过程. Python中debug不像JAVA或者C++那样在IDE中设置断点那么直观. Python的debu ...

  7. 常用的Oracle的doc命令

    常用的Oracle的doc命令 1.连接数据库 普通用户连接数据库: conn scott/tiger --(默认的用户名/密码).conn 即"connection"连接数据库的 ...

  8. C#子线程中更新ui-----c# 多线程多文件批量下载

    c# 多线程多文件批量下载   废话少说,先演示一张效果图 简单说下过程喽 开发过程中其实总是会碰到项目想应用下载文件~ 看其他语言有很多封装好的类库可以使用~~ 作为小白的我并没有找到很多c#的案例 ...

  9. 在数据库中(Oracle),根据时间查询数据: to_date()和to_char()函数

    1. to_date() 函数 1.1 格式 to_date("要转换的字符串","转换的格式")   //两个参数的格式必须匹配,否则会报错.即按照第二个参数 ...

  10. AXFR和IXFR区域传输及原理

    由于区域在DNS中发挥着重要的作用,因此希望在网络上的多个DNS服务器中提供区域,以提供解析名称查询时的可用性和容错.否则,如果使用单个服务器而该服务器没有响应,则该区域中的名称查询会失败.对于主要区 ...