【HDU 1028】【母函数 整数划分】Ignatius and the Princess III

来源:互联网 发布:软件打不开是什么原因 编辑:程序博客网 时间:2024/06/05 07:14

传送门:http://acm.split.hdu.edu.cn/showproblem.php?pid=1028

描述:

Ignatius and the Princess III

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19323    Accepted Submission(s): 13562


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.
 

Sample Input
41020
 

Sample Output
542627
 

Author
Ignatius.L
 

Recommend
We have carefully selected several similar problems for you:  1085 2152 1709 1059 1114 


思路一:

母函数的意思就是把n 用1,2,3,4,5`````来表示的种数,构造母函数:(1+x+x^2+x^3+`````)*(1+x^2+x^4+````)*(1+x^3+x^6+````)````

刘老师的母函数PPT见Here


代码一:

#include <bits/stdc++.h>using  namespace  std;#define rep(i,k,n) for(int i=k;i<=n;i++)const int N=130;int num1[N], num2[N];//num1存放目前所有划分数,num2存放更新时临时的划分数void init(){  rep(i, 0, N-1){     num1[i] = 1;     num2[i] = 0;  }  rep(i, 2, N-1){    rep(j, 0, N-1)//枚举已知范围      for(int k = 0; k + j < N; k += i){//枚举新增范围        num2[k + j] += num1[j];      }    rep(j, 0, N - 1){//重置      num1[j] = num2[j];      num2[j] = 0;    }  }}int  main(){  init();  int n;  while(~scanf("%d", &n)){    printf("%d\n", num1[n]);  }  return 0;}

思路二:

当然整数的划分也可以用递推做

可以参考http://www.cnblogs.com/IMGavin/p/5621370.html 和 http://blog.sina.com.cn/s/blog_677a3eb30100kqnn.html


代码二:

#include<stdio.h>#include<string.h>const int MAXN=130;int dp[MAXN][MAXN];//dp[i][j]表示 i 表示成最大的数不超过 j 的方法数int calc(int n,int m){        if(dp[n][m]!=-1) return dp[n][m];            if(n<1||m<1) return dp[n][m]=0;    if(n==1||m==1) return dp[n][m]=1;    if(n<m) return dp[n][m]=calc(n,n);    if(n==m) return dp[n][m]=calc(n,m-1)+1;    return dp[n][m]=calc(n,m-1)+calc(n-m,m);    }     int main(){    int n;    memset(dp,-1,sizeof(dp));        while(scanf("%d",&n)!=EOF)      printf("%d\n",calc(n,n));    return 0;}



0 0