Logs Stacking堆木头

总时间限制: 1000ms 内存限制: 131072kB

【描述】

Daxinganling produces a lot of timber. Before loading onto trains, the timberjacks will place the logs to some place in the open air first. Looking from the sideway, the figure of a logs stack is as follows: 
We have known that the number of logs in each layer is fewer than the lower layer for at least one log, and that in each layer the logs are connected in a line. In the figure above, there are 12 logs in the bottom layer of the stack. Now, given the number of logs in the bottom layer, the timberjacks want to know how many possible figures there may be. 
给出在最底层的木头的个数,问有多少种堆放木头的方式,当然你的堆放方式不能让木头掉下来. 
在堆放的时候木头必须互相挨着在一起.

【输入】

The first line of input contains the number of test cases T (1 <= T <= 1000000). Then T lines follow. Every line only contains a number n (1 <= n <= 200000) representing the number of logs in the bottom layer.

【输出】

For each test case in the input, you should output the corresponding number of possible figures. Because the number may be very large, just output the number mod 10^5.

【样例输入】

4
1
2
3
5

【样例输出】

1
2
5
34

【Solution】

  用dp[i]表示底层数为i的总方案数。

  我们可以发现,当底层数为i,上一层要放j个木头的时候,一共有(i-j)种情况。举个例子,当底层为5准备放2个木头时共有一下5-2=3种情况:

  所以我们可以得到一个状态转移方程式:dp[i]=dp[i-1]*(i-(i-1))+dp[i-2]*(i-(i-2))+...+dp[1]*(i-1)+1。这个转移方程可以理解为枚举所有可以往基层上一层放木头的可能性,把所有的可能方案相加。

  于是我就傻乎乎的N2算了一波,结果TLE(内心:这么性感的程序你还给我TLE???)。怎么优化到N呢?拿dp[4]和dp[5]做例子:

  dp[4]=dp[3]*1+dp[2]*2+dp[1]*3+1

  dp[5]=dp[4]*1+dp[3]*2+dp[2]*3+dp[1]*4+1

  注意红色的部分,我们发现dp[5]比dp[4]多了一个dp[1]+dp[2]+dp[3]+dp[4],我们会发现dp[i]比dp[i-1]多一个dp[1]+dp[2]+...+dp[i-1]。

看到这里应该都能想到优化方案——用前缀和优化,这道题就A啦。

  AC代码:

  

 #include <cstdio>
int T,N;
int dp[],sum[];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&N); dp[]=sum[]=;
for(int i=;i<=N;++i){
dp[i]=(dp[i-]%+sum[i-]%)%;
sum[i]=(sum[i-]%+dp[i]%)%;
}
printf("%d\n",dp[N]%);
}
return ;
}

【OpenJudge9277】【递推】Logs Stacking堆木头的更多相关文章

  1. 【noi 2.6_9277】Logs Stacking堆木头(DP)

    题意:给出在最底层的木头的个数,问有多少种堆放木头的方式.要求木头必须互相挨着在一起. 解法:f[i]表示最底层i个木头的堆放木头的方式.注意递推的思想!只需知道上一层堆放0~i-1个(即最底层堆放i ...

  2. 【Openjudge 9277 Logs Stacking堆木头】 题解

    题目链接:http://noi.openjudge.cn/ch0206/9277/ ... #include <algorithm> #include <iostream> # ...

  3. 算法技巧讲解》关于对于递推形DP的前缀和优化

    这是在2016在长沙集训的第三天,一位学长讲解了“前缀和优化”这一技巧,并且他这一方法用的很6,个人觉得很有学习的必要. 这一技巧能使线性递推形DP的速度有着飞跃性的提升,从O(N2)优化到O(N)也 ...

  4. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  5. 递推,大数存储E - Order Count

    Description If we connect 3 numbers with "<" and "=", there are 13 cases: 1) ...

  6. 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11

    Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...

  7. hdu4045(递推)

    不会斯特林数的只能用递推思想了,结果发现推出来的就是斯特林数... #include <stdio.h> #include <stdlib.h> #include <st ...

  8. POJ 2166 Heapsort(递推)

    Description A well known algorithm called heapsort is a deterministic sorting algorithm taking O(n l ...

  9. ACM_支离破碎(递推dp)

    支离破碎 Time Limit: 4000/2000ms (Java/Others) Problem Description: 远古时期有一位魔王想向一座宫殿里的公主求婚.为了考验魔王的智力,太后给了 ...

随机推荐

  1. Linux命令--hostname和uname

    hostname命令 hostname命令用于显示和设置系统的主机名称.环境变量HOSTNAME也保存了当前的主机名.在使用hostname命令设置主机名后,系统并不会永久保存新的主机名,重新启动机器 ...

  2. ubuntu下定时弹窗记录工作日志

    背景 记录工作日志,是一个很好的习惯,但不容易坚持,本来打算每天记录,但经常拖延,拖着拖着,有一些事情就忘记了. 等到写周报或月报的时候,才会开始翻邮件,聊天记录,各个仓库的提交log等,回忆都干了些 ...

  3. C语言将字符串转换成对应的数字(十进制、十六进制)【转】

    转自:http://wawlian.iteye.com/blog/1315133 问题1:讲一个十进制数字的字符串表示转换成对应的整数.举例:将“”转换成整数1234. C代码 收藏代码 /*将字符串 ...

  4. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff 思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为  n 的简单多边形,每次只能画一条边或者一个格子的对角 ...

  5. 破解 myeclipse 2014 professional,步骤很重要

    网易 博客 GACHA-动漫萌妹汇集地 LOFTER-最美图片社交 印像派-我的照片书 这些小语种最有前途,免费学 注册登录  加关注     日志     Windows下解决PostgreSQL8 ...

  6. FineReport——函数

    1.VALUE(tableData,col,row)返回tableData中列号为col的值, 行号为row.备注:先从报表数据集中查找,然后再从服务器数据集中查找,返回的是tableData的行数. ...

  7. Mysql 数据库学习笔记04 函数

    一.创建自定义函数 * 使用自定义函数,可以返回字符串.整型.实数或者其他类型: create [aggregate] function 名称 (参数列表) return type begin //函 ...

  8. windows下github 出现Permission denied (publickey)

    github教科书传送门:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 再学习到 ...

  9. LeetCode212. Word Search II

    https://leetcode.com/problems/word-search-ii/description/ Given a 2D board and a list of words from ...

  10. 【hdoj_2570】迷障

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2570 思路:贪心法.要求在浓度不超标的情况下,解药的最大体积.由于体积相同,可以先对浓度排序,然后从浓度小 ...