[HAOI2018] apple tree

Source: Internet
Author: User

Title Description

Little C planted an apple tree in his garden, and each node in the tree had exactly two branches. After careful observation, small C found that every day the tree will grow a new node.

On the first day, fruit trees grow a root knot, and each day, the fruit tree randomly chooses a branch that does not have a node in the current tree, and then grows a new node on the branch, connecting the new node to the node that the branch belongs to.

Small C defines the handicap of a fruit tree as the sum of the distances between the 22 nodes of the trees, and the distance between the two nodes is defined as the number of edges passing through the path from one point to another.

Now he was very curious, ifNnN days later small G came to his home to pick apples, this inconvenience of expectationEeE is how much. But little C hates fractions, so he just wants to know exn!. E \times N! ExN! The result of themodulus of PP P can be shown to be an integer.

Input/output format

Input format:

Reads data from the standard input. One line of two integers NNN (n<=2000), PPP.

Output format:

Output to standard output. Outputs an integer that represents the answer.

Input/Output sampleInput Sample # #:Copy
3 610745795
Sample # # of output:Copy
24
Input Sample #:Copy
305 1000000007
Output Example #:Copy
865018107
Description

These are all n=3n = 3N=3 o'clock possible apple tree patterns, where the number indicates that the node was grown in the first few days, and obviously the distance of 22 nodes per case is 4.

Transmission Door

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 intfac[2001],c[2001][2001],f[2001][2001],n,mod,ans;8 intMain ()9{inti,j;TenCin>>n>>Mod; Onefac[0]=1; A    for(i=1; i<=n;i++) -fac[i]=1ll*fac[i-1]*i%Mod; -    for(i=0; i<=n;i++) the     { -c[i][0]=1; -        for(j=1; j<=i;j++) -C[i][j]= (c[i-1][j-1]+c[i-1][J])%Mod; +     } -    for(i=1; i<=n;i++) +     { Af[i][0]=1; at        for(j=1; j<=n;j++) -f[i][j]=1ll*f[i][j-1]* (i+j-2)%Mod; -     } -    for(i=2; i<=n;i++) -     { -        for(j=n-i+1; j>=1; j--) in     { -Ans= (ans+ (1ll*j* (n-j)%mod*c[n-i][j-1]%mod*fac[j]%mod*f[i][n-j-i+1]%MOD*FAC[I]%MOD))%Mod; to     } +     } -cout<<ans; the}

[HAOI2018] apple tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.