[HAOI2018] Apple tree

Topic description

Little C planted an apple tree in his garden, and each node on the tree has exactly two branches. After careful observation, Little C found that the tree grows a new node every day.

On the first day, the fruit tree will grow a root node. Every day after that, the fruit tree will randomly select a branch that has not grown a node in the current tree, and then grow a new node on this branch. Connect the previous edge between the point and the node to which the branch belongs.

Small C defines the inconvenience of a fruit tree as the sum of the distances between two nodes on the tree, and the distance between two nodes is defined as the number of edges passed by the path from one point to another.

Now he is very curious, if little G comes to his house to pick apples after NN N days, what is the expected EEE for this inconvenience . But little C hates scores, so he just wants to know E×N!E \times N ! E × N ! The result of modulo PP P, it can be shown that this is an integer.

Input and output format

Input format:

Read data from standard input. Two integers NN N (N<=2000), PP P on a line.

Output format:

Output to standard output. Output an integer representing the answer.

Input and output example

Input Example #1: Copy
3 610745795
Output Sample #1: Copy
24
Input Example #2: Copy
305 1000000007
Output Sample #2: Copy
865018107

illustrate

Explanation

The above are all possible apple tree shapes when N=3N = 3 N = 3, where the number indicates the number of days this node grew, and obviously the distance between the two nodes in each case is 44 4 .

portal

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 int fac[2001],C[2001][2001],F[2001][2001],n,Mod,ans;
 8 int main()
 9 {int i,j;
10   cin>>n>>Mod;
11   fac[0]=1;
12   for (i=1;i<=n;i++)
13     fac[i]=1ll*fac[i-1]*i%Mod;
14   for (i=0;i<=n;i++)
15     {
16       C[i][0]=1;
17       for (j=1;j<=i;j++)
18     C[i][j]=(C[i-1][j-1]+C[i-1][j])%Mod;
19     }
20   for (i=1;i<=n;i++)
21     {
22       F[i][0 ] = 1 ;
23        for (j = 1 ; j <= n; j ++ )
 24      F [i] [j] = 1ll * F [i] [j- 1 ] * (i + j- 2 )% Mod;
25      }
 26    for (i = 2 ; i <= n; i ++ )
 27      {
 28        for (j = n-i + 1 ; j> = 1 ; j-- )
 29      {
 30        ans = (ans + (1ll * j * ( nj)% Mod * C [ni] [j- 1 ]% Mod * fac [j]% Mod * F [i] [nj-i + 1 ]% Mod * fac [i]% Mod))% Mod;
31      }
 32     }
 33    cost<< years;
34 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325095527&siteId=291194637