一道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 ...
随机推荐
- [洛谷 P2709] 小B的询问
P2709 小B的询问 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数 ...
- 微信小程序初见+nodejs服务端 (一个简单的博客)
推荐网址: 腾讯云快速开发(nodejs前后端):https://developers.weixin.qq.com/miniprogram/dev/qcloud/qcloud.html#%E5%AF% ...
- python字符串内建函数
- 把旧系统迁移到.Net Core 2.0 日记(10) -- EF core 和之前版本多对多映射区别
EF Core 现在不支持多对多映射,只能做2个一对多映射. 比如Product和Category 我现在定义Product和Category是多对多关系. 那么实体定义如下: public clas ...
- office install problems
regedit 0000 "00005"或"00002"开头的项 remove all regedit options
- Ubuntu上安装MySQL
Ubuntu上安装MySQL非常简单只需要几条命令就可以完成.`````` sudo apt-get update sudo apt-get install mysql-server 会弹出提示,让输 ...
- 稀疏 部分 Checkout
To easily select only the items you want for the checkout and force the resulting working copy to ke ...
- jdk8-全新时间和日期api
1.jdk8日期和时间api是线程安全的 1.java.time 处理日期时间 2.java.time.temporal: 时间校正器.获取每个月第一天,周几等等 3.java.time.forma ...
- 201621123001 《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 关于泛型 <T extends Comparable <T ...
- day55 jQuery 练习
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...