poj-3046-dp
阿新 • • 發佈:2018-04-17
real HA mis hat sibling .org november order arc Ant Counting
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
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
f[i][j]表示從前i種元素中挑出j個的方案個數,f[i][j]=SUM{f[i-1][k] | j-tot[i]<=k<=j},註意到這個方程可以用前綴和優化掉一個A,註意判斷j和tot[i]的關系。
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.
Input
* 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
USACO 2005 November Silver 給出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