Codeforces 1109D (树的计数问题)
思路看这篇博客就行了: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 (树的计数问题)的更多相关文章
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory
Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- 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 ...
- CodeForces - 343D 树链剖分
题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的 ...
- codeforces 341d (树状数组)
problem Iahub and Xors 题目大意 一个n*n的矩阵,要求支持两种操作. 操作1:将一个子矩阵的所有值异或某个数. 操作2:询问某个子矩阵的所以值的异或和. 解题分析 由于异或的特 ...
- CodeForces 396C 树状数组 + DFS
本主题开始看到以为段树或树状数组,但是,对于一个节点的有疑问的所有子节点的加权,这一条件被视为树的根,像 然后1号是肯定在第一层中,然后建立一个单向侧倒查,然后记录下来 其中每个节点 层,终于 两个节 ...
- codeforces 343D 树剖后odt维护
子树修改+路径修改+单点查询 树链剖分+区间维护即可 由于只有单点查询,我直接用了odt,复杂度还行 #include<bits/stdc++.h> #define endl '\n' # ...
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 排列组合,Prufer编码
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1109D.html 题意 所有边权都是 [1,m] 中的整数的所有 n 个点的树中,点 a 到点 b 的距离 ...
随机推荐
- 从无到有开发自己的Wordpress博客主题---代码环境准备
注意这里说的是代码环境准备哦,而不是L(M)AMP运行环境哦,运行环境会在后述文章中在写. 一.在本地初始化git环境并且链接上gitee 1.在gitee上创建一个项目托管你的代码 这个不在赘述,按 ...
- 牛客国庆集训派对Day1:J:Princess Principal(栈模拟求括号匹配)
题目描述 阿尔比恩王国(the Albion Kingdom)潜伏着一群代号“白鸽队(Team White Pigeon)”的间谍.在没有任务的时候,她们会进行各种各样的训练,比如快速判断一个文档有没 ...
- IntelliJ IDEA中使用Git
IntelliJ IDEA下使用Git IntelliJ IDEA下配置Git:git clone.git checkout.git diff.git log.git commit.git push. ...
- 用Toast来增加调试效率的小技巧
import android.content.Context; import android.widget.Toast; /** * Created by apple on 10/7/15. */ p ...
- 完整的qt安装教程
大家可能认为qt收费了 其实不是 大家直接点击 这个 Community 这里的下载 Download 然后呢 就跳转到这个界面 点击 Qt Offline Installer 就会跳到这个地方 滑下 ...
- Memcached的限制和使用建议
1. 在Memcached中可以保存item数据量没有限制的,只要内存足够 2. Memcached单进程最大使用内存2G,要使用更多内存,可以分多个端口开启多个Memcached进程 3. Memc ...
- Tomcat下WebSocket最大连接数测试
WebSocket现在很常用,想要测试tomcat的最大连接数,今天试了一个可行的办法和配置(之前是用全公司的设备一起来测试的,真机环境的测试收到网络的影响很大,其实真实环境应用中,网络才是webso ...
- TCP之一:传输控制协议(Transmission Control Protocol, TCP)
TCP协议主为了在主机间实现高可靠性的包交换传输协议.本文将描述协议标准和实现的一些方法.因为计算机网络在现代社会中已经是不可缺少的了,TCP协议主要在网络不可靠的时候完成通信,对军方可能特别有用,但 ...
- 转载:Oracle RAC日常基本维护命令
本文转载自: https://blog.csdn.net/tianlesoftware/article/details/5358573 Oracle RAC日常基本维护命令 好文转载, Oracle ...
- MyEclipse10.6/Eclipse4.2 集成 flash builder 4.6
MyEclipse10.6/Eclipse4.2 集成 flash builder 4.61. 安装 Myeclipse10.6 或 Eclipse 4.2 以及 Flash builder 4.6 ...