ACM Learning process -51nod 1412 AVL tree Types (Recursive)

Source: Internet
Author: User

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)

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.