您的位置:首页 > 其它

joj 1026解题报告

2011-01-28 14:39 288 查看



1026: The Staircases

ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE

3s8192K1590530Standard
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

这道题可以用动态规划和组合数学的思想,这里只说组合数学的思想,主要就是母函数的思想,输入N,那么此时有N个砖块来组成楼梯,我个人理解其实就是把楼梯可以分解成n个有不同砖块数目组成的长方形和正方形,如图中N=5时,有两种分解方法,第一种是分解为一个砖头组成的正方形,和四个方块组成的长方形,第二种是将其分解为一个砖头组成的正方形和四个砖块组成的长方形。并且每输入一个N,那么一定可以按照这种方法分解,那么这个道题就可以变成一道组合计数的问题,从而可以想到用母函数的思想来解决,(1+x^1)(1+x^2).....(1+x^N),算出x^N项的系数,然后再减1,即为所求的解,例如,当N=5的时候,(1+x^1)(1+x^2)(1+x^3)(1+x^4)(1+x^5),求出x^5的系数为3,x^5=1(x^0)*x^5=x^1*x4=x^2*x^3,而第一种1*x^5是不符合情况的,因为,这种相当于只有一个由5个砖块组成的长方形,想象一下,这就是1个楼梯,不符合题意,所以要将这个情况减去。
代码:
语言:c++
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int i,j,n;
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];
}
}
while(cin>>n)
{
if(n==0)
break;
printf("%.0lf/n",ans
-1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: