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. monkey测试===通过monkey测试检查app内存泄漏和cpu占用

    最近一直在研究monkey测试.网上资料很多,但都是一个抄一个的.原创的很少 我把检查app内存泄漏的情况梳理一下: 参考资料: Monkey测试策略:https://testerhome.com/t ...

  2. python之operator操作符函数

    operator函数主要分为以下几类:对象比较.逻辑比较.算术运算和序列操作. 举例: #python 3.4 >>> operator.eq(1,2)False >>& ...

  3. 004 ConcurrentHashMap原理

    下面这部分内容转载自: http://www.haogongju.net/art/2350374 JDK5中添加了新的concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能.因为 ...

  4. Redis、mongdb、memcached的个人总结

    有测试的实例:http://colbybobo.iteye.com/blog/1986786 详细描述优缺点:https://www.cnblogs.com/binyue/p/4582550.html

  5. jq监听ajax执行开始,出错,结束。

    $(“#msg”).ajaxComplete(function(event,request, settings){   $(this).append(“<li>请求完成.</li&g ...

  6. JavaScript跨域解决方法大全

    跨域的定义:JavaScript出于安全性考虑,同源策略机制对跨域访问做了限制.域仅仅是通过“URL的首部”字符串进行识别,“URL的首部”指window.location.protocol +win ...

  7. php的设计模式

    1.单一职责原则 单一职责原则(Single Responsibility Principle) 含义:1.避免相同的职责分散到不同的类中,2.避免一个类承担太多的职责: srp的好处: 减少类之间的 ...

  8. java常用设计模式学习心得

    学习自:http://shenzhenchufa.blog.51cto.com/730213/161581 代码来自:http://shenzhenchufa.blog.51cto.com/73021 ...

  9. 多路复用I/O模型select() 模型 代码实现

    多路复用I/O:  socket编程之select(),poll(),epoll() 代码: client.c #include <stdio.h> #include <sys/ty ...

  10. php上传文件常见错误

    今天在文件上传过程中遇到的文件上传不过去,和网页报错,最后经查看总结有以下几个方面 上传文件错误码 error=0 正常上传 error=1 上传的大小超过了input[type=file]的文件上传 ...