Clone HDU - 5000 (Dilworth定理)

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 10^9 + 7.

Sample Input

2
1
5
2
8 6

Sample Output

1
7

转载

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)

const int N=2010;
const int mod=1e9+7;

LL dp[N][2*N];

LL val[N];
int main(){
	
	int T;
	scanf("%d",&T);
	while(T--){
		int n;
		scanf("%d",&n);
		LL sum=0;
		rep(i,1,n+1){
			scanf("%lld",&val[i]);	
			sum+=val[i];
		}
		
		memset(dp,0,sizeof(dp));
		
		dp[0][0]=1;
		
		for(int i=1;i<=n;++i){
			for(int s=0;s<=sum-val[i];++s){//考虑其余n-1个数字可能组成的 sum值 
				for(int j=0;j<=val[i];++j){
					dp[i][s+j]=(dp[i][s+j]+dp[i-1][s])%mod;
				}
			}
			for(int s=sum-val[i]+1;s<=sum;++s)//因为还有前面组成的 sum值在[sum-val[i]+1,sum]范围内,所以还要加上 
			dp[i][s]=(dp[i][s]+dp[i-1][s])%mod;
		}
		
		printf("%lld\n",dp[n][sum/2]);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/82768673
今日推荐