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
在dp的过程中注意取mod,否则溢出。第一次用滚动数组,第二次用前缀和。
第一个AC代码:9892kb 157ms:
#include<stdio.h>
int dp[100001][1001],cal[1001];
int main(){
int t,a,s,b,i,j,k,ans;
const int MOD=1000000;
ans=0;
scanf("%d%d%d%d",&t,&a,&s,&b);
for(i=0;i<a;i++){
scanf("%d",&j);
cal[j]++;
}
dp[0][0]=1;
for(i=0;i<=b;i++){
for(j=1;j<=t;j++){
dp[i][j]=dp[i][j-1];
for(k=1;k<=cal[j];k++){
if(i>=k)
dp[i][j]+=dp[i-k][j-1];
else
break;
}
dp[i][j]%=MOD;
}
if(i>=s)
ans=(ans+dp[i][t]%MOD)%MOD;
//printf("%d %d\n",i,dp[i][t]);
}
printf("%d\n",ans);
return 0;
}
代码如下:
#include<stdio.h>
int dp[100001][2],cal[1001];
int main(){
int t,a,s,b,i,j,k;
const int MOD=1000000;
int ans=0;
scanf("%d%d%d%d",&t,&a,&s,&b);
for(i=0;i<a;i++){
scanf("%d",&j);
cal[j]++;
}
dp[0][0]=dp[0][1]=1;
for(j=1;j<=t;j++){
for(i=1;i<=b;i++){
dp[i][j%2]=dp[i][(j+1)%2];
for(k=1;k<=cal[j]&&k<=i;k++)
dp[i][j%2]+=dp[i-k][(j+1)%2];
dp[i][j%2]%=MOD;
}
}
for(i=s;i<=b;i++)
ans=(ans+dp[i][t%2])%MOD;
printf("%d\n",ans);
return 0;
}
这个是192kb,110ms.
再想,如果把转移优化呢?从O(k)到O(1),我们用前缀和。
include<stdio.h>
#include<stdio.h>
#include<string.h>
int dp[100001][2],cal[1001],preSum[100001][2];
int main(){
int t,a,s,b,i,j,tp;
const int MOD=1000000;
int ans=0;
scanf("%d%d%d%d",&t,&a,&s,&b);
for(i=0;i<a;i++){
scanf("%d",&j);
cal[j]++;
}
dp[0][0]=dp[0][1]=1;
for(i=0;i<=b;i++)/*j=0时,只有选0个为1的方案数*/
preSum[i][0]=preSum[i][1]=1;
for(j=1;j<=t;j++){/*如果用滚动数组j必须在i前面...*/
for(i=1;i<=b;i++){
if(cal[j]>=i)
dp[i][j%2]=(preSum[i][(j+1)%2])%MOD;
else{
tp=(preSum[i][(j+1)%2]-preSum[i-cal[j]-1][(j+1)%2])%MOD;
if(tp<0)
tp+=MOD;
dp[i][j%2]=tp;
}
preSum[i][j%2]=(preSum[i-1][j%2]+dp[i][j%2])%MOD;
}
}
for(i=s;i<=b;i++)
ans=(ans+dp[i][t%2])%MOD;
printf("%d\n",ans);
return 0;
}