• [动态规划]Booming Business


    题目描述

    You are an expert in bonsai, the Japanese art of cultivating small trees in small containers.Every year, you win the Bonsai Association’s Pruning Competition (BAPC). With all this talent, it would be a shame not to turn your hobby into your job. Recently, you have rented a small store where you will sell your creations. Now you need to make a window display to draw in customers. Of course, you would like to grow the most impressive tree that will fit the window, but the window is only so tall, and the floor of the display can only bear so much weight. Therefore, you want a tree that is exactly so tall and so heavy that it can fit in your window.
    Being an expert, you know that by definition a bonsai tree consists of a single branch, with 0 or more smaller bonsai trees branching off from that branch.
    figure 1: Four distinct examples of bonsai trees.
    The height and weight of a bonsai tree can be carefully determined. A tree’s weight is equal to the number of branches that appear in it. The weights of the trees in figure 1 are 1, 4,6 and 6, respectively. A tree’s height is equal to the length of the longest chain of branches from the root to the top of the tree. The heights of the trees in figure 1 are 1, 2, 3 and 3,respectively.
    To make the most use of your window, you want to produce a bonsai tree of the precise height and weight that it can support. To get an idea of the number of options available to you, you would like to know how many different trees you could possibly grow for your store. Given a height and a weight, can you determine the number of trees with exactly that height and weight? Because the number may be very large, you may give your answer modulo 1,000,000,007.

    输入

    A single line containing two integers, h and w, with 1 ≤ h, w ≤ 300.

    输出

    Output a single line containing a single integer, the number of bonsai trees of height h and weight w, modulo 109+7.

    样例输入

    2 4
    

    样例输出

    1
    思路:状态dp[h][w]表示高度限制为h的情况重量等于w的树的种数(也就是高度不超过h的重量为w的种数),则状态转移方程为dp[h][w]=sum{i=1~w-1 dp[h-1][i]*dp[h][w-i]};
    最终答案是dp[h][w]-dp[h-1][w];
    (为什么是这样的呢?↓↓

    )
    AC代码:

    #include <iostream>
    #include<cstdio>
    #define mod 1000000007
    typedef long long ll;
    using namespace std;
    
    ll dp[310][310];
    
    int main()
    {
        int h,w;scanf("%d%d",&h,&w);
        for(int i=1;i<=h;i++) dp[i][1]=1;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                for(int k=1;k<=j-1;k++){
                    dp[i][j]=(dp[i][j]+dp[i-1][k]*dp[i][j-k]%mod)%mod;
                }
            }
        }
        ll ans=((dp[h][w]-dp[h-1][w])%mod+mod)%mod;
        printf("%lld
    ",ans);
        return 0;
    }

    太巧妙了tql

    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    开发了那么多项目,你能自己手写个健壮的链表出来吗?【华为云技术分享】
    高性能Web动画和渲染原理系列(3)——transform和opacity为什么高性能【华为云技术分享】
    pringBoot-MongoDB 索引冲突分析及解决【华为云技术分享】
    成为高手前必懂的TCP干货【华为云技术分享】
    Python爬虫从入门到精通——基本库re的使用:正则表达式【华为云技术分享】
    【我的物联网成长记2】设备如何进行选型?【华为云技术分享】
    多云架构落地设计和实施方案【华为云技术分享】
    dom的节点操作
    节点访问关系
    封装class类--分割类名后
  • 原文地址:https://www.cnblogs.com/lllxq/p/9531825.html
Copyright © 2020-2023  润新知