poj-3046-dp
Posted zzq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj-3046-dp相关的知识,希望对你有一定的参考价值。
Ant Counting
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6829 | Accepted: 2514 |
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.
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
* 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
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种元素,每个元素个数为tot[i],询问从所有元素中挑出k个组成的不同集合数目sum[k],k€[S,B] ,ans=SUM{sum[k] | S<=k<=B }
f[i][j]表示从前i种元素中挑出j个的方案个数,f[i][j]=SUM{f[i-1][k] | j-tot[i]<=k<=j},注意到这个方程可以用前缀和优化掉一个A,注意判断j和tot[i]的关系。
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 #define LL long long 6 const LL MOD=1000000; 7 LL f[2][100000+30]; 8 int tot[1010]; 9 int main() 10 { 11 int T,A,S,B; 12 int i,j,k,n,m; 13 while(cin>>T>>A>>S>>B){ 14 memset(tot,0,sizeof(tot)); 15 for(i=1;i<=A;++i){ 16 scanf("%d",&n); 17 tot[n]++; 18 } 19 int cur=0; 20 LL ans=0; 21 f[cur][0]=1; 22 for(i=1;i<=A;++i) f[cur][i]=1; 23 24 for(i=1;i<=T;++i){ 25 cur^=1; 26 f[cur][0]=1; 27 for(j=1;j<=A;++j){ 28 int tt=j-tot[i]-1; 29 if(j<=tot[i]){ 30 f[cur][j]=(f[cur][j-1]+f[cur^1][j])%MOD; 31 } 32 else{ 33 f[cur][j]=(f[cur][j-1]+f[cur^1][j]-f[cur^1][j-1-tot[i]]+MOD)%MOD; 34 } 35 } 36 } 37 ans=(f[cur][B]-f[cur][S-1]+MOD)%MOD; 38 cout<<ans<<endl; 39 } 40 return 0; 41 }
以上是关于poj-3046-dp的主要内容,如果未能解决你的问题,请参考以下文章