You have N balls, out of which r1 are of one color, r2 are of another color, r3 are of another color....and so on. There are C colors of balls in total. Balls of same color are identical . You have to choose K balls out of these N balls. What is the best complexity in which this can be done ( not including complexity of precomputation ), and how can we do this?
I know two ways of solve this problem.
Suppose that
C <= 100
,K <= 100
andRi <= 100
. In this case, we can do a simpleO(C * K * R)
Dynamic Programming approach.Suppose now that
C <= 20
,K <= 10^18
andRi <= 10^18
. In this case we can read this problem as count the number of integral solutions to the following equation:X1 + X2 + X3 + ... + Xc = K
, such that0 <= Xi <= Ri
.So we can solve it using the inclusion-exclusion principle. The complexity is
O(C * 2^C)
. You can read more about it here.I hope it helps and sorry for my poor english.
Thanks!
Although I don't care , but still, why did I get downvoted? Is it a crime to ask about things you don't know?
My guess would be lack of specs (ie. What are the constraints of N, C and K?)
and perhaps formatting and spelling mistake.
Pun intended?
Nevertheless, I thought there would be a straight-cut combinatorics formula that runs in O(1) (after some pre-computation that is), which is why constraints didn't seem to matter. But thanks for clarifying.
This is how you find it using direct formula. (Not sure if coding this is easy)
if N = r1 + r2 + r3 +r4 ... rc
ans = coefficient of x^k in (1+x+x^2+...x^r1) * (1+x+x^2+...x^r2) * (1+x+x^2+...x^r3) * ... *(1+x+x^2+...x^rc)
Thanks. But coding this is DP. No O(1) i get for this.
Do you want to choose K balls out of N randomly, or you want to count the number of ways to do it?
Count the no. of ways to do the selection of K balls.
Its simple ...you dumb af
Why you bring up the topic that was closed 2 years ago