题目链接:http://poj.org/problem?id=3046
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
Source
题意:蚂蚁计数,输入的t代表的是蚂蚁的种类数,a代表的是蚂蚁的总个数,s和b代表的是一个区间的两端,接下来的a行,每行代表着一种蚂蚁。让我们求什么?就是求取j只蚂蚁的种类数,先举个例子吧
3 5 2 3
1, 1, 2, 2, 3
直接拿样例举例
抓1只蚂蚁有3种: {1} {2} {3}
抓2只蚂蚁有5种: {1,1} {1,2} {1,3} {2,2} {2,3}
抓3只蚂蚁有5种: {1,1,2} {1,1,3} {1,2,2} {1,2,3} {2,2,3}
抓4只蚂蚁有3种: {1,2,2,3} {1,1,2,2} {1,1,2,3}
抓5只蚂蚁有1种: {1,1,2,2,3}
那么[s,b]区间就是[2,3],总共有10种,{1,1}, {1,2} ,{1,3} ,{2,2} ,{2,3} , {1,1,2}, {1,1,3}, {1,2,2}, {1,2,3} ,{2,2,3} 。
题解:就是多重集组合数的裸题,我是看了大神的题解才稍微理解了,在这里附上链接:https://blog.csdn.net/viphong/article/details/48110525
dp[i][j]的意思就是前i种蚂蚁取j只蚂蚁的种类数。
x[i] | i j | 0 | 1 | 2 | 3 | 4 |
2 | 1 | dp[1][0]=1 | dp[1][1]=1 | dp[1][2]=1 | dp[1][3]=0 | dp[1][4]=0 |
2 | 2 | dp[2][0]=1 | dp[2][1]=2 | dp[2][2]=3 | dp[2][3]=2 | dp[2][4]=1 |
1 | 3 | dp[3][0]=1 | dp[3][1]=3 | dp[3][2]=5 | dp[3][3]=8 | dp[3][4]=7 |
x[i]就是第i种蚂蚁的个数,我们可以通过列表格的方式来找这个动态方程,实际上就只有两大类:
第一类:j<x[i]
dp[i][j]=dp[i][j-1]+dp[i-1][j];
第二类: j>=x[i]
dp[i][j]=dp[i][j-1]+dp[i-1][j] - dp[i][j-1-x[i]];
第一类的方程容易发现,在列表格的时候就可以发现他实际上就是左边dp和上边dp的和
第二类,建议大家自己去把1 1 2 2 3 3 3推一下表格就可以推出结论,dp[i][j]=dp[i][j-1]- dp[i][j-1-x[i]]+dp[i-1][j]
可以理解为dp[i][j-1]-dp[i][j-1-x[i]]是一个区间和,然后再加上dp[i-1][j]。
#include<iostream>
using namespace std;
int x[11000];
#define mod 1000000
int dp[1010][100010];
int main()
{
int t,a,q,s,b;
cin>>t>>a>>s>>b;
for(int i=1;i<=a;i++)//输入先将每种蚂蚁出现的次数进行计数。
{
cin>>q;
x[q]++;
}
/*一个都不取的方法只有一种,所以将其赋值为1*/
for(int i=0;i<=t;i++)//for(int i=1;i<=a;i++)
dp[i][0]=1;
for(int i=1;i<=t;i++)
for(int j=1;j<=b;j++)
{
if(j<x[i])
dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
else
dp[i][j]=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-x[i]-1]+mod)%mod;
}
int ans=0;/*将s~b区间的相加,但是一定要注意将ans放到括号里面进行模运算,不然会出错*/
for(int i=s;i<=b;i++)
{
ans=(ans+dp[t][i])%mod;
}
cout<<ans<<endl;
return 0;
}