[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。

最后附上代码

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 int n,a,s,b,num[1010],f[1010][100010];
 5 int main()
 6 {
 7     scanf("%d%d%d%d",&n,&a,&s,&b);
 8     for(int i=1,t;i<=a;++i)
 9     {
10         scanf("%d",&t);
11         num[t]++;
12     }
13     for(int i=0;i<=n;++i)
14     {
15         f[i][0]=1;
16     }
17     for(int i=1;i<=n;++i)
18     {
19         for(int j=1;j<=b;++j)
20         {
21             for(int k=0;k<=num[i];++k)
22             {
23                 if(j>=k)
24                 f[i][j]=f[i][j]+f[i-1][j-k];
25             }
26         }
27     }
28     int ans=0;
29     for(int i=s;i<=b;++i)
30     {
31         ans+=f[n][i];
32     }
33     printf("%d",ans);
34     return 0;
35 }

 

 

全部评论

相关推荐

用微笑面对困难:你出于礼貌叫了人一声大姐,大姐很欣慰,她真把你当老弟
点赞 评论 收藏
分享
来,说点可能被同行“骂”的大实话。🙊当初接数字马力Offer时,朋友都说:“蚂蚁的“内包”公司?你想清楚啊!”但入职快一年后的今天,我反而对他有了不一样的看法!🔹&nbsp;是偏见?还是信息差!之前没入职之前外面都在说什么岗位低人一等这类。实际上:这种情况不可至否,不能保证每个团队都是其乐融融。但我在的部门以及我了解的周边同事都还是十分好相处的~和蚂蚁师兄师姐之间也经常开一些小玩笑。总之:身份是蚂蚁公司给的,地位是自己挣的(一个傲娇女孩的自述)。🔹&nbsp;待遇?玩的就是真实!试用期工资全额发!六点下班跑得快(早9晚6或者早10晚7,动态打卡),公积金顶格交。别听那些画饼的,到手的钱和下班的时间才是真的(都是牛马何必难为牛马)。🔹&nbsp;能不能学到技术?来了就“后悔”!我们拥有权限直通蚂蚁知识库,技术栈多到学不完。说“学不到东西”的人,来了可能后悔——后悔来晚了(哈哈哈哈,可以不学但是不能没有)!💥&nbsp;内推地址:https://app.mokahr.com/su/ueoyhg❗我的内推码:NTA6Nvs走我的内推,可以直达业务部门,面试流程更快速,进度可查!今天新放HC,之前挂过也能再战!秋招已经正式开始啦~机会就摆在这,敢不敢来试一试呢?(和我一样,做个勇敢的女孩)
下午吃泡馍:数字马力的薪资一般哇,5年经验的java/测试就给人一万出头,而且刚入职第三天就让人出差,而且是出半年
帮你内推|数字马力 校招
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务