一道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 ...
随机推荐
- Redis开发规范
1.冷热数据分离,不要将所有数据全部都放到Redis中 虽然Redis支持持久化,但是Redis的数据存储全部都是在内存中的,成本昂贵.建议根据业务只将高频热数据存储到Redis中[QPS大于5000 ...
- 一、Redis的数据类型
一Redis的数据类型 string:字符串 hash:哈希 list:列表 set:集合 zset:有序集合(sorted set) 1.string(字符串) redis最基本的类型.可以理解成与 ...
- Linux下切换使用两个版本的JDK
Linux下切换使用两个版本的JDK 我这里原来已经配置好过一个1.7版本的jdk. 输出命令: java -version [root@hu-hadoop1 sbin]# java -version ...
- 如何定位jdk中的native方法源码?
前提条件:已下载openjdk的源码. 以System类的arraycopy为例: 1. 根据关键字定位文件:grep -rn '"arraycopy"' ./openjdk关键字 ...
- System.gc()和-XX:+DisableExplicitGC启动参数,以及DirectByteBuffer的内存释放
首先我们修改下JVM的启动参数,重新运行之前博客中的代码.JVM启动参数和测试代码如下: -verbose:gc -XX:+PrintGCDetails -XX:+DisableExplicitGC ...
- oracle中查询当前系统时间用到的dual是什么?
oracle我们查询当前系统时间的时候,经常用到dual,这个是什么东西呢? -- 查询系统时间 结果:2018/04/17 15:00:48 -- select sysdate from dual; ...
- corePoolSize和maxPoolSize的区别
受限于硬件.内存和性能,我们不可能无限制的创建任意数量的线程,每一台机器允许的最大线程是一个有界值.因此ThreadPoolExecutor管理的线程数量是有界的.线程池就是用这些有限个数的线程,去执 ...
- ASCII编码、Unicode编码、UTF-8
一.区别 ASCII.Unicode 是“字符集” UTF-8 .UTF-16.UTF-32 是“编码规则” 其中: 字符集:为每一个「字符」分配一个唯一的 ID(学名为码位 / 码点 / Code ...
- HTML编辑笔记4
1.CSS(层叠样式表) 2.CSS语法 选择器{ 属性名1:属性值1: 属性名2:属性值2: } 3.引用CSS的三种方式 第一种:行内样式 例:<a style="color:re ...
- node 慕课笔记
global global.testVar2 = 200; 在别的文件中可以任意调用到 因为global是全局变量相当于js的window一样的