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. PHP获取解析URL方法

    们要经常对url里面所带的参数进行解析,如果我们知道了url传递参数名称,例如: /index.php?name=tank&sex=1#top 我们就可以通过$_GET['name'],$_G ...

  2. android139 360 黑名单 增删改查-数据库操作

    BlackNumberOpenHelper.java package com.itheima52.mobilesafe.db.dao; import android.content.Context; ...

  3. MYSQL内存--------启动mysql缓存机制,实现命中率100% 转

    虽然这个标题夸张得过了头,但此文很完整,值得学习.转自 http://www.yy520.net/read.php?278 myql优化,启动MySQL缓存机制,实现命中率100% 配置你的mysql ...

  4. Advice on improving your programming skills

    Programming is cool. But behind the scenes it's also difficult for many people. Many people are defe ...

  5. myEclipse6.5与数据库(SQL Server2008)连接遇到的问题(自己总结的干货)<用SSH框架的时候,用servlet+javabean+jsp的时候>

    昨天因为学习SSH框架的搭建,时隔一年又重新遇到了myEclipse连接数据库的问题.废话不多说,上干货 (以下全部按照我遇到的问题的顺序,也就是没有顺序,就是任性) 请注意:这是在myEclipse ...

  6. Debian 7 安装 Python3.4

    Debian 7 自带的python是2.7.3,要用最新的3.4版本怎么办?我们从官网下载压缩包自己编译. 一.安装编译用的包 $ sudo apt-get install build-essent ...

  7. C++中枚举定义运算符

    由于枚举也是用户定义类型,所以是可以定义运算符, 如: enum Day {sun, mon, tue, wen, thu, fri, sat}; Day& operator++(Day&am ...

  8. Linux下VirtualBox出现kernel driver not installed的解决方法

    今天安装好rhel-server-6.6-i386后,再安装VirtualBox成功,但是再VirtualBox中创建虚拟机的时候出现了“不能为xx虚拟机打开新任务” 并弹出如下的错误信息:

  9. 回溯(UVA129)

    POINT: 如何判断是否包含连续重复子串? 判断 当前串 的 后缀 啦~~~ You have been employed by the organisers of a Super Krypton ...

  10. Android中定义接口的方法

    1.接口方法用于回调(这里定义接口是为了使用其接口方法): public interface ICallback { public void func(); } public class Caller ...