2016多校联赛6H (hdu5800) To My Girlfriend
来源:互联网 发布:下载淘宝网到手机桌面 编辑:程序博客网 时间:2024/06/05 00:17
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
∑i=1n∑j=1n∑k=1n∑l=1n∑m=1sf(i,j,k,l,m)(i,j,k,laredifferent)
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
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 integersn , s (4≤n≤1000,1≤s≤1000) . The next line contains n numbers: a1,a2,…,an (1≤ai≤1000) .
Each case contains 2 integers
Output
Each case print the only number — the number of her would modulo 109+7 (both Liao and Guo like the number).
Sample Input
24 41 2 3 44 41 2 3 4
Sample Output
88
Author
UESTC
Source
2016 Multi-University Training Contest 6
Recommend
wange2014 | We have carefully selected several similar problems for you: 5803 5802 5801 5799 5798
题意:给你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 1000000007typedef 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);}}
0 0
- 2016多校联赛6H (hdu5800) To My Girlfriend
- HDU5800-To My Girlfriend
- 2016多校训练Contest6: 1008 To My Girlfriend hdu5800
- hdu5800 To My Girlfriend(DP)
- 【HDU5800】To My Girlfriend(DP + 滚动数组)
- hdu5800 To My Girlfriend(dp)
- To My Girlfriend HDU
- HDU5800 To My Girlfrend(计数dp)
- HDU 5800 To My Girlfriend
- hdu 5800 To My Girlfriend
- hdu 5800 To My Girlfriend(2016 Multi-University Training Contest 6——DP)
- 【HDOJ 5800】To My Girlfriend(DP)
- Problem To My Girlfriend (HDU 5800)
- HDU-5800-To My Girlfriend-DP
- HDU 5800 To My Girlfriend (动态规划)
- hdu 5800 To My Girlfriend (dp)
- HDU 5800 To My Girlfriend(dp)
- HDU 5800 To My Girlfriend(dp)
- Servlet 自动生成验证码
- 和其他一些面向对象的编程语言一样,不是所有的变量(方法)都要通过创建对象来调用,还可以通过给变量(方法)加上static关键字来直接调用。
- CFormView侧栏停靠
- 数据结构实验之栈:行编辑器
- leetcode 32. Longest Valid Parentheses
- 2016多校联赛6H (hdu5800) To My Girlfriend
- 微信红包随机算法初探
- android studio UI 通过material palette配置颜色
- 没事别想不开去创业公司,创业时常是一个人的夜路,胆小者勿入。
- 使用tortoiseGit在github上协同开发项目
- Java.Web学习笔记 Struts2
- 重新组织函数(重构手法一)
- HDU1532 Drainage Ditches 网络流模板
- 【排序】插入排序,希尔排序,选择排序,冒泡排序,堆排序详解及稳定性分析