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

// 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>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std;

int n;
int c1[N],c2[N];
int val[N],cnt[N];
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);
    while(cin>>n)   //n种类型的物品
    {
        long long sum=0;
        for(int i=1;i<=n;++i)
        {
            cin>>val[i]>>cnt[i];     //单位价值  数量
            sum+=val[i]*cnt[i];
        }
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        for(int i=0;i<=cnt[1]*val[1];i+=val[1])
        {
            c1[i]=1;
        }
        for(int i=2;i<=n;++i)
        {
            for(int j=0;j<=sum;++j)
            {
                for(int k=0;k<=cnt[i];++k)
                {
                    c2[k*val[i]+j]+=c1[j];      // 不要忘了加号
                }
            }
            for(int j=0;j<=sum;++j)
            {
                c1[j]=c2[j];
                c2[j]=0;
            }
        }
        // ............ 剩下的就根据题目来变形
        //c1[i]:取出一些物品,这些物品价值的和为i的取法有c1[i]种
         for(int i=0;i<=sum;++i)
         {
             printf("%d ",c1[i]);
         }
         puts("");
    }
    return 0;
}

//另:
//每种物品都有无限个
/*
//
//第1种物品的价值为1,有无限个;
//第2种物品的价值为2,有无限个;
//第3种物品的价值为3,有无限个;
//.......
//问取出一些物品的价值总和为n的取法有多少个?
#include <iostream>
using namespace std;

const int _max = 10001;
int c1[_max], c2[_max];
// c1是保存各项质量砝码可以组合的数目
// c2是中间量,保存每一次的情况,维持c1的不变性
int main()
{
    int nNum;
    int i, j, k;
    while(cin >> nNum)    //要组合的目标数
    {
        for(i=0; i<=nNum; ++i)   // ---- ① 首先对c1初始化,由第一个表达式(1+x+x2+..xn)初始化,把质量从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+x2+x4....)里,第j个就是x2*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;
}

*/

  

时间: 2024-10-05 23:26:02

组合数学 - 母函数 + 模板总结的相关文章

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

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

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

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

Square Coins (HDU 1398) ———母函数模板详解

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

HDU1398 Square Coins 【母函数模板】

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

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

(简单母函数模板)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

组合数学 - 母函数的运用 --- 模板题

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15064    Accepted Submission(s): 6750 Problem Description We all know that Bin-Laden is a notorious terrorist, and he h

组合数学 - 母函数的运用 + 模板 --- 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.那么,对于给定的字母,可以找到

组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24002    Accepted Submission(s): 8458 Problem Description Nowadays, we all know that Computer College is the biggest department