HDU5000 Clone(計數dp)

Clone

傳送門1
傳送門2
After eating food from Chernobyl, DRD got a super power: he could clone himself right now! He used this power for several times. He found out that this power was not as perfect as he wanted. For example, some of the cloned objects were tall, while some were short; some of them were fat, and some were thin.

More evidence showed that for two clones A and B , if A was no worse than B in all fields, then B could not survive. More specifically, DRD used a vector v to represent each of his clones. The vector v has n dimensions, representing a clone having N abilities. For the i-th dimension, v[i] is an integer between 0 and T[i] , where 0 is the worst and T[i] is the best. For two clones A and B , whose corresponding vectors were p and q , if for 1<=i<=N , p[i]>=q[i] , then B could not survive.

Now, as DRD’s friend, ATM wants to know how many clones can survive at most.

Input

The first line contains an integer T , denoting the number of the test cases.

For each test case: The first line contains 1 integer N , 1<=N<=2000 . The second line contains N integers indicating T[1],T[2],...,T[N] . It guarantees that the sum of T[i] in each test case is no more than 2000 and 1<=T[i] .

Output

For each test case, output an integer representing the answer MOD 109+7 .

Sample Input

2
1
5
2
8 6

Sample Output

1
7


題意

克隆人有n 個屬性,給出每個屬性的最大值T[i] ;屬性值可以是0T[i] ;
如果A 的所有屬性都不比B 低,那麼B 就會掛,問最多存活多少人。

分析

我們可以發現,如果所有人的屬性值和相同那麼它們都可以存活,因爲每兩個人中要麼所有屬性相同,要麼至少有兩個屬性一高一低,所以它們可以存活。
而且當它們的屬性和恰好爲12ni=1T[i] ,也就相當於每個屬性都取到平均值時會有更多人存活。
定義dp[i][j] 表示前i 種屬性的和爲j 的情況有多少種,則

dp[i][j]=k=0min(T[i],j)dp[i1][jk].

顯邊界條件dp[1][i]=1 .

CODE

#include<cstdio>
#include<memory.h>
#define N 2005
#define P 1000000007
int n,T,sum;
int A[N];
int dp[N][N];
int main() {
    scanf("%d",&T);
    while(T--) {
        sum=0;
        scanf("%d",&n);
        for(int i=1; i<=n; i++) {
            scanf("%d",A+i);
            sum+=A[i];
        }
        sum>>=1;
        memset(dp,0,sizeof dp);
        for(int j=0;j<=A[1];j++)dp[1][j]=1;
        for(int i=2;i<=n;i++)
            for(int j=0;j<=sum;j++)
                for(int k=0;k<=A[i]&&k<=j;k++)
                    dp[i][j]=(dp[i][j]+dp[i-1][j-k])%P;
        printf("%d\n",dp[n][sum]);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章