posts - 195,  comments - 30,  trackbacks - 0

One curious child has a set of N little bricks. From these bricks he builds different staircases. Staircase consists of steps of different sizes in a strictly descending order. It is not allowed for staircase to have steps equal sizes. Every staircase consists of at least two steps and each step contains at least one brick. Picture gives examples of staircase for N=11 and N=5:

Your task is to write a program that reads from input numbers N and writes to output numbers Q - amount of different staircases that can be built from exactly N bricks.


Input

Numbers N, one on each line. You can assume N is between 3 and 500, both inclusive. A number 0 indicates the end of input.

 

 


Output

Numbers Q, one on each line.

 


Sample Input

3
5
0

Sample Output

1
2
方法1,动态规划

#include<iostream>
#include<cstdlib>
using namespace std;
  int main()
  {
  freopen("s.txt","r",stdin);
  freopen("key.txt","w",stdout);
  double f[501][501]={0};
  double s;
  int i,j,k,n;
  for(i=3;i<=500;i++)
  for(j=1;j<=(i-1)/2;j++)
    f[i][j]=1;
  for(i=3;i<=500;i++)
    for(j=1;j<=(i-1)/2;j++)
    {
 for(k=j+1;k<=(i-j-1)/2;k++)
   f[i][j]=f[i-j][k]; 
 }
    while(scanf("%d",&n),n) {
        s=0;
        for(i=1;i<=(n-1)/2;i++) s+=f[n][i]; //f[n]=f[n][1]+f[n][2]+-----+f[n][floor((i-1)/2)]
        printf("%.0f\n",s);
    }
  //system("PAUSE");
  return   0;
  }
更妙的方法:生成函数法
计算(1+x)(1+x^2)(1+x^3)-----,x^n的系数即为所求
int i,j;
double ans[510]={1,1};//已经把ans[1]和ans[0]赋为1了,其余为0
 for(i=2;i<=500;i++) {  
        for(j=500;j>=0;j--) { 
            if(i+j<=500) ans[i+j]+=ans[j];
        } 
    } 

先计算(1+x)(1+x^2)
再计算(1+x)(1+x^2)   *(1+x^3)
posted on 2009-06-27 10:44 luis 阅读(411) 评论(0)  编辑 收藏 引用 所属分类: 组合数学动态规划给我启发题

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年3月>
272812345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

友情链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜