Brief Description

求\(K_{n,m}\)

Algorithm Design

首先我们有(Matrix Tree)定理,可以暴力生成几组答案,发现一些规律:

\[K_{n,m} = n^{m-1} * m^{n-1}
\]

然而直接乘法会爆longlong,所以使用快速乘

Code

#include <cstdio>
#define ll long long
ll n, m, p;
inline ll mul(ll a, ll b) {
ll x = 0;
while (b) {
if (b & 1)
x = (x + a) % p;
a = (a << 1) % p;
b >>= 1;
}
return x;
}
inline ll pow(ll a, ll b, ll p) {
ll x = 1;
while (b) {
if (b & 1)
x = mul(x, a);
a = mul(a, a);
b >>= 1;
}
return x;
}
int main() {
scanf("%lld %lld %lld", &n, &m, &p);
if (n == 0 || m == 0) {
printf("0\n");
return 0;
}
printf("%lld\n", mul(pow(n, m - 1, p), pow(m, n - 1, p)));
return 0;
}

[bzoj4766]文艺计算姬——完全二分图生成树个数的更多相关文章

  1. bzoj4766 文艺计算姬

    Description "奋战三星期,造台计算机".小W响应号召,花了三星期造了台文艺计算姬.文艺计算姬比普通计算机有更多的艺术细胞.普通计算机能计算一个带标号完全图的生成树个数, ...

  2. BZOJ4766:文艺计算姬(矩阵树定理)

    Description "奋战三星期,造台计算机".小W响应号召,花了三星期造了台文艺计算姬.文艺计算姬比普通计算机有更多的艺术细胞. 普通计算机能计算一个带标号完全图的生成树个数 ...

  3. [bzoj4766] 文艺计算姬 (矩阵树定理+二分图)

    传送门 Description "奋战三星期,造台计算机".小W响应号召,花了三星期造了台文艺计算姬.文艺计算姬比普通计算机有更多的艺 术细胞.普通计算机能计算一个带标号完全图的生 ...

  4. BZOJ4766: 文艺计算姬(Prufer序列)

    题面 传送门 题解 结,结论题? 答案就是\(n^{m-1}m^{n-1}\) 我们考虑它的\(Prufer\)序列,最后剩下的两个点肯定是一个在左边一个在右边,设左边\(n\)个点,右边\(m\)个 ...

  5. Bzoj4766: 文艺计算姬(Matrix-tree/prufer)

    BZOJ 答案就是 \(n^{m-1}m^{n-1}\) \(prufer\) 证明: \(n\) 中的数字出现 \(m-1\) 次,\(m\) 中出现 \(n-1\) 次,根据 \(prufer\) ...

  6. 【BZOJ】4766: 文艺计算姬

    [题目]给定两边节点数为n和m的完全二分图,求生成树数取模给定的p.n,m,p<=10^18. [算法]生成树计数(矩阵树定理) [题解]参考自 [bzoj4766]文艺计算姬 by WerKe ...

  7. 【BZOJ4766】文艺计算姬 [暴力]

    文艺计算姬 Time Limit: 1 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description "奋战三星期,造台计算机 ...

  8. bzoj 4766: 文艺计算姬 -- 快速乘

    4766: 文艺计算姬 Time Limit: 1 Sec  Memory Limit: 128 MB Description "奋战三星期,造台计算机".小W响应号召,花了三星期 ...

  9. BZOJ 4766: 文艺计算姬

    4766: 文艺计算姬 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 456  Solved: 239[Submit][Status][Discuss] ...

随机推荐

  1. Android Spiner实现Key-Value

    原网址:http://www.eoeandroid.com/thread-29687-1-1.html?_dsign=02d5cd6a 学习到的方法,直接上代码了: 1.定义一个class publi ...

  2. ardupilot_gazebo仿真(三)

    ardupilot_gazebo仿真(三) 标签(空格分隔): 未分类 创建ROS node 实现对无人机的控制(软件在环) MAVROS MAVROS是ROS中的一个能够连接支持MAVLink地面站 ...

  3. Faster RCNN代码解析

    1.faster_rcnn_end2end训练 1.1训练入口及配置 def train(): cfg.GPU_ID = 0 cfg_file = "../experiments/cfgs/ ...

  4. ByteArrayInputStream/ByteArrayOutputStream 学习

    ByteArrayInputStream: byte[] buff = new byte[1024]; ByteArrayInputStream bAIM = new ByteArrayInputSt ...

  5. MongoDB Linux下的安装和启动

    1. 下载MongoDB,此处下载的版本是:mongodb-linux-i686-1.8.1.tgz.tar. http://fastdl.mongodb.org/linux/mongodb-linu ...

  6. 甲级1002 A+B for Polynomials (25)

    题目描述: This time, you are supposed to find A+B where A and B are two polynomials. Input Each input fi ...

  7. lintcode-103-带环链表 II

    带环链表 II 给定一个链表,如果链表中存在环,则返回到链表中环的起始节点的值,如果没有环,返回null. 样例 给出 -21->10->4->5, tail connects to ...

  8. lintcode-64-合并排序数组 II

    64-合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 注意事项 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 样例 给出 A = [1, 2, 3, ...

  9. 【python】 json.dumps() json.dump()的区别

    以前写的很简单,只有几句话,最近发现本文是本博客阅读量最大的一篇文章,觉得这样有种把人骗进来的感觉,于是又细化了一些.如果还有不好的地方,欢迎指出. 首先说明基本功能: dumps是将dict转化成s ...

  10. ELK + Kafka + Filebeat

    ELK + Kafka + Filebeat学习 https://blog.csdn.net/qq_21383435/article/details/79463832 https://blog.csd ...