joj 1026解题报告

 

 1026: The Staircases


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s 8192K 1590 530 Standard

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++
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章