Bzoj 5305: [Haoi2018] apple tree

Source: Internet
Author: User

Description

Solution

\ (n\) points of the two-fork-tree scheme number is \ (n!\)
The proof is very obvious: the new added points take up \ (1\) position, the new add \ (2\) position, then more out of a position, so the first \ (i\) points have \ (i\) kind of method
Consider the number of times each edge is passed, set the subtree size to \ (size\), or \ (size* (n-size) \)
To consider the number of times the parent edge of each point was passed, enumerate the subtree size
Then the contribution is the number of schemes of the inner shape of the subtree multiplied by the external pattern.
The interior is obviously \ (size!\) , but the number is not sure, so it is \ (size!*c_{n-i}^{size-1}\)
Outside we first determine a size of \ (i\) of the tree, and then the more out of the \ (n-size-i+1\) to spell up, the scheme number is \ (\frac{(n-j-1)!} {(i-2)!} \)

#include <bits/stdc++.h>using namespaceStdConst intn= .;intN,mod,c[n][n],fac[n],w[n][n];inline intFintXintY) {if(y<=0)return 1;returnW[x][y];}intMain () {Freopen ("Pp.in","R", stdin); Freopen ("Pp.out","W", stdout); cin>>n>>mod; for(intI=0; i<=n;i++) {c[i][0]=1; for(intj=1; j<=i;j++) c[i][j]= (c[i-1][j-1]+c[i-1][J])%mod; } fac[0]=1; for(intI=1; i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod; for(intI=1; i<=n;i++) {w[i][0]=1; for(intj=1; j<=n;j++) w[i][j]=1ll*w[i][j-1]* (i+j-2)%mod; }intans=0; for(intI=2; i<=n;i++) for(intJ=n-i+1; j>=1; j--) ans= (ans+1ll*fac[j]*c[n-i][j-1]%mod*fac[i]%mod*f (i,n-j-i+1)%mod* (n-j)%mod*j)%mod; cout<<ans<<endl;return 0;}

Bzoj 5305: [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.