Problem Description
Dear Guo
I never forget the moment I met with you.You carefully asked me: “I have a very difficult problem. Can you teach me?”.I replied with a smile, “of course”.”I have n items, their weight was a[i]”,you said,”Let’s define f(i,j,k,l,m) to be the number of the subset of the weight of n items was m in total and has No.i and No.j items without No.k and No.l items.”“And then,” I asked.You said:”I want to know
∑i=1n∑j=1n∑k=1n∑l=1n∑m=1sf(i,j,k,l,m)(i,j,k,laredifferent)
Sincerely yours,
Liao
Input
The first line of input contains an integer T(T≤15) indicating the number of test cases.
Each case contains 2 integers n, s (4≤n≤1000,1≤s≤1000). The next line contains n numbers: a1,a2,…,an (1≤ai≤1000).
Output
Each case print the only number — the number of her would modulo 109+7 (both Liao and Guo like the number).
Sample Input
2
4 4
1 2 3 4
4 4
1 2 3 4
Sample Output
8
8
Author
UESTC
Source
2016 Multi-University Training Contest 6
dp[i][j][s1][s2]代表前个数和为j,s1个必选数。s2个必不选数的方案数。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define LL long long
using namespace std;
const int mod=1000000007;
int n,m;
int dp[1010][1010][3][3];
int a[1010];
void add_mod(int &x, int y) {
x = (x + y) % mod;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
dp[0][0][0][0]=1;
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
for(int s1=0;s1<=2;s1++){ //必选
for(int s2=0;s2<=2;s2++){ //必不选
add_mod(dp[i][j][s1][s2],dp[i-1][j][s1][s2]);
//不拿进来
if(j>=a[i]) add_mod(dp[i][j][s1][s2],dp[i-1][j-a[i]][s1][s2]);
//拿进来,但不是必选和必不选
if(j>=a[i]&&s1-1>=0) add_mod(dp[i][j][s1][s2],dp[i-1][j-a[i]][s1-1][s2]);
//放到必选里去
if(s2-1>=0) add_mod(dp[i][j][s1][s2],dp[i-1][j][s1][s2-1]);
//放到必不选里去
}
}
}
}
LL ans=0;
for(int i=0;i<=m;i++) ans=(ans+dp[n][i][2][2])%mod; //只取走两个必选两个必不选的,前n个数里和为m,2个必选和2必不选
printf("%I64d\n",4*ans%mod);//*4是必选的和比不选的两个数之间有顺序
}
return 0;
}