一道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输出答案即可。
#include<cstdio>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define p 1000000007;
using namespace std;
const int MAXN=;
long long int dp[MAXN];
int main()
{
// freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
int n;
dp[]=;dp[]=;
rep(i,,)
{
rep(j,,i)
{
dp[i]+=dp[i-j]*dp[j-];
dp[i]%=p;
}
}
rep(i,,T)
{
scanf("%d",&n);
printf("%lld\n",dp[n]);
}
return ;
}
一道DP的更多相关文章
- 值得一做》关于一道DP+SPFA的题 BZOJ1003 (BZOJ第一页计划) (normal-)
这是一道数据范围和评测时间水的可怕的题,只是思路有点难想,BUT假如你的思路清晰,完全了解怎么该做,那就算你写一个反LLL和反SLE都能A,如此水的一道题,你不心动吗? 下面贴出题目 Descript ...
- Kickstart Round D 2017 problem A sightseeing 一道DP
这是现场完整做出来的唯一一道题Orz..而且还调了很久的bug.还是太弱了. Problem When you travel, you like to spend time sightseeing i ...
- 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 一道dp[不太好写]
http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2281 Description An arithmetic progression ...
- nyoj16矩形嵌套(第一道dp关于dag的题目)
http://acm.nyist.net/JudgeOnline/problem.php?pid=16 题意:有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c, ...
- 2018微软实习笔试一道dp题目总结
题意大概是说在一维数轴上起点和终点的距离是d,现在我们要从起点走到终点.每走一个单位长度消耗一个单位能量,初始时有K单位能量.同时在起点和终点之间分布一些加油站a1,a2,...an,给你加油站数量. ...
- USACO 6.1 Postal Vans(一道神奇的dp)
Postal Vans ACM South Pacific Region -- 2003 Tiring of their idyllic fields, the cows have moved to ...
- 洛谷 P1466 集合 Subset Sums Label:DP
题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...
- 【63测试20161111】【BFS】【DP】【字符串】
第一题: tractor 题目描述 农场上有N(1 <= N <= 50,000)堆草,放在不同的地点上.FJ有一辆拖拉机,也在农场上.拖拉机和草堆都表示为二维平面上的整数坐标,坐标值在1 ...
随机推荐
- 循环中点击单个事件(巧用this,指向当前对象)
<em id='show' value="<?php echo $member['phone']; ?>" class="sui">&l ...
- telnet的装配及xinetd的讨论
telnet由于是不安全的明文传输所以基本被ssh取代了,尤其是在Linux上:不过还是可能会用到,且启停方式还有些不同所以还是有必要说明一下. rpm -qa | grep telnet #查看是否 ...
- IOS中position:fixed弹出框中的input出现光标错位的问题
解决方案是 在弹框出现的时候给body添加fixed <style type="text/css"> body{ position: fixed; width: 100 ...
- JS--label语句的使用
使用label语句可以在代码中添加标签,以便将来使用. 一般与for循环一起使用 如: var num = 0; outermost: for(var i=0;i<10;i++){ for(va ...
- MySQL(一) 初识MySQL
数据库基础 数据库是由一批数据构成的有序的集合,这些数据被存放在结构化的数据表里.数据表之间相互联系,反映了客观事物间的本质联系.数据库系统提供对数据的安全控制和完整性控制. 什么是数据库 数据库的发 ...
- img2html实现将图片转换成网页
简单介绍img2html的用法,安装就不用说了pip.这个包现只支持python2,支持python的话需改下源码这几个部分: 加注释的是修改的地方 #!/usr/bin/env python # e ...
- bzoj4278
题解: 把第一个串放好,加一个oo 然后把第二个串倒序放进来 然后就是http://www.cnblogs.com/xuanyiming/p/8510231.html这一题了 代码: #include ...
- 【Ctrl】 + 【Alt】 + 【F1~F6】 和 【Ctrl】 + 【Alt】 + 【T】打开的终端有什么不同?
ctrl +alt +Fn 打开的是模拟终端,简单说来,Linux系统一开机会自动打开6个模拟终端,然后自动切换到其中一个(一般来说是切换到图形界面的那个也就是说窗口管理器是在这6个模拟终端中运行的) ...
- :装饰者模式--Beverage
#ifndef __BEVERAGE_H__ #define __BEVERAGE_H__ #include <string> using namespace std; class Bev ...
- 5.10 C++内存管理操作符重载
参考:http://www.weixueyuan.net/view/6388.html 注意: 内存管理操作符new.new[].delete和delete[]同样也可以进行操作符重载,其重载形式既可 ...