先处理出根的度为2,其余点度<=4的无标号有根树的方案数
环有旋转和翻转两种变换,由于m>=3,构成的置换群阶为2m,用burnside引理处理
旋转k(0<=k<m)步可以形成gcd(m,k)个等价类,每个等价类包含m/gcd(m,k)个位置
旋转+翻转需要分奇偶处理:
若m为奇数,则有m个这种置换,形成(m+1)/2个等价类,其中一个等价类包含1个位置,其余包含2个位置
若m为偶数
1、则有m/2个置换形成m/2个等价类,每个等价类包含2个位置
2、另有m/2个置换形成m/2+1个等价类,其中两个等价类包含1个位置,其余包含2个位置
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
int P;
template <typename T> void read(T &x)
{
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
template <typename T> void write(T x)
{
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T> void writeln(T x)
{
write(x);
puts("");
}
inline int Gcd(int a,int b) {for (int c;b;c = a,a = b,b = c % b); return a;}
inline int Phi(int n)
{
int x = n;
for (int i = 2;i * i <= n;i ++)
if (n % i == 0)
{
do n /= i;
while (n % i == 0);
x = x / i * (i - 1);
}
if (n > 1) x = x / n * (n - 1);
return x;
}
inline int Fix(int x) {return x + (x >> 31 & P);}
struct Num
{
int x;
Num(int a = 0) : x (a) {}
Num operator + (Num w) {return Fix(x + w.x - P);}
Num operator * (Num w) {return ll(x) * w.x % P;}
void operator += (Num w) {x = Fix(x + w.x - P);}
};
Num s[5][1007],Gs[11],Id[117],F[57][1007],G[57][1007],Ans;
void Calc(int m,int n)
{
int g = Gcd(n,m);
Num v = 0;
for (int d = 1;d <= g; d ++)
if (g % d == 0) v += F[m / d][n / d] * Phi(d);
v += G[m][n] * m;
Ans += v * Id[m * 2];
}
int main()
{
read(n) , read(m) , read(P);
if (m > n) m = n;
s[0][0] = Id[1] = 1;
for (int i = 2;i <= 115;i ++) Id[i] = Id[P % i] * (P - P / i);
for (int i = 1;i <= n;i ++)
{
F[1][i] = G[1][i] = s[0][i - 1] + s[1][i - 1] + s[2][i - 1];
Gs[1] = F[1][i] + s[3][i - 1];
for (int j = 2;j <= 3;j ++) Gs[j] = Gs[j - 1] * (Gs[1] + (j - 1)) *Id[j];
for (int j = 3;j;--j)
{
for (int k = n;k >= i;--k)
{
for (int t = 1;t <= j; t ++)
{
int w = k - t * i;
if (w >= 0) s[j][k] += Gs[t] * s[j - t][w];
}
}
}
}
for (int i = 2;i <= m;i ++)
{
for (int j = i;j <= n;j ++)
{
for (int k = 1;k < j;k ++) F[i][j] += F[i - 1][j - k] * F[1][k];
if (i & 1)
{
for (int k = 2;k < j;k += 2) G[i][j] += F[i >> 1][k >> 1] * F[1][j - k];
}else{
for (int k = 1;k < j;k ++) G[i][j] += G[i - 1][j - k] * F[1][k];
if (~j&1) G[i][j] += F[i >> 1][j >> 1];
G[i][j] = G[i][j] * Id[2];
}
}
}
//printf("%d\n",G[m][n]);
for (int i = 3;i <= m ;i ++) Calc(i,n);
writeln(Ans.x);
return 0;
}