HDU 5800 To My Girlfriend (动态规划)
来源:互联网 发布:linux认证工程师 编辑:程序博客网 时间:2024/06/03 21:33
题目链接:HDU 5800
题面:
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 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 modulo109+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
题意:
吐槽下题意,很难懂。求的是,给定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 <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;}
0 0
- HDU 5800 To My Girlfriend (动态规划)
- HDU 5800 To My Girlfriend【动态规划】
- HDU 5800 To My Girlfriend
- hdu 5800 To My Girlfriend
- To My Girlfriend HDU
- Problem To My Girlfriend (HDU 5800)
- HDU-5800-To My Girlfriend-DP
- hdu 5800 To My Girlfriend (dp)
- HDU 5800 To My Girlfriend(dp)
- HDU 5800 To My Girlfriend(dp)
- HDU 5800 To My Girlfriend 【DP】
- HDU 5800 To My Girlfriend 【DP】
- HDU 5800 To My Girlfriend(DP)
- [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+乱搞+总结
- hdu 2841 Visible Trees 容斥,分解质因数
- SharedPreferences存储
- Three.js学习笔记---我和小伙伴都惊呆了
- PHP位运算符详解
- 向量基础知识
- HDU 5800 To My Girlfriend (动态规划)
- Ajax 异步加载
- Java Web 代码调试
- css 重置样式表
- Android Studio的国际化
- HDU 1203 I NEED A OFFER! (背包)
- Spring中jdbcTemplate的用法实例
- vijos P1443 银河英雄传说
- React-BootStrap框架快速体验上手