poj3046生成函数 dp

Ant Counting
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4862 Accepted: 1852

Description

Bessie was poking around the ant hill one day watching the ants march to and fro while gathering food. She realized that many of the ants were siblings, indistinguishable from one another. She also realized the sometimes only one ant would go for food, sometimes a few, and sometimes all of them. This made for a large number of different sets of ants! 

Being a bit mathematical, Bessie started wondering. Bessie noted that the hive has T (1 <= T <= 1,000) families of ants which she labeled 1..T (A ants altogether). Each family had some number Ni (1 <= Ni <= 100) of ants. 

How many groups of sizes S, S+1, ..., B (1 <= S <= B <= A) can be formed? 

While observing one group, the set of three ant families was seen as {1, 1, 2, 2, 3}, though rarely in that order. The possible sets of marching ants were: 

3 sets with 1 ant: {1} {2} {3} 
5 sets with 2 ants: {1,1} {1,2} {1,3} {2,2} {2,3} 
5 sets with 3 ants: {1,1,2} {1,1,3} {1,2,2} {1,2,3} {2,2,3} 
3 sets with 4 ants: {1,2,2,3} {1,1,2,2} {1,1,2,3} 
1 set with 5 ants: {1,1,2,2,3} 

Your job is to count the number of possible sets of ants given the data above. 

Input

* Line 1: 4 space-separated integers: T, A, S, and B 

* Lines 2..A+1: Each line contains a single integer that is an ant type present in the hive

Output

* Line 1: The number of sets of size S..B (inclusive) that can be created. A set like {1,2} is the same as the set {2,1} and should not be double-counted. Print only the LAST SIX DIGITS of this number, with no leading zeroes or spaces.

Sample Input

3 5 2 3
1
2
2
1
3

Sample Output

10

Hint

INPUT DETAILS: 

Three types of ants (1..3); 5 ants altogether. How many sets of size 2 or size 3 can be made? 


OUTPUT DETAILS: 

5 sets of ants with two members; 5 more sets of ants with three members




题目意思就是:有那么多个蚂蚁家庭,家庭里面有那么多的蚂蚁,然后通过这些蚂蚁的组合可以得到多少组合

3 sets with 1 ant: {1} {2} {3} 
5 sets with 2 ants: {1,1} {1,2} {1,3} {2,2} {2,3} 
5 sets with 3 ants: {1,1,2} {1,1,3} {1,2,2} {1,2,3} {2,2,3} 
3 sets with 4 ants: {1,2,2,3} {1,1,2,2} {1,1,2,3} 
1 set with 5 ants: {1,1,2,2,3} 

就是从S  到  B区间一共有多少组合;

当为三的时候  上叙里面显示为5sets;然后依次类推


题目需要知识就是组合数学里面的生成函数


下面是别人的话

利用生成函数的性质,可以把问题化为求多项式
(1+x+x^2+...+x^N1)(1+x+...+x^N2)(1+x+...+x^N3)...(1+x+...+x^NT)中次数为S,S+1,...B的项的系数之和,
我补充一点就是N1,N2,,,指的是每一个蚂蚁家庭里面的数目


然后通过组合数学的生成函数就可以理解以下代码的了


推荐一篇文章生成函数的文章

http://www.matrix67.com/blog/archives/120




#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

const int MAX=1010;
int a[MAX];
int T,A,S,B;

const int maxn=100005;
const int mod=1000000;
int s1[maxn],s2[maxn];


void deal()
{
    memset(s1,0,sizeof(s1[0])*(B+1));
    memset(s2,0,sizeof(s2[0])*(B+1));

    s1[0]=1;
    for(int i=1;i<=T;i++){
        for(int j=0;j<=B;j++)
            for(int k=0;k<=a[i]&&(k+j)<=B;k++)
                s2[j+k]=(s2[j+k]+s1[j])%mod;

        for(int j=0;j<=B;j++){
            s1[j]=s2[j];
            s2[j]=0;
        }
    }

    int sum=0;
    for(int i=S;i<=B;i++)
        sum=(sum+s1[i])%mod;
    printf("%d\n",sum);

}

int main()
{
    while(scanf("%d%d%d%d",&T,&A,&S,&B)!=EOF)
    {
        memset(a,0,sizeof(a[0])*(T+1));
        int temp;
        for(int i=0;i<A;i++){
            scanf("%d",&temp);
            a[temp]++;
        }

        deal();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值