#include <stdio.h>
#define maxm 1000
#define maxn 13
int buf[maxn], t, ans[maxm][maxn], tmp[maxn], n, w;
void judge(){
int i, j;
for(i = 0; i < w; ++i){
if(tmp[0] != ans[i][0]) continue;
for(j = 1; j <= tmp[0]; ++j)
if(tmp[j] != ans[i][j]) break;
if(j > tmp[0]) return;
}
for(i = 0; i <= tmp[0]; ++i) ans[w][i] = tmp[i];
++w;
}
void print(){
int i, j;
for(i = 0; i < w; ++i){
printf("%d", ans[i][1]);
for(j = 2; j <= ans[i][0]; ++j)
printf("+%d", ans[i][j]);
printf("\n");
}
}
void dfs(int sum, int p, int c){
int i;
if(sum == 0){
tmp[0] = c-1;
judge();
return;
}
if(sum < 0) return ;
for(i = p; i < n; ++i){
tmp[c] = buf[i];
dfs(sum-buf[i], i+1, c+1);
}
}
int main(){
int i;
while(scanf("%d%d", &t, &n) != EOF){
if(n==0) break;
for(i = 0; i < n; ++i) scanf("%d", &buf[i]);
w = 0;
printf("Sums of %d:\n", t);
dfs(t, 0, 1);
if(w == 0)
printf("NONE\n");
else
print();
}
return 0;
}
poj 1564 / zoj 1711 深搜 隐式图搜索
最新推荐文章于 2021-05-12 17:23:12 发布