【OpenJudge9277】【递推】Logs Stacking堆木头
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堆木头的更多相关文章
- 【noi 2.6_9277】Logs Stacking堆木头(DP)
题意:给出在最底层的木头的个数,问有多少种堆放木头的方式.要求木头必须互相挨着在一起. 解法:f[i]表示最底层i个木头的堆放木头的方式.注意递推的思想!只需知道上一层堆放0~i-1个(即最底层堆放i ...
- 【Openjudge 9277 Logs Stacking堆木头】 题解
题目链接:http://noi.openjudge.cn/ch0206/9277/ ... #include <algorithm> #include <iostream> # ...
- 算法技巧讲解》关于对于递推形DP的前缀和优化
这是在2016在长沙集训的第三天,一位学长讲解了“前缀和优化”这一技巧,并且他这一方法用的很6,个人觉得很有学习的必要. 这一技巧能使线性递推形DP的速度有着飞跃性的提升,从O(N2)优化到O(N)也 ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- 递推,大数存储E - Order Count
Description If we connect 3 numbers with "<" and "=", there are 13 cases: 1) ...
- 【主席树维护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 ...
- hdu4045(递推)
不会斯特林数的只能用递推思想了,结果发现推出来的就是斯特林数... #include <stdio.h> #include <stdlib.h> #include <st ...
- POJ 2166 Heapsort(递推)
Description A well known algorithm called heapsort is a deterministic sorting algorithm taking O(n l ...
- ACM_支离破碎(递推dp)
支离破碎 Time Limit: 4000/2000ms (Java/Others) Problem Description: 远古时期有一位魔王想向一座宫殿里的公主求婚.为了考验魔王的智力,太后给了 ...
随机推荐
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
- Pylot网站Web服务器性能和负载压力测试-适用Windows可绘制图表
为了能够准确地评估网站服务器对网络流量的承受能力,我们一般会采取模拟网站用户访问,通过不断地增加并发数,延长访问时长,从而最终得出网站Web服务器的性能和负载能力.当然也可以通过Web压力测试,来完善 ...
- springboot在不同环境下进行不同的配置
原文链接:http://www.cnblogs.com/java-zhao/p/5469183.html 不同的环境设置一个配置文件,例如:dev(开发)环境下的配置设置在application-de ...
- Jmeter接口测试示例
如果是Web,需要使用badboy进行录制,今天讲的是接口,因此可以不用录制. (1)新建测试计划 (2)添加http请求默认值 (3)添加http信息头管理器 (4)添加token的正则表达式:&q ...
- html,图片上传预览,input file获取文件等相关操作
input file常用方法: var obj=document.getElementById("upimage"); var file=obj.files[0];//获取文件数据 ...
- [PAT] 1146 Topological Order(25 分)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- 关于Sphinx中使用 RealTime Index的问题
我们有了完整索引和增量索引,为什么还需要研究实时索引? 1.完整索引每个晚上空闲时执行一次,时间较长,但问题不大,因为IO慢,CPU累,但那个时间段基本没有人使用平台,比如凌晨2点. 2.增量索引:目 ...
- 通过kubeadm安装kubernetes 1.7文档记录[docker容器方式]
参照了网上N多文档,不一一列表,共享精神永存!!!! ================================================== 获取所有安装包 安装包分为两类,rpm安装包 ...
- PHP策略设计模式
<?php /** 抽象策略角色,以接口实现 */ interface Strategy { /** 算法接口 */ public function algorithmInterface(); ...
- 论文笔记-SPP_NET中提到的金字塔
时隔这么久终于考完试放假了,现在终于有时间开始研究spp net的相关内容了,看了几篇网上的博客,发现看完之后还是不是很懂,于是乎下载了spp net的原始论文<Spatial Pyramid ...