In how many ways we can pick K elements from set of n elements to form a number X?

青春壹個敷衍的年華 提交于 2019-12-09 02:04:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!