题目链接
题解
妙啊
要求的是所有可能的树形的所有点对距离和
直接考虑点的贡献肯定想不出,这样的所有点对距离问题通常转化为边的贡献
考虑一条边会产生多少贡献
我们枚举\(i\)节点的父亲边
首先我们认识到一点,按照题中所给的生成树的方式,\(n\)个节点的树有\(n!\)种形态
我们枚举了边,贡献为边两侧点数之积,所以再枚举一下\(i\)子树大小\(siz\)
那么贡献为
\[siz(n - siz)
\]
\]
\(i\)子树的方案数为
\[{n - i \choose siz - 1}siz!
\]
\]
\(i\)之外的树的方案数为
\[i!(i - 1)^{\overline{n - i - siz + 1}} = i(i - 1)(n - siz - 1)!
\]
\]
所以最后的答案为
\[\sum\limits_{i = 2}^{n}\sum\limits_{siz = 1}^{n - i + 1}{n - i \choose siz - 1}siz!siz(n - siz)i(i - 1)(n - siz - 1)!
\]
\]
复杂度\(O(n^2)\)
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 2005,maxm = 100005,INF = 0x3f3f3f3f;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
return flag ? out : -out;
}
int n,P,C[maxn][maxn],fac[maxn],ans;
int main(){
n = read(); P = read();
for (int i = 0; i <= n; i++){
C[i][0] = C[i][i] = 1;
for (int j = 1; j <= (i >> 1); j++)
C[i][j] = C[i][i - j] = (C[i - 1][j] + C[i - 1][j - 1]) % P;
}
fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % P;
for (int i = 2; i <= n; i++)
for (int j = 1; j <= n - i + 1; j++)
ans = (ans + 1ll * C[n - i][j - 1] * fac[j] % P * (n - j) % P * j % P * i % P * (i - 1) % P * fac[n - j - 1] % P) % P;
printf("%d\n",ans);
return 0;
}