zoukankan      html  css  js  c++  java
  • HDU 4359 Easy Tree DP?

    Easy Tree DP?

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1487    Accepted Submission(s): 567


    Problem Description
    A Bear tree is a binary tree with such properties : each node has a value of 20,21…2(N-1)(each number used only once),and for each node ,its left subtree’s elements’ sum<its right subtree’s elements’ sum(if the node hasn’t left/right subtree ,this limitation is invalid).
    You need to calculate how many Bear trees with N nodes and exactly D deeps.
     
    Input
    First a integer T(T<=5000),then T lines follow ,every line has two positive integer N,D.(1<=D<=N<=360).
     
    Output
    For each test case, print "Case #t:" first, in which t is the number of the test case starting from 1 and the number of Bear tree.(mod 109+7)
     
    Sample Input
    2
    2 2
    4 3
     
    Sample Output
    Case #1: 4
    Case #2: 72
     
    Author
    smxrwzdx@UESTC_Brightroar
     
    Source

    解题:哎。。连续训练了两个月,身累心更累。。不想说什么了,看这位大神的解说吧

    他的钻头是可以突破天际的钻头

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 365;
     4 typedef long long LL;
     5 const LL mod = 1e9 + 7;
     6 LL dp[maxn][maxn],c[maxn][maxn];
     7 void init(){
     8     memset(dp,-1,sizeof dp);
     9     for(int i = 0; i < maxn; ++i){
    10         c[i][0] = c[i][i] = 1;
    11         for(int j = 1; j < i; ++j)
    12             c[i][j] = (c[i-1][j-1] + c[i-1][j])%mod;
    13     }
    14 }
    15 LL dfs(int n,int d){
    16     if(n == 1 && d >= 1) return 1;
    17     if(n == 1 || d == 0) return 0;
    18     if(dp[n][d] != -1) return dp[n][d];
    19     LL &ret = dp[n][d];
    20     ret = (dfs(n-1,d-1)*c[n][1]*2)%mod;
    21     for(int k = 1; k <= n-2; ++k)
    22         ret = (ret + (dfs(n-k-1,d-1)*dfs(k,d-1)%mod*c[n-2][k]%mod*c[n][1])%mod)%mod;
    23     return ret;
    24 }
    25 int main(){
    26     int kase,cs = 1,n,d;
    27     init();
    28     scanf("%d",&kase);
    29     while(kase--){
    30         scanf("%d%d",&n,&d);
    31         printf("Case #%d: %I64d
    ",cs++,(dfs(n,d) - dfs(n,d-1) + mod)%mod);
    32     }
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    Hexo yilia 主题启用及相关设置
    Hexo Next 主题启用及相关设置
    值得一用的 Windows 软件清单
    ES6新特性2
    ES6新特性1
    手写promise第三天
    手写promise第一天
    Generator异步函数
    函子Functor
    柯里化函数组合的学习
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4782466.html
Copyright © 2011-2022 走看看