http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1770
This is question B of this BSG Bss Geek Challenge . Set p (i, J) to indicate the number of nodes in the number of I, the height of the AVL tree is J .
Well, for 1 <= k <= i-1
P[I][J] + = P[k][j-1]*p[i-1-k][j-1]%mod;
P[I][J] + = P[k][j-2]*p[i-1-k][j-1]%mod;
P[I][J] + = P[k][j-1]*p[i-1-k][j-2]%mod;
But this simulation isn^3of complexity. Obviously it's not going to work. ButJand thekthe scope is to beIconstrained. So I optimized theJthat layer, the local can run fast. Set up a fromand the torepresentsJThe range of this one-dimensional run, so every time this timeJThe minimum value is the next time from, this timeJThe maximum value is the next one. to. So you can.
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<vector>#include<string>#defineLL Long Long#defineMOD 1000000007using namespacestd;Const intMAXN =2002; LL P[MAXN][MAXN];voidinit () {int from, to, Tfrom, tto; Memset (P,0,sizeof(p)); p[0][0] =1; p[1][1] =1; from=0; to =1; for(inti =2; i < MAXN; ++i) {tfrom=to ; Tto= from; to++; for(intj = from; J <= to; ++j) { for(intK =0; K < I; ++k) {P[i][j]+ = p[k][j-1]*p[i-1-k][j-1]%MOD; if(J >1) {P[i][j]+ = p[k][j-2]*p[i-1-k][j-1]%MOD; P[I][J]+ = p[k][j-1]*p[i-1-k][j-2]%MOD; } P[i][j]%=MOD; if(P[i][j]) {Tfrom=min (Tfrom, j); Tto=Max (Tto, J); } } } from=Tfrom; to=Tto; } //cout << "OK" <<endl;}intMain () {//freopen ("test.in", "R", stdin);init (); intN; while(SCANF ("%d", &n)! =EOF) { intans; LL T=0; for(inti =1; I <= N; ++i) T = (T+p[n][i])%MOD; Ans=T; printf ("%d\n", ans); } return 0;}
View Code
ACM Learning process -51nod 1412 AVL tree Types (Recursive)