bzoj4766 文艺计算姬

Description

"奋战三星期,造台计算机"。小W响应号召,花了三星期造了台文艺计算姬。文艺计算姬比普通计算机有更多的艺术细胞。普通计算机能计算一个带标号完全图的生成树个数,而文艺计算姬能计算一个带标号完全二分图的生成树个数。更具体地,给定一个一边点数为n,另一边点数为m,共有n*m条边的带标号完全二分图K_{n,m},计算姬能快速算出其生成树个数。小W不知道计算姬算的对不对,你能帮助他吗?

Input

仅一行三个整数n,m,p,表示给出的完全二分图K_{n,m}
1 <= n,m,p <= 10^18

Output

仅一行一个整数,表示完全二分图K_{n,m}的生成树个数,答案需要模p。

Sample Input

2 3 7

Sample Output

5

 

正解:矩阵树定理+快速幂+黑科技乘法。
很水的一道题。。用矩阵树定理打表以后就发现$Ans=m^{n-1}*n^{m-1}$,然后可以先快速幂然后黑科技乘法解决。

 

 1 //It is made by wfj_2048~
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <complex>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <cmath>
10 #include <queue>
11 #include <stack>
12 #include <map>
13 #include <set>
14 #define inf (1<<30)
15 #define eps (1e-9)
16 #define il inline
17 #define RG register
18 #define ll long long
19 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
20 
21 using namespace std;
22 
23 ll n,m,p;
24 
25 il ll mul(RG ll a,RG ll b){
26     return (ll)(a*b-(ll)((long double)a/p*b+0.5)*p+p)%p;
27 }
28 
29 il ll qpow(RG ll a,RG ll b){
30     RG ll ans=1;
31     while (b){
32     if (b&1) ans=mul(ans,a);
33     a=mul(a,a),b>>=1;
34     }
35     return ans;
36 }
37 
38 int main(){
39     File("ji");
40     cin>>n>>m>>p;
41     RG ll a=qpow(n%p,m-1),b=qpow(m%p,n-1);
42     printf("%lld",mul(a,b));
43     return 0;
44 }

 

posted @ 2017-04-02 07:59  wfj_2048  阅读(278)  评论(0编辑  收藏  举报