joj 1026解题报告
2011-01-28 14:39
288 查看
1026: The Staircases
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 1590 | 530 | Standard |
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; }
相关文章推荐
- JOJ 1064: Caeser Comes Back 解题报告
- JOJ 1051: Flip and Shift 解题报告
- JOJ 1065: Truck Driver 解题报告
- joj 1386解题报告
- JOJ 1054: Area 解题报告
- JOJ 1058 T9 解题报告
- HDOJ 1026 Ignatius and the Princess I 解题报告
- joj 1386解题报告
- joj 1920解题报告
- 衡阳八中1026windy数解题报告
- JOJ 1061: Rank The Teams 解题报告
- [BZOJ1026][SCOI2009]windy数 解题报告|数位dp
- JOJ 1055: Cog-Wheels 解题报告
- JOJ 1071: Maya Calendar 解题报告
- poj 1026 解题报告
- zoj1828||joj1949 解题报告
- 【九度OJ】题目1026:又一版 A+B 解题报告
- JOJ 1060: Time And Motion 解题报告
- joj 1031 Hanoi Tower Troubles Again!解题报告
- joj 1019解题报告