杭电 ACM 1028 Ignatius and the Princess III

首先,我要对给予大众无私帮助和奉献的作者:Tanky Woo 表示感谢!同时提供他的文章链接:

http://www.wutianqi.com/?p=596

这题是典型的母函数(也叫做生成函数)题,有一定的难度,关键是理解母函数的概念和解法。关于这些我就不多说了,大家只要在百度上搜索就有了。

母函数这题,有很多要注意的地方,像我就是由于小的细节不注意,导致一错再错。比如那个 j 的步进问题,一开始是 j++, 后来是 j = j * 2,再后来是 j *= expr,最后才找到正确答案。

有些注释掉的代码,是我发现错误用的。在草稿上演算了许久,也没找到错误,便借助于观查过程来检查了。

我还把递归的解法也贴上来了,因为递归比较好做,同时理解递归的解题方法也比较有意义。可惜就是超时了。

// 母函数解法,终于成功了! /* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Date : 2011/3/8 Thuesday Result: 3574731 2011-03-08 15:56:26 Accepted 1028 15MS 324K 1635 B C++ pyy //----------------------------------------------------------------------------*/ #include <iostream> #include <stdio.h> using namespace std; void out(int *p, int n) { for (int i = 0; i <= n; i++) printf("%d ", *(i+p)); putchar('/n'); } int main() { int g[1001], t[1001], n; while (cin >> n) { int i, expr, j; expr = 0; // 初始化 for (i = expr; i <= n; i++) { g[i] = 1; t[i] = 1; } // 第 expr 个表达式。 for (expr = 2; expr <= n; expr++) { // 前一个表达式与当前表达式第 j 个数相乘。 // 此处 j 增加的步长应该注意! for (j = expr; j <= n; j += expr) { // 前一个表达式与当前表达式的 第 i 幂次的数相乘后, // 前一个表达式第 i 幂次的系数加 1. for (i = j; i <= n; i++) { // t 为前一个表达式,在与当前表达式 // 逐个相乘的过程中,始终不变。 g[i] += t[i - j]; } // out(g, n); } // t 复制为当前表达式,充当下一个表达式的前表达式 for (j = 0; j <= n; j++) t[j] = g[j]; // cout << "----/n"; // out(g, n); // cout << "-------/n/n"; } cout << g[n] << endl; } return 0; } ======================================递归解法====================================== // 递归解法,果断超时, 但能理解递归解法,很有意义。 /* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Date : 2011/2/27 Thursday Begin : 15:50 End : 17:35 //----------------------------------------------------------------------------*/ #include <iostream> #include <stdio.h> using namespace std; const int flag = 0xFFFFFFFE; // flag = 1111,1111,1111,1110 // the possibile forms of sequences with head of "head", example for Sample-1 /*------------------------------- Sample-1 -----------------------------------// such as: (head = 2, total = 4) 4 = 2 + 2; 4 = 2 + 1 + 1; //----------------------------------------------------------------------------*/ // 以 head 为开头的序列的可能形式, 示例见 Sample-1 int cal(int head, int total) { // the synonym of "if (head == 1 || head == 0)" // 相当于 "if (head == 1 || head == 0)" if (!(head & flag)) return 1; int tail, min, sum = 0, i; // the number behind head // 在 head 后面的数字 tail = total - head; // current trailing sequence forms after head could be diverse /*----------------------------- Sample-2 -----------------------------// such as : 3 = 3; 3 = 2 + 1; 3 = 1 + 1 + 1; //--------------------------------------------------------------------*/ // 当前 head 后面的尾序列的组成形式可能是多样性的, 比如上例。 if (tail > 1) { /*------------------------------------------------------------// the beggest element in trailing sequence must be the smaller one between tail and head //------------------------------------------------------------*/ /*------------------------------------------------------------// 尾序列中的最大元素必须是 head 和 tail 中的较小者。 //------------------------------------------------------------*/ min = tail > head ? head : tail; // calculate the possibile forms of tailing sequence, see // Sample-2 // 计算尾序列的可能形式 见 Sample-2 sum += cal(min, tail); } // there is only one possibility else sum += 1; // the possibile forms of sequences with head of "head = head - 1", // example for Sample-1 // 以 head = head - 1 为开头的序列的形式,示例见 Sample-1 sum += cal(head - 1, total); return sum; } int main() { int n; while (scanf("%d", &n)) { printf("%d/n", cal(n, n)); } return 0; }

======================================原题如下======================================

Ignatius and the Princess III

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

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值