Ant Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4982   Accepted: 1896

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

题意:有一个蚁巢,里面有T个不同的家族,每个家族有N_i只蚂蚁,共有A只蚂蚁,同家族蚂蚁无区分,从这A只蚂蚁中选取K只蚂蚁组成一个集合(S<=K<=B),问共能组成多少集合。
思路:定义dp[i][j]:从前i个家族取出j只蚂蚁的组合数。那么相当于考虑第i个家族若取出k(k<=min{family[i],j})只蚂蚁,前(i-1)个家族取出(j-k)只蚂蚁即可。
                min{family[i],j}
所以dp[i][j]=∑dp[i-1][j-k] 
                 k=0
节省空间考虑,可以用滚动数组。
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
const int MOD=;
const int T_MAX=,A_MAX=;
int family[T_MAX];
int dp[][A_MAX+];
int main() {
int T,A,S,B;
while (cin >>T>> A>>S>>B) {
memset(family, , sizeof(family));
for (int i = ;i < A;i++) {
int index;
cin >> index;
family[index]++;
}
int total = ;
dp[][] = ;//从0个家族取出0只蚂蚁,只有一种可能
for (int i = ;i <= T;i++) {
total += family[i];
int cur =i& ;
int pre = (i - ) & ;
memset(dp[cur],,sizeof(dp[cur]));//清除上次记录
for (int k = ;k <= family[i];k++) {
for (int j = total;j >= k;j--) {//这j只蚂蚁总数不能超过这几个家族蚂蚁的总数
dp[cur][j] =(dp[cur][j]+ dp[pre][j - k])%MOD;
}
}
}
int cur = T&;
int result=;
for (int i = S;i <= B;i++) {
result =(result+ dp[cur][i])%MOD;
}
cout << result << endl;
memset(dp[(T - ) & ], , sizeof(dp[(T - ) & ]));
}
return ;
}
 
 

poj 3046 Ant Counting的更多相关文章

  1. poj 3046 Ant Counting(多重集组合数)

    Ant Counting Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  2. poj 3046 Ant Counting (DP多重背包变形)

    题目:http://poj.org/problem?id=3046 思路: dp [i] [j] :=前i种 构成个数为j的方法数. #include <cstdio> #include ...

  3. poj 3046 Ant Counting——多重集合的背包

    题目:http://poj.org/problem?id=3046 多重集合的背包问题. 1.式子:考虑dp[ i ][ j ]能从dp[ i-1 ][ k ](max(0 , j - c[ i ] ...

  4. POJ 3046 Ant Counting ( 多重集组合数 && 经典DP )

    题意 : 有 n 种蚂蚁,第 i 种蚂蚁有ai个,一共有 A 个蚂蚁.不同类别的蚂蚁可以相互区分,但同种类别的蚂蚁不能相互区别.从这些蚂蚁中分别取出S,S+1...B个,一共有多少种取法. 分析 :  ...

  5. POJ 3046 Ant Counting DP

    大致题意:给你a个数字,这些数字范围是1到t,每种数字最多100个,求问你这些a个数字进行组合(不包含重复),长度为s到b的集合一共有多少个. 思路:d[i][j]——前i种数字组成长度为j的集合有多 ...

  6. POJ 3046 Ant Counting(递推,和号优化)

    计数类的问题,要求不重复,把每种物品单独考虑. 将和号递推可以把转移优化O(1). f[i = 第i种物品][j = 总数量为j] = 方案数 f[i][j] = sigma{f[i-1][j-k], ...

  7. 【POJ - 3046】Ant Counting(多重集组合数)

    Ant Counting 直接翻译了 Descriptions 贝西有T种蚂蚁共A只,每种蚂蚁有Ni只,同种蚂蚁不能区分,不同种蚂蚁可以区分,记Sum_i为i只蚂蚁构成不同的集合的方案数,问Sum_k ...

  8. BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

    2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 56  Solved: 16[S ...

  9. 1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

    2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 85  Solved: 40[S ...

随机推荐

  1. Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学

    B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...

  2. ResultSet转成java类对象

    在做web开发时遇到一个事情: 需要从mysql数据表中查询数据并遍历查询结果 这样最简单的方式是:查询到结果根据表中字段列表的顺序来一个个获取字段,但这样需要记住字段的顺序,操作起来不是那么方便.因 ...

  3. C# : 操作Word文件的API - (将C# source中的xml注释转换成word文档)

    这篇博客将要讨论的是关于: 如何从C#的source以及注释, 生成一份Word格式的关于各个类,函数以及成员变量的说明文档. 他的大背景如下...... 最近的一个项目使用C#, 分N个模块, 在项 ...

  4. 升级、备份红帽PaaS openshift 上的 wordpress

    红帽提供了一个很稳定的PAAS服务平台:openshift!此博客即作为wordpress建在里面. 这里记录怎样升级与备份wordpress. 预备: 安装 openshift command li ...

  5. sizeof求字节以及与strlen的区别

    例子一: /* *根据以下条件进行计算: *1. 结构体的大小等于结构体内最大成员大小的整数倍 *2. 结构体内的成员的首地址相对于结构体首地址的偏移量是其类型大小的整数倍,比如说double型成员相 ...

  6. JS 操作 radio input(cc问卷管理)

    1.选中特定的单选按钮 function showDetail(content){ $("input[name^='radio']").removeAttr("check ...

  7. Naive Bayes Algorithm

    朴素贝叶斯的核心基础理论就是贝叶斯理论和条件独立性假设,在文本数据分析中应用比较成功.朴素贝叶斯分类器实现起来非常简单,虽然其性能经常会被支持向量机等技术超越,但有时也能发挥出惊人的效果.所以,在将朴 ...

  8. bash的for循环从命令读取值

    bash的for循环可以很方便地从命令读取值,还可以指定分割值 下面的程序可以打印文件的内容,前面加上行号 #!/bin/bash # 打印每一行的内容,前面加行号 filename="/h ...

  9. 关于设置android:imeOptions属性无效的解决办法

    在对Android的EditText控件进行设置时,经常会限定一下输入法的属性,设置右下角为完成或者搜索等,一般都会想到android:imeOptions属性,但是仅仅这么设置通常是无效的,还要搭配 ...

  10. byte[] bytes和string转换

    public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "        {     ...