母函数(生成函数)

母函数:

大意:G(x)=a0+a1*x^2+a2*x^3……

对于序列a0,a1,a2,…构造一函数,称函数G(x)是序列a0,a1,a2,…的母函数。

例子:

有1克、2克、3克、4克的砝码各一枚,能称出哪几种重量?每种重量各有几种可能方案? 

考虑用母函数来接吻这个问题:

我们假设x表示砝码,x的指数表示砝码的重量,这样:

1个1克的砝码可以用函数1+x表示,

1个2克的砝码可以用函数1+x2表示,

1个3克的砝码可以用函数1+x3表示,

1个4克的砝码可以用函数1+x4表示,

我们拿1+x2来说,前面已经说过,x表示砝码,x的指数表示重量,即这里就是一个质量为2的砝码,那么前面的1表示什么?1代表重量为2的砝码数量为0个。

 

几种砝码的组合可以称重的情况,可以用以上几个函数的乘积表示:

(1+x)(1+x2)(1+x3)(1+x4)

=(1+x+x2+x3)(1+x3+x4+x7)

=1+x+x2+2x3+2x4+2x5+2x6+2x7+x8+x9+x10 

从上面的函数知道:可称出从1克到10克,系数便是方案数

模板:

#include<iostream>  
usingnamespace std;   
constint _max = 10001;  
//c1是保存各项质量砝码可以组合的数目  
//c2是中间量,保存没一次的情况  
intc1[_max], c2[_max];    
intmain()  
{   //int n,i,j,k;  
    int nNum;  //  
    int i, j, k;  
   
    while(cin >> nNum)  
    {  
       for(i=0; i<=nNum; ++i)  //首先对c1初始化,由第一个表达式(1+x+x^2+..x^n)初始化,把质量从0到n的所有砝码都初始化为1. 
       {  
           c1[i] = 1;  
           c2[i] = 0;  
       }  
       for(i=2; i<=nNum; ++i)   // i从2到n遍历,这里i就是指第i个表达式,上面给出的第二种母函数关系式里,每一个括号括起来的就是一个表达式。     
       {  

           for(j=0; j<=nNum; ++j)  //j 从0到n遍历,这里j就是只一个表达式里第j个变量,比如在第二个表达式里:(1+x^2+x^4....)里,第j个就是x^(2*j). 
              for(k=0; k+j<=nNum; k+=i)// k表示的是第j个指数,所以k每次增i(因为第i个表达式的增量是i) 
              {  
                  c2[j+k] += c1[j];  
              }  
           for(j=0; j<=nNum; ++j)     // 把c2的值赋给c1,而把c2初始化为0,因为c2每次是从一个表达式中开始的           
           {  
              c1[j] = c2[j];  
              c2[j] = 0;  
           }  
       }  
       cout << c1[nNum] << endl;  
    }  
    return 0;  
}

例题:

 

B - Ignatius and the Princess III

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 1028

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

 

题意:给你整数m;求出其能被正整数组成的方案数;(典型的母函数)

code:

#include<stdio.h>
int s[305],sl[305];
int main()
{
    int n,i,j,k;
    for(i=0;i<301;i++)
    {        s[i]=1;        sl[i]=0;
    }
    for(i=2;i<=301;i++)
    {
        for(j=0;j<301;j++)
        {
            for(k=0;k+j<301;k+=i)
            {
                sl[j+k]+=s[j];
            }
        }
        for(j=0;j<301;j++)
        {
            s[j]=sl[j];
            sl[j]=0;
        }
    }
    while(scanf("%d",&n)!=EOF&&n)
    {
        printf("%d\n",s[n]);
    }
    return 0;
}

 

 

 

 

你可能感兴趣的:(母函数(生成函数))