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. Perl6 Bailador框架(2):路径设置

    use v6; use Bailador; =begin pod get表示是get发送 post表示是post发送 get/post 后面的 '/name' 表示是路径 => sub {} 是 ...

  2. python基础===通过菲波那契数列,理解函数

    def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.&quo ...

  3. JS实现全选与取消 Jquery判断checkbox是否被选中

    1.JS实现checkbox全选与取消 <body> <input type="checkbox" name="select_all"/> ...

  4. NOIP 2013 day2

    tags: 模拟 贪心 搜索 动态规划 categories: 信息学竞赛 总结 积木大赛 花匠 华容道 积木大赛 Solution 发现如果一段先单调上升然后在单调下降, 那么这一块的代价是最高的减 ...

  5. 10:django 模板语言

    Django的模板语言的目的是取得力量和易用性之间的平衡,与其他的模板语言相比,django模板语言显得更简单,更专一, django模板系统由模板,变量,过滤器,标签,注释等主要部分组成 模板 一个 ...

  6. hdu 1533(最小费用最大流)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. Linux 基础——处理文件与目录的命令

    继续第三天学习,每天下班后积累一点点,始终相信厚积薄发. 一.处理文件的命令 touch dest_file:在当前目录下创建指定的文件. cp source dest:将指定的猿文件复制到目标文件, ...

  8. “无法识别的配置节system.webServer”解决办法

    在Winsows 2008 Server 上安装asp.net 1.1 的应用程序,在启用默认文档或者浏览目录时会向 web.config 文件添加 <system.webServer> ...

  9. 前端读者 | 前端开发者调试面板vConsole

    来着微信团队开源的一个调试工具,[GitHub地址]https://github.com/Tencent/vConsole 一个轻量.可拓展.针对手机网页的前端开发者调试面板. 特性 查看 conso ...

  10. nginx基于tcp负载均衡

    官方参考文档:http://nginx.org/en/docs/stream/ngx_stream_core_module.html 只有nginx1.9以上的版本才支持tcp负载均衡 配置必须出现在 ...