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

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 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


Mean:

给你一个n,问你n的拆分有多少种。

analyse:

经典的母函数运用题,不解释。

Time complexity:O(n^3)

Source code:

// Memory   Time
// 1347K     0MS
// by : Snarl_jsb
// 2014-09-18-15.21
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std;
int c1[300],c2[300];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
//    freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
//    freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
    int n;
    while(cin>>n)
    {
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        for(int i=0;i<=n;++i)
        {
            c1[i]=1;
        }
        for(int i=2;i<=n;i++)
        {
            for(int j=0;j<=n;++j)
            {
                for(int k=0;k<=n;k+=i)
                {
                    c2[k+j]+=c1[j];
                }
            }
            for(int j=0;j<=n;++j)
            {
                c1[j]=c2[j];
                c2[j]=0;
            }
        }
        cout<<c1[n]<<endl;
    }
    return 0;
}

  

时间: 2024-07-29 13:48:44

组合数学 - 母函数 + 模板题 : 整数拆分问题的相关文章

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

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

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

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.那么,对于给定的字母,可以找

HDU 2082 母函数模板题

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

组合数学 - 母函数 + 模板总结

// Memory Time // 1347K 0MS // by : Snarl_jsb // 2014-09-19-18.23 #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<stack>

Pollard-Rho大整数拆分模板

随机拆分,简直机智. 关于过程可以看http://wenku.baidu.com/link?url=JPlP8watmyGVDdjgiLpcytC0lazh4Leg3s53WIx1_Pp_Y6DJTC8QkZZqmiDIxvgFePUzFJ1KF1G5xVVAoUZpxdw9GN-S46eVeiJ6Q-zXdei 看完后,觉得随机生成数然后和n计算gcd,可以将随机的次数根号一下.思想很叼. 对于里面说的birthday trick,在执行次数上我怎么看都只能减一半.只是把平均分布,变成了靠近0

组合数学 - 母函数的运用 + 模板 --- hdu : 2082

找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4093    Accepted Submission(s): 2933 Problem Description 假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到

一道解母函数的题

由于自己是第一次做母函数的题,感觉好难,下面的话是写给新手的 题目是这样的: http://acm.hdu.edu.cn/showproblem.php?pid=1028 看完题目之后呢,它的意思大概是这样的: 给一个整数N,让你求N的拆分. 例如: assume N is 4, we can find:  4 = 4;  4 = 3 + 1;  4 = 2 + 2;  4 = 2 + 1 + 1;  4 = 1 + 1 + 1 + 1; 发现有5种拆分方法. 那么编程如何实现呢? 看代码,如下

(简单母函数模板)hdu 1028 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): 14405    Accepted Submission(s): 10142 Problem Description "Well, it seems the first problem is too easy. I will le