思路看这篇博客就行了:https://www.cnblogs.com/zhouzhendong/p/CF1109D.html, 讲的很好

今天学到了prufer编码,这是解决树上计数问题的一大利器,博客:https://www.cnblogs.com/jianglangcaijin/p/5989930.html

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int mod = 1000000007;
const int maxn = 1000010;
LL v[maxn], inv[maxn];
int n, m;
LL qpow(LL a, LL b) {
if(b < 0) return qpow(a, b + mod - 1);
LL ans = 1;
for (; b; b >>= 1) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
void init(int n) {
v[0] = 1;
for (int i = 1; i <= n; i++)
v[i] = (v[i - 1] * i) % mod;
inv[n] = qpow(v[n], mod - 2);
for (int i = n - 1; i >= 0; i--)
inv[i] = (inv[i + 1] * (i + 1)) % mod;
}
LL C(LL a, LL b) {
return ((v[a] * inv[b]) % mod * inv[a - b]) % mod;
}
LL solve(int i) {
LL res = 0;
res = ((C(m - 1, i) * C(n - 2, i)) % mod * qpow(m, n - 2 - i)) % mod;
res = ((res * qpow(n, n - 3 - i)) % mod * (i + 2) ) % mod;
res = (res * v[i]) % mod;
return res; }
int main() {
int a, b;
scanf("%d%d%d%d", &n, &m, &a, &b);
init(max(n, m) + 1);
LL ans = 0;
for (int i = 0; i < m && i <= n - 2; i++) {
ans = (ans + solve(i)) % mod;
}
cout << ans <<endl;
}

  

Codeforces 1109D (树的计数问题)的更多相关文章

  1. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

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

  2. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  3. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  4. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  5. CodeForces - 343D 树链剖分

    题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的 ...

  6. codeforces 341d (树状数组)

    problem Iahub and Xors 题目大意 一个n*n的矩阵,要求支持两种操作. 操作1:将一个子矩阵的所有值异或某个数. 操作2:询问某个子矩阵的所以值的异或和. 解题分析 由于异或的特 ...

  7. CodeForces 396C 树状数组 + DFS

    本主题开始看到以为段树或树状数组,但是,对于一个节点的有疑问的所有子节点的加权,这一条件被视为树的根,像 然后1号是肯定在第一层中,然后建立一个单向侧倒查,然后记录下来 其中每个节点 层,终于 两个节 ...

  8. codeforces 343D 树剖后odt维护

    子树修改+路径修改+单点查询 树链剖分+区间维护即可 由于只有单点查询,我直接用了odt,复杂度还行 #include<bits/stdc++.h> #define endl '\n' # ...

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

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

随机推荐

  1. maxsigma

    10^8 768 10^7 448 10^6 240 10^5 128

  2. 【ftp】服务器的链接命令

    1. 连接ftp服务器 格式:ftp [hostname| ip-address] a)在Linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密 ...

  3. 在Spring中通过构造自动装配--constructor

    在Spring中,可以使用“通过构造自动装配”,实际上是按构造函数的参数类型自动装配. 这意味着,如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么将自动装配. packag ...

  4. XML的应用 ---- 从一个范例看xml数据、xsd验证、xslt样式

    从一个范例看XML的应用 引言 如果你已经看了Asp.Net Ajax的两种基本开发模式 这篇文章,你可能很快会发现这样一个问题:在那篇文章的方式2中,客户端仅仅是发送了页面上一个文本框的内容到服务端 ...

  5. Linux环境下安装jenkins

    废话不多说,直接开始 1.从官网下载Jenkins的war包 2.下载好的War放到Tomcat的网站根目录webapps下,然后启动Tomcat. 3.打开浏览器,输入http://IP:8080/ ...

  6. mybatis-generator的坑

    有天发现mybatis-generator不能用,要加什么根项目前缀, 搞到我重新从github下载,然后发现仓库用不了,原来新项目会改我的maven配置(新坑 后来终于明白要在子项目栏使用手脚架,就 ...

  7. <trim>: prefix+prefixOverrides+suffix+suffixOverrides

    <trim prefix="where" prefixOverrides="where" suffixOverrides="and"& ...

  8. Linux IO 监控与深入分析

    https://jaminzhang.github.io/os/Linux-IO-Monitoring-and-Deep-Analysis/ Linux IO 监控与深入分析 引言 接昨天电话面试,面 ...

  9. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  10. Linux常用命令(个人使用频率较高)

    1,日志查看 tail(cat) -f|grep ERROR(任意字符) filepath (任意行数) -f|grep ERROR(任意字符) filepath 2,查看目录&授权 file ...