Title Link: HDU 5800
Surface:
To My girlfriend Time limit:2000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 737 Accepted Submission (s): 292
Problem Description Dear Guo
I never forget the moment I met with you. You carefully asked me: "I had a very difficult problem. Can you teach me? ". I replied with a smile, "of course". " I have n items, their weight is 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 were m in total and have 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,..., a (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 multi-university Training Contest 6
Test instructions
It is difficult to understand the test instructions under the Spit Groove. Is the number of the given n, where several numbers are selected, the weights of the several numbers and m, and those numbers are not labeled as i,j, there are the number of sets labeled K,l.
Solving:
DP[I][J][S1][S2], represents the first I items, the total weight is J, there are S1 required, S2 must not choose the number of programs. So for the current state, it has four transition states.
1. Select the current, increase the weight, increase the required number.
2. Select the current, increase the weight, do not increase the number of required.
3. Do not select the current, do not increase the weight, increase the number of unnecessary selection.
4. Do not select the current, do not increase the weight, do not increase the number of required.
Because i,j can be interchangeable, l,k can also be interchangeable, so the final plan number multiplied by 4 is the request.
Code:
#include <iostream> #include <iomanip> #include <cstdio> #include <algorithm> #include <
cstring> #define MOD 1000000007 #define LL long Long #define P (a) printf ("%d\n", a) using namespace Std;
unsigned dp[1005][1005][3][3];
int a[1005];
int main () {int t,n,s,tmp;
LL ans;
scanf ("%d", &t);
while (t--) {ans=0;
scanf ("%d%d", &n,&s);
for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
Memset (Dp,0,sizeof (DP));
Dp[1][a[1]][0][0]=1;
Dp[1][a[1]][1][0]=1;
Dp[1][0][0][0]=1;
Dp[1][0][0][1]=1;
for (int. i=2;i<=n;i++) {for (int j=0;j<=s;j++) {tmp=j+a[i]; if (tmp<=s) {for (int k=0;k<=2;k++) {dp[i][tmp][0][k]= (Dp[i][tmp][0][k]+dp[i-1][j][0][k]
)%mod;
Dp[i][tmp][1][k]= (Dp[i][tmp][1][k]+dp[i-1][j][0][k])%mod;
Dp[i][tmp][1][k]= (Dp[i][tmp][1][k]+dp[i-1][j][1][k])%mod;
Dp[i][tmp][2][k]= (Dp[i][tmp][2][k]+dp[i-1][j][1][k])%mod; dp[i][tmp][2][k]= (Dp[i][tmp][2][k]+dp[i-1][j][2][k])%mod;
}} for (int k=0;k<=2;k++) {dp[i][j][k][0]= (dp[i][j][k][0]+dp[i-1][j][k][0])%mod;
Dp[i][j][k][1]= (dp[i][j][k][1]+dp[i-1][j][k][0])%mod;
Dp[i][j][k][1]= (dp[i][j][k][1]+dp[i-1][j][k][1])%mod;
Dp[i][j][k][2]= (dp[i][j][k][2]+dp[i-1][j][k][1])%mod;
Dp[i][j][k][2]= (dp[i][j][k][2]+dp[i-1][j][k][2])%mod;
}}} for (int i=1;i<=s;i++) ans= (ans+dp[n][i][2][2])%mod;
printf ("%lld\n", ans*4%mod);
} return 0; }