CodeForces 687C The Values You Can Make
来源:互联网 发布:unity3d动画教程 编辑:程序博客网 时间:2024/06/04 20:14
给出n个数和K。 问n个数所有能构成k的子集合中所有的可能的和是多少?
思路:
dp[i][j]表示当前和是i能否构成j。
如果dp[i][j]是可以的话,那么dp[i+m][j]和dp[i+m][j+m]都是可以得!(因为是子集合!!)
最后枚举dp[K][i],把可以得放入ans数组或者vector输出即可!
不过我刚开始不明白dp[0][0]=1的实际意义。意义:空集是其他集合的子集合。
#include<stdio.h>#include<iostream>#include<math.h>#include<string.h>#include<iomanip>#include<stdlib.h>#include<ctype.h>#include<algorithm>#include<deque>#include<functional>#include<iterator>#include<vector>#include<list>#include<map>#include<queue>#include<set>#include<stack>#define CPY(A, B) memcpy(A, B, sizeof(A))typedef long long LL;typedef unsigned long long uLL;const int MOD = int (1e9) + 7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const double EPS = 1e-9;const double OO = 1e20;const double PI = acos (-1.0);const int dx[] = {-1, 0, 1, 0};const int dy[] = {0, 1, 0, -1};using namespace std;bool dp[505][505];vector<int>ans;int main() { int n,K; while (cin>>n>>K) { ans.clear(); memset (dp,0,sizeof (dp) ); dp[0][0]=1; for (int i=0; i<n; i++) { int m; cin>>m; for (int j=K; j>=m; j--) { for (int k=0; k+m<=K; ++k) { if (dp[j-m][k]) { dp[j][k]=dp[j][k+m]=true; } } } } for (int i=0; i<=K; i++) {if (dp[K][i]) { ans.push_back (i); }} sort (ans.begin(),ans.end() ); int l=ans.size(); cout<<l<<endl; for (int i=0; i<l; ++i) { if (i) { printf (" "); } cout<<ans[i]; } puts (""); } return 0;}
0 0
- Codeforces 687C The Values You Can Make(背包)
- Codeforces-687C The Values You Can Make
- CodeForces 687C The Values You Can Make
- CodeForces 687C The Values You Can Make
- CodeForces 687C The Values You Can Make
- Codeforces 687C The Values You Can Make DP
- 687C: The values you can make
- Codeforces 689 C The Values You Can Make(dp)
- The Values You Can Make CodeForces
- [DP] Codeforces 687C #360 (Div. 1) C. The Values You Can Make
- 7_6_M题 The Values You Can Make题解[Codeforces 687C](DP)
- CodeForces 687C - The Values You Can Make(01背包dp)
- codeforces 687C - The Values You Can Make(背包+滚动数组)
- The Values You Can Make
- The Values You Can Make
- codeforces 688 E. The Values You Can Make
- 【43.75%】【codeforces 688E】The Values You Can Make
- Codeforces 688E The Values You Can Make【Dp】
- T3单据记账失败
- VSFlex8n.ocx控件过期
- T1商品无法增加下级分类
- 将DataTable中执行Select(\"条件\")后的结果显示在DataGridView
- 借助ComboBox实现DataGridView列下拉选择效果
- CodeForces 687C The Values You Can Make
- U8存货核算常用存储过程
- Vi详细用法
- T3提示登陆失败
- Cadence基础知识8(3W原则的实质以及20H原则最强整理)
- SQL 2008 更改表结构后提示启用了\"阻止保存要求重新创建表的更改\"选项
- win7/8/8.1/2008等系统打开Chm文件补丁
- springmvc sends and receives data by ajax request using json format
- 工资发放签名表 项目混乱