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 Gym 100338B Geometry Problem 计算几何

    Problem B. Geometry ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

  2. gcc中不同namespace中同名class冲突时

    正常情况下,编译器都会报错,提示你有两个候选类,让你明确的选择一个. 比如我的情况,我自己设计了一个类Message, 然后在某个文件里面引用了它.但是我的文件中又引入了mongodb的头文件,非常不 ...

  3. XCode中调整字体大小

    转自:http://www.cnblogs.com/mengshu-lbq/archive/2012/12/24/2830859.html 1)调出Xcode的preference应该是 Comman ...

  4. tar --help

    pengdl@debian:~/test$ mkdir test1 pengdl@debian:~/test$ mkdir test2 pengdl@debian:~/test$ tar -xzf p ...

  5. cocos2d-x 3.0 alpha1 生成Qt qch帮助文档

    Qt的助手挺好用的. 比chm好多了 cocos2d-x使用doxygen生成文档. 默认生成的是html形式, 需要打开浏览器, 这个是比较耗资源吧 可以修改配置, 让doxygen同时输出qch形 ...

  6. 批处理DataTable

    DataTable dt = CreateTable(); SqlConnection conn = new SqlConnection("Data Source=.;Initial Cat ...

  7. CCScale9Sprite的使用

    #include "cocos-ext.h" USING_NS_CC_EXT; //框中需要显示的label CCLabelTTF *label = CCLableTTF::cre ...

  8. Java 自带性能监控工具:监视和管理控制台jconsole的使用

    关于JConsole工具的使用请参见:http://blog.csdn.net/defonds/article/details/45064297

  9. Ext.Slider的试用小记

    最近需要做一个滑动条,动态改变地图动画播放的速度.实在没有自己用js写一个的那个水平,于是找到了Ext.Slider,ExtJS的版本是3.3.0. new Ext.Slider({ id: 'pla ...

  10. 初识CSS3之媒体查询(2015年05月31日)

    一.什么是媒体查询 媒体查询是面向不同设备提供不同样式的一种实现方式,它可以为每种类型的用户提供最佳的体验,也是响应式设计的实现方式. 现今每天都有更多的手机和平板电脑问市.消费者能够拥有可想象到的各 ...