Title Description
There are two points set $ S, T $, $ \ quad \ forall x \ in S, y \ in T $, $ x, there is $ \ frac {1} {2} $ probability that an edge exists between y $.
Now from any $ S, T $ each randomly pick a point, ask a desired distance between the two points.
If none, then the distance is $ 0 $
Output at a desired distance value $ P $ domain mold, to ensure that $ P $ is a prime number.
data range
For $ 100 \ $% of the data, to ensure that $ 1 \ le n, m \ le 100, 772001 \ le p \ le 1000000007 $, to ensure that p is a prime number.
answer
Autistic exam questions.
Violence may be seen as if the sum of the shortest went 1 $ $ $ T $ point number in / from program number 1 $ S $ $ $ point number, i.e. for each program to $ $ BFS again.
$ $ BFS process consideration, because it is a bipartite graph, so the first layer $ i $ only point and the point of $ i-1 $ a linked layer side.
Therefore, with the process of $ DP $ analog $ BFS $, the set $ f_ {i, j, k, 0/1} $ represents $ S $ selected a $ I $ points, $ T $ selected a $ J $ points, the last layer has $ k $ points, which points are $ k $ $ S / T $ program number, set $ G $ is the sum of the depth of the deepest of all programs.
When the transfer of the election does not determine what option $ 1 $ dot $ T $ in the way statistics can answer.
Efficiency: $ O (n ^ 4) $, note cards often.
Code
#include <bits/stdc++.h> using namespace std; const int N=105; int n,m,P,f[N][N][N][2],g[N][N][N][2],p[N][N],c[N][N],s,h[N*N],d[N][N][N][2]; inline int X(int x){return x>=P?x-P:x;} int K(int x,int y){ int z=1; for (;y;y>>=1,x=1ll*x*x%P) if (y&1) z=1ll*z*x%P; return z; } int main(){ scanf("%d%d%d",&n,&m,&P);if (n<m) swap(n,m); c[0][0]=f[1][0][1][0]=h[0]=1; for (int i=1;i<=n;i++){ c[i][0]=1; for (int j=1;j<=i;j++) c[i][j]=X(c[i-1][j-1]+c[i-1][j]); } for (int i=1;i<=n;i++){ p[i][1]=X(X(p[i-1][1]<<1)+1); for (int j=2;j<=n;j++) p[i][j]=1ll*p[i][j-1]*p[i][1]%P; } for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) for (int k=0;k<=n;k++) d[i][j][k][0]=1ll*c[i][k]*p[j][k]%P, d[i][j][k][1]=1ll*c[i][k]*p[j][k+1]%P; for (int i=1;i<=n*m;i++) h[i]=X(h[i-1]<<1);m--; for (int i=1;i<=n;i++) for (int j=0;j<=m;j++){ for (int w,k=1;k<=i;++k){ w=p[k][1]; for (int x=1;x+j<=m;++x) w=X(w+d[m-j][k][x][1]), f[i][j+x][x][1]=X(f[i][j+x][x][1]+1ll*d[m-j][k][x][0]*f[i][j][k][0]%P), g[i][j+x][x][1]=X(g[i][j+x][x][1]+1ll*d[m-j][k][x][0]*(g[i][j][k][0]+f[i][j][k][0])%P); s=X(s+1ll*w*h[(n-i)*(m-j+1)]%P*(g[i][j][k][0]+f[i][j][k][0])%P); } for (int k=1;k<=j;++k) for (int x=1;x+i<=n;++x) f[i+x][j][x][0]=X(f[i+x][j][x][0]+1ll*d[n-i][k][x][0]*f[i][j][k][1]%P), g[i+x][j][x][0]=X(g[i+x][j][x][0]+1ll*d[n-i][k][x][0]*(g[i][j][k][1]+f[i][j][k][1])%P); } printf("%lld\n",1ll*s*K(h[n*(m+1)],P-2)%P);return 0; }