问题
There is one important point - We can pick any element any number of times but the total picked element should be equal to K.
For example - If set of elements is 1 2 3 5 and K = 3 and X = 4.
Then answer is 1 because there is only one way to pick 3 elements which adds upto 4 and those 3 elements are two 1's and one 2. (1+1+2 = 4)
An algorithm can greatly help. :)
回答1:
Let's consider DP solution for coin change problem. Usually entries of array A with length (Sum+1) contain integers - number of ways to make the value of every cell.
Simple modification - make 2D array A[Sum+1][K]
, so A[M][P]
will contain number of ways to make value M using P coins.
来源:https://stackoverflow.com/questions/37193565/in-how-many-ways-we-can-pick-k-elements-from-set-of-n-elements-to-form-a-number