HDU2082母函数模板题

找单词

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5782    Accepted Submission(s): 4062

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

Source

2006/1/15 ACM程序设计期末考试

知识点:

母函数(生成函数):

生成函数有普通型生成函数和指数型生成函数两种(本题是普通型)。

形式上,普通型母函数用于解决多重集的组合问题,

指数型母函数用于解决多重集的排列问题。

母函数还可以解决递归数列的通项问题(例如使用母函数解决斐波那契数列,Catalan数的通项公式)。

普通母函数:

构造母函数G(x), G(x) = a0 + a1*x + a2* +
a3* +....+
an*, 
则称G(x)是数列a0,a1…an的母函数。

通常普通母函数用来解多重集的组合问题,其思想就是构造一个函数来解决问题,一般过程如下:

1.建立模型:物品n种,每种数量分别为k1,k2,..kn个,每种物品又有一个属性值p1,p2,…pn,(如本题的字母价值),

求属性值和为m的物品组合方法数。(若数量ki无穷
也成立,即对应下面式子中第ki项的指数一直到无穷)

2.构造母函数:G(x)=(1++)(1+++…)…(1+++…)       
(一)

=a0 + a1*x + a2* +
a3* +....+
akk*    
(设kk=k1·p1+k2·p2+…kn·pn)  (二)

G(x)含义: ak 为属性值和为k的组合方法数。

母函数利用的思想:

1.把组合问题的加法法则和幂级数的乘幂对应起来。

2.把离散数列和幂级数对应起来,把离散数列间的相互结合关系对应成为幂级数间的运算关系,最后由幂级数形式来

确定离散数列的构造。

代码实现:

求G(x)时一项一项累乘。先令G=1=(1+0*x+0*+…0*),再令G=G*(1++)得到形式(二)的式子…最后令G=G*(1+++…)。

题解:

1.建模:物品(字母)26种,每种数量x1,x2…x26,属性值为1,2,3..26,求属性值和<=50的组合方法数。

2.G(x)=(1++)(1+++…)…(1++…)

#include <iostream>

#include <algorithm>

#include <stdio.h>

#include <string.h>

using namespace std;

int c1[100],c2[100];

int a[30];

int main()

{

int t;

cin >> t;

while(t --)

{

for(int i = 1; i <= 26; i ++)

cin >> a[i];

memset(c1,0,sizeof(c1));

memset(c2,0,sizeof(c2));

c1[0] = 1;///初始化

for(int i = 1; i <= 26; i ++)///对应26个多项式

{

for(int j = 0; j <= 50; j ++)   ///每个多项式中对应的指数

for(int k = 0; k <= a[i] && k * i + j <= 50; k ++)  ///k*i表示被乘多项式各项的指数

c2[j + k * i] += c1[j];

memcpy(c1,c2,sizeof(c2));///c2数组的值赋值给c1

memset(c2,0,sizeof(c2));///c2初始化

}

///累加

int sum = 0;

for(int i = 1; i <= 50; i ++)

sum += c1[i];

cout << sum << endl;

}

return 0;

}

时间: 2024-08-07 00:06:29

HDU2082母函数模板题的相关文章

HDU1028 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): 12521    Accepted Submission(s): 8838 Problem Description "Well, it seems the first problem is too easy. I will let

组合数学 - 母函数 + 模板题 : 整数拆分问题

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

HDU 2082 母函数模板题

生成函数,英文是Generating Function.恕本人不才,本文只介绍生成函数的其中一种用法. 生成函数是说,构造这么一个多项式函数g(x),使得x的n次方系数为f(n). 对于母函数,我看到最多的是这样两句话: 1.“把组合问题的加法法则和幂级数的乘幂对应起来.” 2.“把离散数列和幂级数一 一对应起来,把离散数列间的相互结合关系对应成为幂级数间的运算关系,最后由幂级数形式来确定离散数列的构造. “ 其实这两句话我也不算太懂.先放这里,说不定以后可能会慢慢理解吧. 还是先举个大牛博客中

组合数学 - 母函数 --- 模板 + 详解

Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8341    Accepted Submission(s): 5674 Problem Description People in Silverland use square coins. Not only they have square shapes but

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>

几道树剖模板题

寒假后半段一直都在外出旅游..颓了好久..qaq 旅游期间写了几道树剖模板题,贴上来.. BZOJ 1036 没啥好说的,裸题 1 #include <cstdio> 2 #include <algorithm> 3 4 #define LEFT (segt[cur].l) 5 #define RIGHT (segt[cur].r) 6 #define MID (segt[cur].mid) 7 #define SUM (segt[cur].Sum) 8 #define MAX (

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

洛谷P3381——费用流模板题

嗯..随便刷了一道费用流的模板题....来练练手. #include<iostream> #include<cstdio> #include<cstring> using namespace std; int h[5210],d[5210],used[5210],que[100010],last[5210]; int k=1,INF=0x7fffffff,ans1=0,ans2=0; inline int read(){ int t=1,num=0; char c=ge