[bzoj4766]文艺计算姬——完全二分图生成树个数
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;
}