HDU 1028 Ignatius and the Princess III伊格和公主III(AC代码)母函数

本题听说可用递推、DP等方法来做,但是此题是母函数的入门经典喔~所以我用了母函数

 1 #include <iostream>
 2 #define N 120
 3 using namespace std;
 4 int ans[N+1],sup[N+1];//ans保存答案,sup保存临时值
 5 void main()
 6 {
 7     int num=0,i,j,k;
 8     for(i=0;i<N+1;i++)    //全部初始化为1
 9         ans[i]=1;
10     for(i=2;i<=N;i++)    // 第i个括号
11     {
12         for(j=0;j<=N;j+=i)  //第i个括号中的第j项
13             for(k=0;k+j<=N;k++)  //临时答案中的第k个数字
14                 sup[j+k]+=ans[k];
15         for(k=0;k<=N;k++)  //每解决一个括号,得将答案保存,后一括号乘以此答案得出新答案
16             ans[k]=sup[k];
17         memset(sup,0,sizeof(sup));//临时数组清零
18     }
19     while(cin>>k)    //用c++的话得这么写才对
20         cout<<ans[k]<<endl;
21 }

1028

题意:输入一个数n,打印出一此数字可以有多少种组成方法,每一方法是不记录排列顺序的。用来组成的数字可以有1、2、3....n。比如n个1组成了n,一个n也组成n。这就算两种。1=1,2=1+1=2,3=3=1+2=1+1+1,而1+2和2+1只能算一种。n最大为120。

思路:母函数看了一天没理解出点什么好用的东西,网上一堆人讲不清楚,或许我没发现好的教材。我只会这解此题哎...。关于母函数的原理不讲了。讲怎么实现几个括号相乘。

举例总比讲一堆废话有用,看题:

我们要算的n是等于120,把其简化为5,就是说设n最大为5,道理一样的。5一共有7种方法对吗!自己手写吧。

如果想要得出结果,那么一共有5个括号要相乘,分别如下:

为什么是5个括号?

第1个括号:最多可由5个1组成。

第2个括号:最多只能有两个2对吧?如果3个2就已经是6了,6超过5了喔~

第3~5个括号:好了,你已经学会了类推法!

怎么乘?

以初中老师教的方法是第2个括号乘以第1个括号,然后第3个括号乘以上一次相乘的结果....

一共4步,也就是n-1步,那么i=2to n是不是有n-1步?比如i=2、3、4、5,就是4步嘛。

接着,两个括号的相乘要怎么办?

在i的循环内再加一个循环,得把每一步的右边括号里的每个数都要乘以左边括号一次。那j的上下限是多少?下限为0,上限是5,即n。但是过程不是只是自增一,而是自增i个单位,为什么?第1步j的值就会为0、2、4,间隔为2,而第2步中j的值就会为0、3,间隔为3,如果n>5的话,第2步的j的值就会为0、3、6了。所以呢!j=0 to n递增方式(j+=i)。

此时就完毕了?没呢!

上一步骤只是将每一步中的右边括号拆出来逐个乘以左边括号,但是左边括号还没拆呢。对于每个j,也就是右边括号里的每个值,都得逐个乘以左边括号里的每个值。那么得再叠加一层循环了,下限为0,上限为5,即为n。又为什么?第1个括号中不是有6位吗,而每个括号中还有个1在最前面,别落下了。这里讲不出思路了,意会吧!

讲代码:

代码中ans数组先保存第1个括号中的每个系数,也就是全为1,比如ans[0]=1,ans[1]=1.....

这个数组在同一个i值的情况下不能变化,我本来想用一个数组解决,但是后来发现搞不定,所以开了个辅助数组sup。

当每次右括号中的一个数乘以左括号时,将相乘的结果累加在sup数组中,整个右括号里的数都乘过一次后,再将sup数组中的值存到ans数组中, 作为临时答案。

这样子ans中最后保存的就是所要的系数了,ans的下标代表x的几次方,比如ans[4]代表x的4次方的系数。

将整个乘法的过程自己实现一下才知道其精髓,和很多需要注意的地方,看代码实在不是好习惯。

时间: 2024-08-29 06:49:23

HDU 1028 Ignatius and the Princess III伊格和公主III(AC代码)母函数的相关文章

hdu 1028 Ignatius and the Princess III(母函数,完全背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1028 整数划分问题. 第一道母函数...母函数入门 小于等于n的整数共有n个,1,2......n,每个数都有无限多个,对于整数1,它所对应的母函数为(1+x+x^2+...+x^k+...),整数2对应的母函数为(1+x^2+X^4+...+x^(2*k)+...),整数3对应的母函数为(1+x^3+x^6+...+x^(3*k)+...),以此类推,直到整数n. 那么n的整数划分的个数就是这n个母函数乘积

hdu 1028 Ignatius and the Princess III 简单dp

题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是dp[i][i].那么dp[i][j]=dp[i][j-1]+dp[i-j][i-j],dp[i][j-1]是累加1到j-1的结果,dp[i-j][i-j]表示的就是最大为j,然后i-j有多少种表达方式啦.因为i-j可能大于j,这与我们定义的j为最大值矛盾,所以要去掉大于j的那些值 /*******

hdu 1028 Ignatius and the Princess III(整数划分)

Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12731    Accepted Submission(s): 8999 Problem Description "Well, it seems the first problem is too easy. I will let

ACM: HDU 1028 Ignatius and the Princess III-DP

HDU 1028 Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description "Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says. &qu

hdu 1028 Ignatius and the Princess III 【整数划分】

Ignatius and the Princess III                                                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15730    Accepted Submission(

HDU 1028 Ignatius and the Princess III dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 一道经典题,也是算法设计与分析上的一道题,可以用递推,动态规划,母函数求解,我用的是动态规划,也就是递推的变形. dp[i][j]表示数i的划分中最大数不超过j的划分的个数 状态转移方程: if(j > i) dp[i][j] = dp[i][i]; if(j == i) dp[i][j] = dp[i][j - 1] + 1; if(j < i) dp[i][j] = dp[i][j -

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

Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15794    Accepted Submission(s): 11138 Description "Well, it seems the first problem is too easy. I will let you kno

HDU 1028.Ignatius and the Princess III【分析】【8月20】

Ignatius and the Princess III 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

(简单母函数模板)hdu 1028 Ignatius and the Princess III

Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14405    Accepted Submission(s): 10142 Problem Description "Well, it seems the first problem is too easy. I will le

HDU 1028 Ignatius and the Princess III (动态规划)

题目链接:HDU 1028 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