HDU 1028 Ignatius and the Princess III(母函数或dp)

                                              Ignatius and the Princess III

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

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
  
  
4 10 20
 
Sample Output
  
  
5 42 627
 
Author
Ignatius.L

题解:求组合数。
第一种:dp
第二种:母函数

AC代码:
dp:
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
	int dp[122][122]={0};
	int n;
	for(int i=0;i<121;i++)
    {
    	dp[i][0]=1;
		dp[i][1]=1;
		dp[0][i]=1;
		dp[1][i]=1;
	}
    for(int i=2;i<121;i++)
		for(int j=2;j<121;j++){
			if(i>=j)
			 	dp[i][j]=dp[i][j-1]+dp[i-j][j];
		   else   
		 		dp[i][j]=dp[i][i];
		}
 		
		while(scanf("%d",&n)==1)
		printf("%d\n",dp[n][n]);
 
	return 0;
}

母函数:
//母函数:
//G(x) = (1 + x^1 + x^2..+x^n)(1 + x^2 + x^4 + x^6 + ...)(1 + x^3 + x^6 +..)(..)(1 + x^n)
//第一个表达式(1 + x^1 + x^2..+x^n)中 x的指数代表【解中'1'的出现次数】 比如x^2 = x^(1 * 2) 这是'1'出现了两次 x^3 = x^(1 * 3) '1'出现3次
//相似的 第二个表达式(1 + x^2 + x^4 + x^6 + ...) x^4 = x^(2 * 2) '2'出现两次 x^6 = x^(2 * 3) '2'出现3次
//...以此类推 【* 1(0次项) 是代表该数字出现次数为0】

//乘法原理的应用:每一个表达式 表示的都是 某个变量的所有取值【比如第一个表达式 表示'1'可以取的值(即n拆分后'1'出现的次数)可以为 {0,1,2...n}】
//每个变量的所有取值的乘积 就是问题的所有的解(在本问题中表现为‘和’)
//例子:4 = 2 + 1 + 1就是  x^(1 * 2)【'1'出现2次】
//			* x^(2 * 1)【'2'出现1次】
//			* x^(3 * 0)【'3'出现0次】
//			* x^(4 * 0)【..】
//			的结果
//上述4个分式乘起来等于 1 * (x^4) 代表 4的一个拆分解
//所以 G(x)展开后 其中x^n的系数就是 n的拆分解个数
# include <cstdio>
int main()
{
	int C1[123], C2[123], n;
	while(scanf("%d", &n) != EOF)
	{
		for(int i = 0; i <= n; i++) //初始化 第一个表达式 目前所有指数项的系数都为1
		{
			C1[i] = 1;
			C2[i] = 0;
		}
		for(int i = 2; i <= n; i++) //第2至第n个表达式
		{
			for(int j = 0; j <= n; j++) //C1为前i-1个表达式累乘后各个指数项的系数
			{
				for(int k = 0; j + k <= n; k += i) //k为第i个表达式每个项的指数 第一项为1【即x^(i * 0)】(指数k=0),第二项为x^(i * 1)(指数为k=i), 第三项为x^(i * 2)... 所以k的步长为i
				{
					C2[j + k] += C1[j];  //(ax^j)*(x^k) = ax^(j+k) -> C2[j+k] += a  【第i个表达式每一项的系数都为1; a为C1[j]的值(x^j的系数); C2为乘上第i个表达式后各指数项的系数】
				}
			}
			for(int j = 0; j <= n; j++) //刷新当前累乘结果各指数项的系数
			{
				C1[j] = C2[j];
				C2[j] = 0;
			}
		}
		printf("%d\n",C1[n]);
	}
		return 0;
}


 母函数相关知识请看本博客的数论---素数与函数。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值