思路看这篇博客就行了: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. 微软SaaS多租户解决方案

    微软SaaS多租户解决方案 docs.microsoft.com/en-us/azure/sql-database/saas-tenancy-app-design-patterns https://d ...

  2. windows服务是如何被调用的?

    1.服务介绍 操作系统在启动的时候,会启动一些不需要用户交互的进程.这些进程被称为服务.当操作系统启动后它就自动被运行. 2.组成 服务程序.服务控制程序(SCP,service control pr ...

  3. ubuntn 配置webpy nginx

    webpy环境搭建 在开始webpy搭建之前,有必要熟悉一下什么是fastcgi,因为搭建环境时都是使用这个模式去运行webpy程序的,具体的fastcgi描述可以参考各种百科:fastcgi协议官网 ...

  4. git自用笔记

    同步远程库:git clone xxx.git [filename] git ls-files: 查看已经添加进暂存区的文件. 在commit前修改一个文件后(假设名为:xxx.file),想撤销时, ...

  5. devops 几个方便的工具

    1. fake API      [canned](https://github.com/sideshowcoder/canned  )    fake API.      [wiremock](ht ...

  6. [Err] 1067 - Invalid default value for 'xxxTime'

    下面是导入sql脚本的的局部脚本 `xxxTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 发现是NO_ZERO_IN_DATE,NO_Z ...

  7. hdu 4609 3-idiots——FFT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 答案就是随便选三条边的方案 - 不合法的方案. 不合法的方案就是算出 x+y = k 的方案数 g[ ...

  8. bzoj 2763 [JLOI2011]飞行路线——分层图

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 分层图两种方法的练习. 1.把图分成k+1层,本层去上面一层的边免费.但空间时间都不算 ...

  9. (转)Android中的页面切换动画

    这段时间一直在忙Android的项目,总算抽出点时间休息一下,准备把一些项目用到的Android经验分享一下. 在Android开发过程中,经常会碰到Activity之间的切换效果的问题,下面介绍一下 ...

  10. vim中多行注释和多行删除命令

      1.多行注释:   1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;   2. 在行首使用上下键选择需要注释的多行;   3. 按下键盘(大写)“I”键,进入插入 ...