题目中提到 It guarantees that the sum of T[i] in each test case is no more than 2000 and 1 <= T[i].
加上一堆顺序,基本可以猜到是属于递推形的dp
dp[i][j]表示前i个和为j时的方案数
dp方程: dp[i][j]+=dp[i][j-k]
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const int INF=0x3f3f3f3f; 11 const double eps=1e-5; 12 typedef long long ll; 13 #define cl(a) memset(a,0,sizeof(a)) 14 #define ts printf("*****\n"); 15 const int MAXN=2005; 16 int n,m,tt; 17 int a[MAXN]; 18 int dp[MAXN][MAXN]; 19 int main() 20 { 21 int i,j,k,ca=1; 22 #ifndef ONLINE_JUDGE 23 freopen("1.in","r",stdin); 24 #endif 25 scanf("%d",&tt); 26 while(tt--) 27 { 28 scanf("%d",&n); 29 int sum=0; 30 for(i=0;i<n;i++) 31 { 32 scanf("%d",a+i); 33 sum+=a[i]; 34 } 35 cl(dp); 36 for(i=0;i<=a[0];i++) 37 { 38 dp[0][i]=1; 39 } 40 for(i=1;i<n;i++) 41 { 42 for(j=0;j<=sum/2;j++) 43 { 44 for(k=0;k<=a[i];k++) 45 { 46 if(j-k<0) break; 47 dp[i][j]+=dp[i-1][j-k]; 48 dp[i][j]%=MOD; 49 } 50 } 51 } 52 printf("%d\n",dp[n-1][sum/2]); 53 } 54 }