To My Girlfriend
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1397 Accepted Submission(s): 543
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
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) .
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
Recommend
wange2014
题意:给定n个数,其中选定若干数,这若干数的权值和为m,且这些数中没有下标为i,j的数,有下标为k,l的数的集合个数。
解题思路:
dp[i][j][s1][s2],代表的是前i个物品,总权值为j,已有s1个必选,s2必不选的方案数。那么对于当前一个状态,有四种转移状态:
1.选中当前的,增加权值,增加必选个数。
2.选择当前的,增加权值,不增加必选个数。
3.不选中当前的,不增加权值,增加不必选个数。
4.不选中当前的,不增加权值,不增加必选个数。
因为i,j可以互换,l,k也可以互换,故而最后方案数乘以4即为所求
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int dp[1001][1001][3][3];
int n, s, a[1001];
int f(int a, int b)
{
int sum = 0;
while (b)
{
if (b & 1) { sum += a; sum %= mod; }
b >>= 1;
a = (a*2) % mod;
}
return sum;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
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++)
{
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;
}
if (j + a[i] > s) continue;
for (int k = 0; k <= 2; k++)
{
dp[i][j + a[i]][0][k] = (dp[i][j + a[i]][0][k] + dp[i - 1][j][0][k]) % mod;
dp[i][j + a[i]][1][k] = (dp[i][j + a[i]][1][k] + dp[i - 1][j][0][k]) % mod;
dp[i][j + a[i]][1][k] = (dp[i][j + a[i]][1][k] + dp[i - 1][j][1][k]) % mod;
dp[i][j + a[i]][2][k] = (dp[i][j + a[i]][2][k] + dp[i - 1][j][1][k]) % mod;
dp[i][j + a[i]][2][k] = (dp[i][j + a[i]][2][k] + dp[i - 1][j][2][k]) % mod;
}
}
}
int ans = 0;
for (int i = 1; i <= s; i++)
ans = (ans + dp[n][i][2][2]) % mod;
printf("%d\n", f(ans,4));
}
return 0;
}