To My Girlfriend
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 346 Accepted Submission(s): 126
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
题意:给你n个数,求:
∑i=1n∑j=1n∑k=1n∑l=1n∑m=1sf(i,j,k,l,m)(i,j,k,laredifferent)
i,j为必选数的序号,k,l为必不选数的序号,m是背包体积。
正常人一开始的思维一开始应该都是直接n^3解决,但是时间复杂度太高,所以会去想怎么优化。我也是一样的,后来我想到了只需要再开两维来表示必不选和必选时间复杂度就变成了n^2*4,虽然思路我是想到了但是因为一个小错误赛时一直a不过去,后来看题解思路也跟我一样,然后对比其他人的代码,几乎都是一样的,为什么自己就过不了呢?额,这个小错误我就不说了心累,下面说一下动态转移方程。
很明显就只有4种情况,选,不选,必选,必不选,所以dp[i][j][k][l]=dp[i-1][j-a[i]][k][l]+dp[i-1][j][k][l]+dp[i-1][j-a[i]][k-1][l]+dp[i-1][j][k][l-1];
剩下还有一个问题就是i和j交换,k和l交换也是可以的所以最后结果要乘4。下面给代码。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<set>
using namespace std;
#define maxn 1005
#define MOD 1000000007
typedef long long LL;
int dp[maxn][maxn][3][3];
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, s;
scanf("%d%d", &n, &s);
int a[maxn];
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
dp[0][0][0][0] = 1;
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] >= 0){
dp[i][j][k][l] += dp[i - 1][j-a[i]][k][l];
dp[i][j][k][l] %= MOD;
if (k){
dp[i][j][k][l] += dp[i - 1][j - a[i]][k - 1][l];
dp[i][j][k][l] %= MOD;
}
}
if (l){
dp[i][j][k][l] += dp[i - 1][j][k][l - 1];
dp[i][j][k][l] %= MOD;
}
/*if (dp[i][j][k][l]) {
cout << i << " " << j << " " << k << " " << l <<" "<< dp[i][j][k][l]<< endl;
}*/
}
}
}
}
LL sum = 0;
for (int i = 1; i <= s; i++) {
sum += dp[n][i][2][2];
sum %= MOD;
}
printf("%lld\n", (sum<<2)%MOD);
}
}