[poj3046][Ant counting数蚂蚁]
题目链接
http://noi.openjudge.cn/ch0206/9289/
描述
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.
输入* 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
输出* 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.
样例输入3 5 2 3
1
2
2
1
3样例输出
10
英文题目也是真的难懂,理解了很久,其实题目意思挺简单的,就是给你s个t种蚂蚁,每种蚂蚁有一定的数量,然后去求用这些种蚂蚁组成s——b之间长的,情况数有多少。
仔细理解的话可以发现,我们可以把这个看成背包问题,对于这t种蚂蚁,如果其中一组数量为k,那么就有k+1种情况,既不选这种蚂蚁,选一个,选两个……选k个。
那么我们就套用背包模板,先枚举种类,再枚举序列长度,第三层枚举选的个数。每种区间,第三层枚举的选不同个数的情况之和,那么状态转移方程就是
f[i][j]+=f[i-1][j-k] //f[i][j]表示前i个种类中选j长的序列的情况数。
所以当第i种选k个时,那么情况数就是前i-1种选j-k个的情况数,因为然后再枚举k,k去不同数时的情况加起来,就是前i种选j长的序列的情况数。
注意:一定要给f数组整上一个开始的值,不然怎么加都是0;所以我们把前i种选0的情况赋值成1。
最后附上代码
#include<cstdio>
#include<iostream>
using namespace std;
int n,a,s,b,num[],f[][];
int main()
{
scanf("%d%d%d%d",&n,&a,&s,&b);
for(int i=,t;i<=a;++i)
{
scanf("%d",&t);
num[t]++;
}
for(int i=;i<=n;++i)
{
f[i][]=;
}
for(int i=;i<=n;++i)
{
for(int j=;j<=b;++j)
{
for(int k=;k<=num[i];++k)
{
if(j>=k)
f[i][j]=f[i][j]+f[i-][j-k];
}
}
}
int ans=;
for(int i=s;i<=b;++i)
{
ans+=f[n][i];
}
printf("%d",ans);
return ;
}
[poj3046][Ant counting数蚂蚁]的更多相关文章
- BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 56 Solved: 16[S ...
- 1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 85 Solved: 40[S ...
- bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*
bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁&&bzoj1630[Usaco2007 Demo]Ant Counting 题意: t个族群,每个族群有 ...
- bzoj1630 / bzoj2023 [Usaco2005 Nov]Ant Counting 数蚂蚁
Description 有一天,贝茜无聊地坐在蚂蚁洞前看蚂蚁们进进出出地搬运食物.很快贝茜发现有些蚂蚁长得几乎一模一样,于是她认为那些蚂蚁是兄弟,也就是说它们是同一个家族里的成员.她也发现整个 ...
- BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁(dp)
题意 题目描述的很清楚... 有一天,贝茜无聊地坐在蚂蚁洞前看蚂蚁们进进出出地搬运食物.很快贝茜发现有些蚂蚁长得几乎一模一样,于是她认为那些蚂蚁是兄弟,也就是说它们是同一个家族里的成员.她也发现整个 ...
- BZOJ 2023 [Usaco2005 Nov]Ant Counting 数蚂蚁:dp【前缀和优化】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2023 题意: 有n个家族,共m只蚂蚁(n <= 1000, m <= 1000 ...
- 【noi 2.6_9289】Ant Counting 数蚂蚁{Usaco2005 Nov}(DP)
题意:有M个家族的蚂蚁,各Ni只(互相相同).问选出 l~r 只的不同方案数. 解法:很基础的一种DP,不要被"排列组合"所迷惑了啊~我之前接触过这个类型,可惜又忘了,一定要记住! ...
- BZOJ 1630/2023 Ant Counting 数蚂蚁
DP. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- 【bzoj2023/1630】[Usaco2005 Nov]Ant Counting 数蚂蚁 dp
题解: 水题 f[i][j] 前i种用了j个,前缀和优化就可以了
随机推荐
- CSS 边框(border)实例
CSS 边框(border)实例:元素的边框 (border) 是围绕元素内容和内边距的一条或多条线. CSS border 属性允许你规定元素边框的样式.宽度和颜色. CSS 边框属性属性 描述bo ...
- Jmeter(GUI模式)教程
前些天,领导让我做接口的压力测试.What??我从未接触过这方面,什么都不知道,一脸蒙.于是我从学习jmeter开始入手. 现在记录下来jmeter的使用步骤,希望能对大家有所帮助. 一.安装Jmet ...
- JAVA中使用MD5加密实现密码加密
1.新建Md5.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package c ...
- python基础学习笔记(六)
学到这里已经很不耐烦了,前面的数据结构什么的看起来都挺好,但还是没法用它们做什么实际的事. 基本语句的更多用法 使用逗号输出 >>> print 'age:',25 age: 25 ...
- Notes of Daily Scrum Meeting(12.25)
今天在学姐的帮助下,我们终于把网络连接的部分连通了,这对我们是一个很大的鼓舞,也找到了前期 连不通的问题在哪里,这让我们重新有了进行下去的勇气和决心,我们会在最后这几天把前端和后端结合, 做出我们最后 ...
- 20135337——Linux实践三:ELF文件格式(64位系统,简单分析)
ELF文件格式简单分析 (具体分析见上一篇ELF文件格式32位系统) ELF-header 第一行: 457f 464c :魔数: 0201 :64位系统,小端法 01 :文件头版本 剩余默认0: 第 ...
- 使用github的感想
github的仓库链接:https://github.com/liyan941016/test github是一个基于git的代码托管平台,要想使用github第一步就要注册账户,然后要创建一个仓库 ...
- Practice4 阅读《构建之法》6-7章
关于第五章后面的阅读已经在Practice3中有所感悟,下面是6-7章的读书笔记. 第6章 敏捷流程这一章讲了“敏捷流程”这一概念,关于这一名词我是很陌生的,在阅读之后有了一定的理解.敏捷流程是提供了 ...
- python 中一些常用的内置函数
一.常用内置函数 abs(x) 返回绝对值,参数为int float,非字符只能num all(iterable) 如果迭代对象里面的所有值都为真就返回True.all([1, 2, -7]) --- ...
- shell脚本--文件包含
首先介绍一下shell中包含文件的方法,在C,C++,PHP中都是用include来包含文件,Go和Java使用import来包含(导入)包,而在shell中,很简单,只需要一个点“.”,然后跟着文件 ...