From 2cd2040d4f2171bdc8ced2a36590432964636d8a Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Sat, 7 May 2016 20:16:01 +0800 Subject: [PATCH] Create 1159_MyCodeBattle.cpp From https://vijos.org/p/1159/solution --- VIJOS/1159_MyCodeBattle.cpp | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 VIJOS/1159_MyCodeBattle.cpp diff --git a/VIJOS/1159_MyCodeBattle.cpp b/VIJOS/1159_MyCodeBattle.cpp new file mode 100644 index 0000000..0ab6566 --- /dev/null +++ b/VIJOS/1159_MyCodeBattle.cpp @@ -0,0 +1,57 @@ +#include +#define LL long long +using namespace std; +const int VMAXN = 2e4 + 10; +const int TMAXN = 100 + 10; +const int INF = 0x3f3f3f3f; + +int num[TMAXN], dp[VMAXN], k, n, m; +int ans[TMAXN]; + +bool Check(int sum) +{ + int &cur = dp[sum]; + if (cur != -1) + return cur; + if (sum == 0) + return cur = 1; + for (int i = 0; i < k; i++) + if (sum >= ans[i] && Check(sum - ans[i])) + return cur = 1; + return cur = 0; +} + +void DFSID(int cur, int dep) +{ + if (dep == k) + { + memset(dp, -1, sizeof dp); + if (Check(n)) + { + printf("%d", k); + for (int i = 0; i < dep; i++) + printf(" %d", ans[i]); + puts(""); + exit(0); + } + return; + } + if (cur >= m) + return; + ans[dep] = num[cur]; + DFSID(cur + 1, dep + 1); + DFSID(cur + 1, dep); +} + +int main() +{ + //freopen("input.txt", "r", stdin); + int i, j; + scanf("%d%d", &n, &m); + for (i = 0; i < m; i++) + scanf("%d", &num[i]); + sort(num, num + m); + for (k = 1; k <= m; k++) + DFSID(0, 0); + return 0; +}