https://codeforces.com/problemset/problem/687/C
Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is ci. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.
Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values x, such that Arya will be able to make x using some subset of coins with the sum k.
Formally, Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum x using these coins.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of coins and the price of the chocolate, respectively.
Next line will contain n integers c1, c2, ..., cn (1 ≤ ci ≤ 500) — the values of Pari's coins.
It's guaranteed that one can make value k using these coins.
Output
First line of the output must contain a single integer q— the number of suitable values x. Then print q integers in ascending order — the values that Arya can make for some subset of coins of Pari that pays for the chocolate.
Examples
Input
6 18 5 6 1 10 12 2
Output
16 0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
Input
3 50 25 25 50
Output
3 0 25 50
C++版本一
1048ms
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
using namespace std;
typedef long long ll;
const int N=500+10;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k;
int a[N];
int dp[N][N];
bool b[N][N][N];
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<=k;i++){
for(int j=0;j<=k;j++){
dp[i][j]=-INF;
}
}
dp[0][0]=0;
for(int i=1;i<=n;i++){
for(int j=k;j>=a[i];j--){
for(int l=k-j;l>=0;l--){
if(dp[j-a[i]][l]!=-INF&&!b[j-a[i]][l][i]&&dp[j][l]<dp[j-a[i]][l]+a[i]){
dp[j][l]=dp[j-a[i]][l]+a[i];
b[j][l][i]=1;
}
if(dp[l][j-a[i]]!=-INF&&!b[l][j-a[i]][i]&&dp[l][j]<dp[l][j-a[i]]+a[i]){
dp[l][j]=dp[l][j-a[i]]+a[i];
b[l][j][i]=1;
}
}
}
}
priority_queue<int,vector<int>,greater<int> >ans;
for(int i=0;i<=k;i++){
if(k==dp[i][k-i]){
ans.push(i);
}
}
int len=ans.size();
cout << len << endl;
for(int i=0;i<len;i++){
t=ans.top();
ans.pop();
cout << t << ((i!=len-1)?" ":"\n");
}
//cout << "Hello world!" << endl;
return 0;
}
C++版本二
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
using namespace std;
typedef long long ll;
const int N=500+10;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k;
int a[N];
int dp[N][N];
bool b[N][N][N];
int ans[N];
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<=k;i++){
for(int j=0;j<=k;j++){
dp[i][j]=-INF;
}
}
dp[0][0]=0;
for(int i=1;i<=n;i++){
for(int j=k;j>=a[i];j--){
for(int l=k-j;l>=0;l--){
if(dp[j-a[i]][l]!=-INF&&!b[j-a[i]][l][i]&&dp[j][l]<dp[j-a[i]][l]+a[i]){
dp[j][l]=dp[j-a[i]][l]+a[i];
b[j][l][i]=1;
}
if(dp[l][j-a[i]]!=-INF&&!b[l][j-a[i]][i]&&dp[l][j]<dp[l][j-a[i]]+a[i]){
dp[l][j]=dp[l][j-a[i]]+a[i];
b[l][j][i]=1;
}
}
}
}
//priority_queue<int,vector<int>,greater<int> >ans;
int cnt=0;
for(int i=0;i<=k;i++){
if(k==dp[i][k-i]){
//ans.push(i);
ans[++cnt]=i;
}
}
//int len=ans.size();
//cout << len << endl;
cout << cnt << endl;
// for(int i=0;i<len;i++){
// t=ans.top();
// ans.pop();
// cout << t << ((i!=len-1)?" ":"\n");
// }
for(int i=1;i<=cnt;i++){
cout << ans[i] << ((i!=cnt)?" ":"\n");
}
//cout << "Hello world!" << endl;
return 0;
}
C++版本三
93ms
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
using namespace std;
typedef long long ll;
const int N=500+10;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k;
int a;
bool dp[N][N];
int ans[N];
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
dp[0][0]=1;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a);
for(int j=k;j>=a;j--){
for(int l=0;l<=k-a;l++){
if(dp[j-a][l]){
dp[j][l]=dp[j][l+a]=1;
}
}
}
}
int cnt=0;
for(int i=0;i<=k;i++){
if(dp[k][i]){
ans[++cnt]=i;
}
}
cout << cnt << endl;
for(int i=1;i<=cnt;i++){
cout << ans[i] << ((i!=cnt)?" ":"\n");
}
//cout << "Hello world!" << endl;
return 0;
}
C++版本四
31ms的大佬代码
背包,DFS