zoukankan      html  css  js  c++  java
  • 一道DP

    也是校赛学长出的一道题~想穿了很简单。。但我还是听了学长讲才明白。

    观察力有待提高。

    Problem D: YaoBIG’s extra homework
    Time Limit
    Memory Limit
    1 s
    128 MB
    Description
    One day, a teacher assigned this extra homework to students of UCAS:
    "What’s the number of the solution that an n  n ladder is covered by exactly n rectangles? Note
    that every two rectangles cannot overlap and the rectangles cannot reach out of the ladder."(The "Note"
    section gives you an example of the "ladder".)
    While YaoBIG was awkward, he still wanted the extra points, so he asked you to help him.
    Input
    The input contains zero or more test cases and the first line of the input gives the number T of test
    cases. For each test case:
    There is only one line with an integer n.
    Output
    For each test case, output the number of the solution that an n  n ladder is covered by exactly n
    rectangles, which is described above.
    Notice that the answer might be too large, so you should output the answer modulo 1000000007.
    Limits
    0 <=T <=1 000
    1 <=n <=1000
    Sample input and output
    Sample Input Sample Output
    1
    4
    14
    Note
    The diagram explaining the case where n = 4 is shown below:

    大致复盘了一下应该怎么想。

    从求n阶阶梯形由n个矩形填满的种类这个问题本身我们大致能嗅到一点DP的味道,并且同时比较容易发现搜索保证最后恰好是n个填满相当困难。

    于是找一下有没有最优子结构。

    关键在于一个矩形不可能填满两个棱角。所以每个棱角都必然由不同的矩形填充,然后考虑左上角的那个格子由某个矩形填充,左上角的格子和一个棱角格子由一个矩形占据,然后问题就被划分成了两个同理的子问题。

    另外有1000个cases,但是只用算一遍然后对每个case输出答案即可。

     1 #include<cstdio>
     2 #include<algorithm>
     3 #define rep(i,a,b) for(int i=a;i<=b;++i)
     4 #define p 1000000007;
     5 using namespace std;
     6 const int MAXN=1010;
     7 long long int dp[MAXN];
     8 int main()
     9 {
    10    // freopen("in.txt","r",stdin);
    11     int T;
    12     scanf("%d",&T);
    13     int n;
    14     dp[0]=1;dp[1]=1;
    15     rep(i,2,1000)
    16     {
    17         rep(j,1,i)
    18         {
    19             dp[i]+=dp[i-j]*dp[j-1];
    20             dp[i]%=p;
    21         }
    22     }
    23     rep(i,1,T)
    24     {
    25         scanf("%d",&n);
    26         printf("%lld
    ",dp[n]);
    27     }
    28     return 0;
    29 }
  • 相关阅读:
    ASP.NET MVC路由规则
    VS2013 修改TFS的本地映射路径
    新安装的VS的一些设置
    ASP.NET MVC验证标注的扩展-checkbox必选
    进入做Mvc项目的时候 返现某个文件夹下面css js png等静态文件都访问不了
    Mac入门 (二) 使用VMware Fusion虚拟机
    Mac入门(一)基本用法
    软件测试面试 (二) 如何测试网页的登录页面
    软件测试面试 (一) 如何测试一个杯子
    Python自动化测试 (二) ConfigParser模块读写配置文件
  • 原文地址:https://www.cnblogs.com/zhixingr/p/6748417.html
Copyright © 2011-2022 走看看