1. 程式人生 > >POJ 3046 Ant Counting(多重集組合數)

POJ 3046 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. 

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} 。

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;
}