题目链接

https://www.lydsy.com/JudgeOnline/problem.php?id=1430

题解

容易发现,设fif_iii个点无根树的个数,则答案为(n1)!fn(n-1)!f_n。由prufer序列的性质可知,fn=nn2f_n=n^{n-2},则答案为(n1)!×nn2(n-1)!\times n^{n-2}

代码

#include <cstdio>

int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

const int mod=9999991;

int quickpow(int a,int b,int m)
{
  int res=1;
  while(b)
    {
      if(b&1)
        {
          res=1ll*res*a%m;
        }
      a=1ll*a*a%m;
      b>>=1;
    }
  return res;
}

int n,fac;

int main()
{
  n=read();
  fac=1;
  for(int i=2; i<=n-1; ++i)
    {
      fac=1ll*fac*i%mod;
    }
  printf("%lld\n",1ll*fac*quickpow(n,n-2,mod)%mod);
  return 0;
}