首先,我要对给予大众无私帮助和奉献的作者: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
"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!"