URAL 1017 Staircases 楼梯
2016-03-07 17:11
405 查看
递推:
设dp[tot][x]表示一共用tot个block,最后一个台阶block的个数为x个。所以dp[tot][x]=dp[tot-1][x-1]+dp[tot-x][x-1];
可以这样理解:
计算用tot个block&&最后一个台阶block数为x的种数:
因为最后一个台阶block数已经知道是x了,
所以只用关心倒数第二个台阶。
ans=倒数第二个台阶放[1,x-2]个block的种数+倒数第二个台阶放x-1个block的种数。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn= 500 ;
ll dp[maxn+4][maxn+4];
void work()
{
// memset(dp[0],0,sizeof dp[0]);
for(int tot=1;tot<=maxn;tot++)
{
dp[tot][0]=0;
for(int x=1;x<=tot;x++)
{
if(tot==1&&x==1) {dp[tot][x]=1;continue;}
dp[tot][x]=dp[tot-1][x-1]+dp[tot-x][x-1];
}
}
/*
摘自http://www.cnblogs.com/xiongqiangcs/archive/2013/05/09/3068379.html
这样写更简洁
for(int i = 2; i <= N; i ++ ){
for(int j = 1; j <= i; j ++){
dp[i][j] = dp[i-1][j-1] + dp[i-j][j-1];
}
}
*/
}
int main()
{
work();
int x;
while(~scanf("%d",&x))
{
ll ans=0;
for(int i=1;i<=x;i++)
{
ans+=dp[x][i];
}
ans--;
printf("%lld\n",ans);
}
return 0;
}
记忆化搜索
因为层数不确定,用记忆化搜索解决也比较合适。
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> using namespace std; typedef long long ll; const int INF =0x3f3f3f3f; const int maxn= 500 ; int n; ll dp[maxn+4][maxn+4]; ll DP(int x,int least) { if(~dp[x][least]) return dp[x][least]; if(!x) return dp[x][least]=1; ll ans=0; for(int i=least;i<=x;i++) { ans+=DP(x-i,i+1); } return dp[x][least]=ans; } int main() { memset(dp,-1,sizeof dp); while(~scanf("%d",&n)) { printf("%lld\n",DP(n,1)-1); } return 0; }
相关文章推荐
- VC6.0 error LNK2001: unresolved external symbol _main解决办法
- process launch failed: Security 解决方案
- Rails
- 1106. Lowest Price in Supply Chain (25)
- 利用thumbnailer API来提取图标视频或专辑的thumbnail
- poj 1995 Raising Modulo Numbers(快速幂模板)
- sendmail使用
- [leetcode] 11. Container With Most Water
- 可伸缩性, 可用性和稳定性模式 Scalability, Availability & Stability Patterns
- saiku - 优化多维度查询效率
- 【Xamarin挖墙脚系列:在VMware11中安装Mac10.11 EI Captain后的vmware tools】
- 常用的三种邮件协议
- URAL 1017 Staircases
- Intel 82599 ixgbe & ixgbevf CNA 卡驱动分析02——VF/PF/MailBox
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
- C# Linq 交集、并集、差集、去重http://blog.csdn.net/joyhen/article/details/39578325
- Dell R710 RAID1设置
- explain中的key_len
- Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]
- 简单总结RectF、Rect 和Matrix ,还有Paint的使用方法