hdu6710(DP+BFS)

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=6710

题意

给定一个二分图,左右分别有 $n$ 个点和 $m$ 个点,则有 $nm$ 条边,每条边出现的概率为 $0.5$ ,问左右等概率随机选点 ,问这两个点的最短路的期望

题解

由对称性,这个可以转化为从 $1\sim n$ 的最短路的期望,这里用的是从 $1$ 到其他点的最短路的平均期望。。

从 $1$ 开始做 $BFS$ ,构造分层图,在其中求期望和

设 $d[i][j][k][v]$ 为第 $i$ 层有 $v$ 个点,到当前层黑点 $j$ 个,白点 $k$ 个的路径期望和

然后直接统计计数就可以了




代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
*         ┏┓    ┏┓
*         ┏┛┗━━━━━━━┛┗━━━┓
*         ┃       ┃  
*         ┃   ━    ┃
*         ┃ >   < ┃
*         ┃       ┃
*         ┃... ⌒ ...  ┃
*         ┃ ┃
*         ┗━┓ ┏━┛
*          ┃ ┃ Code is far away from bug with the animal protecting          
*          ┃ ┃ 神兽保佑,代码无bug
*          ┃ ┃           
*          ┃ ┃       
*          ┃ ┃
*          ┃ ┃           
*          ┃ ┗━━━┓
*          ┃ ┣┓
*          ┃ ┏┛
*          ┗┓┓┏━━━━━━━━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#include<cmath>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1ll<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 35
#define nm 50005
using namespace std;
const double pi=acos(-1);
const double eps=1e-8;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}





ll inf;
inline void reduce(ll&x){x+=x>>63&inf;}
inline ll qpow(ll x,ll t){
ll s=1;
for(;t;t>>=1,x=x*x%inf)if(t&1)s=s*x%inf;
return s;
}
int n,m,_t;
ll g[2][NM][NM][NM],d[2][NM][NM][NM],ans,comb[NM][NM];
ll p[NM],inv[NM],invp[NM],b[NM][NM],p2[NM*NM];
int cou;

int main(){
int _=read();while(_--){
mem(d);mem(g);
n=read();m=read();inf=read();
p[0]=p[1]=inv[1]=invp[0]=invp[1]=1;
p2[0]=1;inc(i,1,900)p2[i]=p2[i-1]*2%inf;
inc(i,2,30)p[i]=p[i-1]*i%inf,inv[i]=inv[inf%i]*(inf-inf/i)%inf,invp[i]=invp[i-1]*inv[i]%inf;
inc(i,1,30)inc(j,1,30)b[i][j]=qpow(qpow(2,i)-1,j);
inc(i,0,30)inc(j,0,i)comb[i][j]=p[i]*invp[j]%inf*invp[i-j]%inf;
g[0][1][0][1]=1;_t=0;ans=0;
//cou=p2[(n-1)*m];
inc(p,1,n+m-1){
_t^=1;mem(d[_t]);mem(g[_t]);
inc(i,1,n)inc(j,0,m)if(p&1){
inc(k,1,i)if(g[_t^1][i][j][k])
inc(v,1,m-j){
ll t=comb[m-j][v]*b[k][v]%inf;
reduce(g[_t][i][j+v][v]+=t*g[_t^1][i][j][k]%inf-inf);
reduce(d[_t][i][j+v][v]+=t*(p*v*g[_t^1][i][j][k]%inf+d[_t^1][i][j][k])%inf-inf);
}
}else{
inc(k,1,j)if(g[_t^1][i][j][k])
inc(v,1,n-i){
ll t=comb[n-i][v]*b[k][v]%inf;
reduce(g[_t][i+v][j][v]+=t*g[_t^1][i][j][k]%inf-inf);
reduce(d[_t][i+v][j][v]+=d[_t^1][i][j][k]*t%inf-inf);
}
}
if(p&1)
inc(i,1,n)inc(j,1,m)inc(k,1,m)reduce(ans+=d[_t][i][j][k]*p2[(n-i)*(m-j)]%inf-inf);
else
inc(i,1,n)inc(j,1,m)inc(k,1,n)reduce(ans+=d[_t][i][j][k]*p2[(n-i)*(m-j)]%inf-inf);
}
//printf("%d\n",cou);
//printf("%lld\n",ans);
printf("%lld\n",ans*inv[m]%inf*qpow(p2[n*m],inf-2)%inf);
}
return 0;
}