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

  1. /*program by mangoyang*/
  2. #include <bits/stdc++.h>
  3. #define inf (0x7f7f7f7f)
  4. #define Max(a, b) ((a) > (b) ? (a) : (b))
  5. #define Min(a, b) ((a) < (b) ? (a) : (b))
  6. typedef long long ll;
  7. using namespace std;
  8. template <class T>
  9. inline void read(T &x){
  10. int ch = 0, f = 0; x = 0;
  11. for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
  12. for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
  13. if(f) x = -x;
  14. }
  15. #define int ll
  16. const int N = 10000005, mod = 1e9+7;
  17. int js[N], inv[N], n, m, a, b, ans;
  18. inline int Pow(int a, int b){
  19. if(b == -1) b = mod - 2;
  20. int ans = 1;
  21. for(; b; b >>= 1, a = a * a % mod)
  22. if(b & 1) ans = ans * a % mod;
  23. return ans;
  24. }
  25. inline int C(int x, int y){
  26. if(x < y) return 0;
  27. return js[x] * inv[y] % mod * inv[x-y] % mod;
  28. }
  29. signed main(){
  30. read(n), read(m), read(a), read(b);
  31. js[0] = inv[0] = 1;
  32. for(int i = 1; i <= max(n, m); i++)
  33. js[i] = js[i-1] * i % mod, inv[i] = Pow(js[i], mod - 2);
  34. for(int i = 0; i <= n - 2; i++)
  35. (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;
  36. cout << ans << endl;
  37. return 0;
  38. }

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. c++ poco 使用mysql中文乱码问题

    poco 是c++ 一个比较好的库,现在正在学习使用它,碰到一些问题记录在此. poco版本:poco-1.46-all ,带有数据库的支持模块 操作系统:ubuntu 1.使用poco的MySQL模 ...

  2. 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)

    [题意]给定n个数字ai,每次询问一个区间中随机抽选两个数字,数字相同的概率,以分数最简形式输出.n,ai<=50000. [算法]莫队算法 [题解]参考:莫队……讲稿? by Foreseea ...

  3. 引用类型 ( 对象定义 )——Array 类型

    本文地址:http://www.cnblogs.com/veinyin/p/7607293.html  一个数组中可以存储不同类型的值,可以混合存储数字.字符串.对象等 1 创建数组 1.1 构造函数 ...

  4. POJ 3734 Blocks (矩阵快速幂)

    题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...

  5. 【译】第二篇 SQL Server代理作业步骤和子系统

    本篇文章是SQL Server代理系列的第二篇,详细内容请参考原文. SQL Server代理作业由一系列的一个或多个作业步骤组成.一个作业步骤分配给一个特定的作业子系统(确定作业步骤去完成的工作). ...

  6. textarea输入框随内容撑开高度

    原文链接 方法一(jquery): $('textarea').each(function () {  this.setAttribute('style', 'height:' + (this.scr ...

  7. python作业员工信息表程序(第四周)

    作业需求: 1. 员工信息表程序,实现增删改查操作: 2. 可进行模糊查询,语法至少支持下面3种: select name,age from staff_table where age > 22 ...

  8. 33、求按从小到大的顺序的第N个丑数

    一.题目 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 二.解法 ...

  9. (转)USB协议简介

    USB协议简介     USB是一种协议总线,即主机与设备之间的通信需要遵循一系列约定.协议内容较多,这里仅作一些简单介绍,深入学习,可参看USB规范(WWW.usb.org).     为了理解协议 ...

  10. 64_p4

    perl-Test-Compile-1.3.0-4.fc26.noarch.rpm 12-Feb-2017 05:09 26486 perl-Test-ConsistentVersion-0.3.0- ...