if we have n different things and we need to distribute them among m different people then how many ways can we do it such that for each of the m persons there is conditions that:
person 1 can have at least a things and at most b things
person 2 can have at least c things and at most d things
.. and so on ?
e.g if n = 5 and m =3 and the conditions are:
person 1 can receive at least 0 and at most 1 gift
person 2 can receive at least 1 and at most 3 gift
person 3 can receive at least 1 and at most 4 gift
then the number of ways of distributing these 5 gifts is 6((0 1 4), (0 2 3), (0 3 2), (1 1 3), (1 2 2), (1 3 1)).
One way i believe is to iterate through all possible combinations for each range and see which ones sum upto n , but can’t think of an efficient algorithm.
Thanks
You probably want to use a generating function approach. Represent the number of objects that person
igets by the exponents ofx. This means that if personican have at least3and at most7things, this corresponds to the termRemember to think of
+asORand*asAND. If we want to impose conditions and person1and person2, then multiply their functions together. For example, with person1having between3and7things, and say person2has at least5things, and add a third person with at most10things. Then we get:which can also be written as
The way to get information back from this is the following. The coefficient of
x^Min the expansion of these terms gives the number of ways to distribute a total ofMthings among all the people subject to the given constraints.You can work this out from the formulas, or write a program to extract the coefficient, but the idea is to use generating functions as a convenient and efficient way to encode the constraints along with the answer.