我需要一個算法來解決這個問題:總和組合列表
給定一組n個自然數x1,x2,...,xn,數字S和k。形成從集合中挑選的k個數字的總和(一個數字可以選擇多次),總和爲S.
換句話說:列出S的所有可能的組合:邊界:n < = 256,x < = 1000,k < = 32
例如
problem instance: {1,2,5,9,11,12,14,15}, S=30, k=3
There are 4 possible combinations
S=1+14+15, 2+14+14, 5+11+15, 9+9+12.
有了這些界限,使用暴力是不可行的,但我認爲動態規劃是一種好方法。該方案是:表t,其中t [m,v] =由m個數形成的總和v的組合數。
1. Initialize t[1,x(i)], for every i.
2. Then use formula t[m,v]=Sum(t[m-1,v-x(i)], every i satisfied v-x(i)>0), 2<=m<=k.
3. After obtaining t[k,S], I can trace back to find all the combinations.
的困境是,T [M,V]可通過重複的可交換的組合增加例如,T [2,16] = 2由於16 = 15 + 1和1 + 15。此外,由於1 + 14 + 15,1 + 15 + 14,...,2 + 14 + 14,14 + 2 + 14,最終結果f [3,30]很大,...
如何擺脫對稱排列?提前致謝。
看起來像一個揹包問題(HTTP ://en.wikipedia.org/wiki/Knapsack_problem) – demas
特別是,看起來像整數分區問題http://en.wikipedia.org/wiki/Partition_%28number_theory%29沒有數字k – user1704182