没有任何优化,纯深搜做法:
#include<iostream>
#include<vector>
using namespace std;const int N = 100010;
int nums[N],selected[N];
int n,M,sum;
bool ans=false;void Out()
{for (int i = 0; i <= n - 1; i++)printf("%d", selected[i]);puts("");
}void dfs(int level=0)
{if (level == n) {if (sum == M) {ans=true;Out();}return;} dfs(level + 1);selected[level] = 1;sum += nums[level];dfs(level + 1);sum -= nums[level];selected[level] = 0;
}int main()
{scanf("%d", &n);scanf("%d", &M);for (int i = 0; i <= n - 1; i++){scanf("%d", &nums[i]);}dfs(0);if(!ans){printf("-1");}return 0;
}