Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2834024
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: C/C++

2012-05-02 10:44:22

整数拆分
为什么1~n,因为它可以从1~m,取值,并且可以取多次,而m<=n;
(1+x+x2+x3...xn)*(1+x2+x4..xn)*(1+x3+x6..xn)*...(1+xn+x2n+...xnn)
两点要注意:
k的变化:k在第2 括号  k=k+2;这样递增
     k在第3 括号 k=k+3;
     k在第4 括号 k=k+4;  
     所以k=k+i;
i的变化:究竟有多少个括号;i=2从第二个括号开始
             看每个括号的第二项,可以推出x2,x3,...xn
             那就一定是n个括号啦 
只是一个括号数,i以上两点,构成母函数的变化,基于它的变化可以解决很多问题
              
所以输出c1[n],就是xn的系数( n 的组合数)

Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.

"The second problem is, given an positive integer N, we define an equation like this:
  N=a[1]+a[2]+a[3]+...+a[m];
  a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
  4 = 4;
  4 = 3 + 1;
  4 = 2 + 2;
  4 = 2 + 1 + 1;
  4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
 

Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
 

Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.



点击(此处)折叠或打开

  1. #include<stdio.h>



  2. /*
  3. //c1是用来存放展开式的系数的,而c2则是用来计算时保存的,
  4. //他是用下标来控制每一项的位置,比如 c2[3] 就是 x^3 的系数。
  5. //用c1保存,然后在计算时用c2来保存变化的值。
  6. */
  7. const int max=120;
  8. int c1[max+1],c2[max+1];

  9. int main()
  10. {
  11.     int n,i,j,k;
  12.     while(scanf("%d",&n)!=EOF)
  13.     {
  14.         
  15.         // 计算的方法还是模拟手动运算,一个括号一个括号的计算,
  16.         // 从前往后
  17.         //对于 1+x+x^2+x^3+ 他们所有的系数都是 1
  18.         // 而 c2全部被初始化为0是因为以后要用到 c2[i] += x ;
  19.         for(i=0;i<=n;i++)
  20.         {
  21.             c1[i]=1;
  22.             c2[i]=0;
  23.         }

  24.         //第一层循环是一共有 n 个小括号,而刚才已经算过一个了
  25.         //所以是从2 到 n
  26.         for(i=2;i<=n;i++)
  27.         {
  28.             for(j=0;j<=n;j++)
  29.             {
  30.                 for(k=0;k+j<=n;k+=i)
  31.                 {
  32.                     c2[k+j]+=c1[j];
  33.                 }
  34.             }    

  35.             for(j=0;j<=n;j++)
  36.             {
  37.                 c1[j]=c2[j];
  38.                 c2[j]=0;
  39.             }
  40.         }

  41.         printf("%d\n",c1[n]);
  42.     }
  43.     return 0;
  44. }

阅读(595) | 评论(0) | 转发(0) |
0

上一篇:母函数基本模板

下一篇:hdu1398Square Coins

给主人留下些什么吧!~~