今天稍微学习了母函数,决定mark下。
看过许多有关母函数的资料,介绍母函数的思想基本一样,我这里就通俗理解为:母函数就是一个多项式前面的系数的一个整体的集合,而子函数就是这个多项式每一项前面的系数。
母函数(生成函数)主要为两种,普通型以及指数型。
普通型的一般求解就是模拟多项式系数求解。
而指数型一般数量级很大,需要通过级数化简。比较坑,要有不错的高数功底。
普通型的可以用在求解组合以及整数拆分的题型中。
例如,对于有n种物品,如果第i个物品有ki个,我们可以列式n个项相乘 (x^0+x^1+...x^k1)*(x^0+x^1+...x^k2)*...*(x^0+x^1+...x^kn),每一项表示对于第i件物品,可以有(x^0+x^1+...x^ki)中取法,【注意系数都为1,因为同种物品去i件,它的取法是1】多项相乘:因为取m件物品这件事实要分为对n种物品各取分别取1次【0~ki个】, 是组合计数的乘法原理, x^m 的系数是组合成m件物品的所有方案数。
可以说不同的问题,有不同的解法,对于一道可以用母函数来求解的题而言,可能还有比母函数更简洁的方法,因人而异。不一定遇到组合类型的题型就要用组合函数,在这里我只是要通过一些例子来说明如果我们需要用母函数来求解,那么,该如何选定合适的母函数呢?
HDU 2082 找单词 http://acm.hdu.edu.cn/showproblem.php?pid=2082
Problem Description
假设有x1个字母A, x2个字母B,….. x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,….. 字母Z的价值为26。那么,对于给定的字母,可以找到多少价值<=50的单词呢?单词的价值就是组成一个单词的所有字母的价值之和,比如,单词ACM的价值是1+3+14=18,单词HDU的价值是8+4+21=33。(组成的单词与排列顺序无关,比如ACM与CMA认为是同一个单词)。
Input
输入首先是一个整数N,代表测试实例的个数。
然后包括N行数据,每行包括26个<=20的整数x1,x2,…..x26.
Output
对于每个测试实例,请输出能找到的总价值<=50的单词数,每个实例的输出占一行。
Sample Input
2
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 2 6 2 10 2 2 5 6 1 0 2 7 0 2 2 7 5 10 6 10 2 10 6 1 9
Sample Output
7
379297
#include "stack"
#include "cstdio"
#include "iostream"
#include "cmath"
#include "set"
#include "sstream"
#include "cctype"
#include "string"
#include "cstring"
#include "algorithm"
#include "queue"
#include "map"
using namespace std;
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
const int M=55;
int s[M],a[M],b[M];
int main()
{
int i,n,t,j,k;
cin>>t;
while(t--)
{
for(i=1;i<=26;i++)
{
scanf("%d",&s[i]);
}
memset(a,0,sizeof(a));
for(i=0;i<=s[1];i++)
a[i]=1;//将第一个的系数给a
for(i=2;i<=26;i++)//从第二项开始
{
if(s[i]==0)
continue;
memset(b,0,sizeof(b));
for(k=0;k<=50;k++)//系数从0到50
{
if(a[k]==0)
continue;
for(j=0;j<=s[i];j++)
{
if(k+i*j<=50)//i*j依次表示第i项x的指数
{
b[k+i*j]+=a[k];
}
}
}
for(j=0;j<=50;j++)//将结果给a
a[j]=b[j];
}
int sum=0;
for(i=1;i<=50;i++)//求和
sum+=a[i];
printf("%d\n",sum);
}
return 0;
}
HDU 2079
http://acm.hdu.edu.cn/showproblem.php?pid=2079
选课时间(题目已修改,注意读题)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3156 Accepted Submission(s): 2466
Problem Description
又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合。你来帮帮他吧。(xhd认为一样学分的课没区别)
Input
输入数据的第一行是一个数据T,表示有T组数据。
每组数据的第一行是两个整数n(1 <= n <= 40),k(1 <= k <= 8)。
接着有k行,每行有两个整数a(1 <= a <= 8),b(1 <= b <= 10),表示学分为a的课有b门。
Output
对于每组输入数据,输出一个整数,表示学n个学分的组合数。
Sample Input
2
2 2
1 2
2 1
40 8
1 1
2 2
3 2
4 2
5 8
6 9
7 6
8 8
Sample Output
2
445
代码如下:
#include "stack"
#include "cstdio"
#include "iostream"
#include "cmath"
#include "set"
#include "sstream"
#include "cctype"
#include "string"
#include "cstring"
#include "algorithm"
#include "queue"
#include "map"
using namespace std;
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
const int M = 55;
int c1[700], c2[700];
int a[10], b[10];
int main()
{
int T;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++)
{
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));
int n, kk;
scanf("%d%d", &n, &kk);
int i, j, k;
for (i = 1; i <= kk; i++)
{
scanf("%d%d", &a[i], &b[i]);
}
for (i = 0; i <= a[1] * b[1] && i <= n; i += a[1])
{
c1[i] = 1;
}
for (i = 2; i <= kk; i++)
{
for (j = 0; j <= n; j++)
{
for (k = 0; k <= a[i] * b[i] && k <= n; k += a[i])
{
c2[k + j] += c1[j];
}
}
for (j = 0; j <= n; j++)
{
c1[j] = c2[j];
c2[j] = 0;
}
}
printf("%d\n", c1[n]);
}
return 0;
}
HDU 1171 Big Event in HDU
http://acm.hdu.edu.cn/showproblem.php?pid=1171
背包问题,同样是物品价值和数量。构造普通生成函数
HDU 1028 Ignatius and the Princess III
http://acm.hdu.edu.cn/showproblem.php?pid=1028
整数划分问题,相当于有1,2,3……价值的物品无数。然后便是一样的构造
HDU 1398 Square Coins
http://acm.hdu.edu.cn/showproblem.php?pid=1398
有物品价值1,4,9,16……平方数的物品无数。
根据题意,仔细分析,建立关系。