[HAOI2018] apple tree problem solution

Topic Link

Effect: do not explain

Ideas:

First, there are several programs the n- ! Species, the first 1 point only 1 choices, the 2 point 2 choices, to generate 2 substituents selected while consuming one of the first 3 points there are 3 choices, and so there are n! Kinds of programs, because the final answer * the n- ! , It is actually the sum of the output of each.

Because enumeration scheme is not feasible, consider enumeration side, calculate the contribution of each point connected to his father's side, easy to know contribute to the SIZ * (the n--SIZ) , SIZ sub-tree size. So enumeration point siz can be. Consider the subtree composition form, with the outer shape of the subtree. To enumerate the current set i dot subtree size siz , the sub-tree number has not considered the siz! Kinds of patterns , considering there are number C (ni, siz-1) kinds of combinations of numbers , the total of the sub-tree siz ! * C (ni, siz- 1) kinds of programs; outer subtree considered: As already i points, which i points can i ! Kinds of programs , from the i + siz-1 since the start point i subtree rooted siz been determined, so that this point can not be inserted to i within the subtree rooted, only i-1 choices , this part of the contribution is (i-1) * (i ) * (i + 1) *...... * (n---SIZ. 1) , the sum of the previous simplification to give I * (. 1-I) * (n---SIZ. 1)! . The current enumeration point i and siz contribution to the answer to siz * (n-siz) * i * (i-1) * (n-siz-1)! * C (ni, siz-1) * siz! . And a combined pretreatment factorial enumerated points i and siz statistics can. Time complexity of O (n ^ 2);

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <memory.h>
 4 #define r(x) x=read()
 5 #define MAXX 2005
 6 #define MAX(a,b) (a>b?a:b)
 7 #define MIN(a,b) (a<b?a:b)
 8 using namespace std;
 9 typedef long long ll;
10 ll read()
11 {
12   char ch=0;ll w=0,ff=1;
13   while(ch<'0'||ch>'9'){if(ch=='-')ff=-1;ch=getchar();}
14   while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
15   return ff*w;
16 }
17 ll P,n,fac[MAXX][MAXX],jie[MAXX],ans;
18 int main()
19 {
20   jie[0]=1ll;
21   r(n),r(P);
22   for(int i=0;i<=n;++i)
23     for(int j=0;j<=i;++j)
24       fac[i][j]=(j==0?1:(fac[i-1][j-1]+fac[i-1][j])%P);
25   for(int i=1;i<=2000;++i) jie[i]=jie[i-1]*i*1ll%P;
26   for(int i=2;i<=n;++i)
 27      for ( int s = 1 , s <= n-i + 1 ; ++ s)
 28        ans = (ans + (W * 1ll * (n-s) * 1ll% P * jie [s]% P * jie [sz- n- 1 ] and% * * P (i- 1 )% P * fac [ni] [sz- 1 ])% P)% P;
29    printf ( " % lld " , ans% P);
30    return  0 ;
31 }

 

Guess you like

Origin www.cnblogs.com/dah1314/p/11708729.html