【BZOJ1630】[Usaco2007 Demo]Ant Counting

描述 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. // 有三个家庭的 ANT,共五只,分别编号为 1,2,2,1,3,现在将其分为 2 个集合及 3 集合,有多少种分法。

输入格式 InputFormat

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.

输出格式 OutputFormat

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.

样例输入 SampleInput

10 21 7 15
6
1
3
2
3
1
10
6
10
4
7
2
5
1
9
7
4
10
3
8
2

样例输出 SampleOutput

49157

来源 Source

Silver


BZOJ 1630


代码 Code

a[i] 为第 i 种蚂蚁的数量,f[i][j] 表示前 i 种蚂蚁分成 j 只蚂蚁一组的方案数。

\[f[i][j]=\sum_{k=0}^{a[i]}(f[i-1][j-k])\].

空间用滚动数组优化,时间用前缀和优化。

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define mod 1000000
int a[100005],b[100005];
int sum[100005];
int f[2][100005];
int i,j,t,n,m,l,r,k,z,y,x,ans;
int main()
{
    memset(sum,0,sizeof(sum));
    memset(f,0,sizeof(f));
    scanf("%d%d%d%d",&n,&m,&x,&y);
    for (i=1;i<=m;i++) scanf("%d",&z),a[z]++;
    for (i=1;i<=n;i++) b[i]=b[i-1]+a[i];
    f[0][0]=1;
    for (i=1;i<=n;i++)
    {
        sum[0]=f[(i&1)^1][0];
        for (j=1;j<=b[i];j++) sum[j]=(sum[j-1]+f[(i&1)^1][j])%mod;
        for (j=0;j<=b[i];j++) f[i&1][j]=((j<=a[i])?sum[j]:(sum[j]-sum[j-a[i]-1]))%mod;
    }
    ans=0;
    for (i=x;i<=y;i++) ans+=f[n&1][i],ans%=mod;
    printf("%d\n",ans);
    return 0;
}