【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: 远古时期有一位魔王想向一座宫殿里的公主求婚.为了考验魔王的智力,太后给了 ...
随机推荐
- c#使用selenium+Chromedriver参数配置
using System; //添加selenium的引用 using OpenQA.Selenium.PhantomJS; using OpenQA.Selenium.Chrome; using O ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做 ...
- Android SDK更新 Connection to http://dl-ssl.google.com refused
问题: Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xml, reason: Conne ...
- 12-7 NSDictionary
原文:http://rypress.com/tutorials/objective-c/data-types/nsdictionary NSDictionary 如同NSSet,NSDictionar ...
- vue 计算属性 实例选项 生命周期
vue 计算属性: computed:{} 写在new vue()的属性,只要参与运算,数据不发生变化时,次计算只会执行一次,结果缓存,之后的计算会直接从缓存里去结果.如果其中的值发生变化(不管几个) ...
- hdu 1081(最大子矩阵)
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1133(卡特兰数变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题意:排队买50块一张的票,初始票台没有零钱可找,有m个人持有50元,n人持有100元, ...
- zookeeper编程入门系列之zookeeper实现分布式进程监控和分布式共享锁(图文详解)
本博文的主要内容有 一.zookeeper编程入门系列之利用zookeeper的临时节点的特性来监控程序是否还在运行 二.zookeeper编程入门系列之zookeeper实现分布式进程监控 三. ...
- Redis实现分布式锁 php
一.分布式锁的作用: redis写入时不带锁定功能,为防止多个进程同时进行一个操作,出现意想不到的结果,so...对缓存进行插入更新操作时自定义加锁功能. 二.Redis的NX后缀命令 Redis有一 ...
- Nuget私服使用
首先前提是师父已经搭好私服环境了(怎么搭建参考https://www.cnblogs.com/liupengblog/archive/2012/09/10/2678508.html). 然后在vs中打 ...