HDU 5800 To My Girlfriend DP
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
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
/** @Date : 2016-11-09-17.37
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#define LL long long
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+2000;
const int mod = 1e9 + 7;
int a[1010];
int dp[1010][1010][3][3];
LL solve(int &n, int &s)
{
MMF(dp);
dp[0][0][0][0] = 1;
//dp[1][0][0][0] = 1;
LL ans = 0;//返回值要用LL...
for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= s; j++)
{
for(int k = 0; k <= 2; k++)
{
for(int l = 0; l <= 2; l++)
{
dp[i][j][k][l] += dp[i-1][j][k][l];
dp[i][j][k][l] %= mod;
if(j >= a[i])
{
dp[i][j][k][l] += dp[i-1][j-a[i]][k][l];
dp[i][j][k][l] %= mod;
}
if(k > 0)
{
if(j >= a[i])
dp[i][j][k][l] += dp[i-1][j - a[i]][k-1][l];
dp[i][j][k][l] %= mod;
}
if(l > 0)
{
dp[i][j][k][l] += dp[i-1][j][k][l-1];
dp[i][j][k][l] %= mod;
}
}
}
if(i == n)
{
ans += dp[i][j][2][2];
ans %= mod;
}
}
}
return (ans * 4 + mod) % mod;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, s;
scanf("%d%d", &n, &s);
for(int i = 1; i <= n; i++)
scanf("%d", a + i);
printf("%lld\n", solve(n, s));//输出LL型?
}
return 0;
}