分配方式的数量
如果我们有 n 种不同的东西,并且需要将它们分配给 m 个不同的人,那么我们可以有多少种方法来做到这一点,以便对于 m 个人中的每一个人都有以下条件: 人 1 至少可以拥有 a 件物品,最多可以拥有 b 件物品 人 2 可以拥有至少 c 件物品,最多 d 件物品 .. 等等 ?
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)).
我相信的一种方法是迭代每个范围的所有可能组合,并查看哪些组合的总和达到 n ,但无法想到有效的算法。 谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想使用生成函数方法。用
x
的指数表示i
获得的对象数量。这意味着如果一个人i
可以拥有至少3
并且最多7
个东西,这对应于术语记住要考虑
+
为OR
,*
为AND
。如果我们想施加条件以及人员1
和人员2
,那么将它们的函数相乘。例如,人员1
拥有3
和7
之间的事物,并假设人员2
至少拥有>5
件事,并添加最多10
件事的第三人。然后我们得到:也可以写成 从中
获取信息的方法如下。这些项展开时的 x^M 系数给出了在给定约束条件下,在所有人之间分配总共
M
种事物的方法数量。您可以从公式中计算出来,或者编写一个程序来提取系数,但其想法是使用生成函数作为一种方便而有效的方法来对约束和答案进行编码。
You probably want to use a generating function approach. Represent the number of objects that person
i
gets by the exponents ofx
. This means that if personi
can have at least3
and at most7
things, this corresponds to the termRemember to think of
+
asOR
and*
asAND
. If we want to impose conditions and person1
and person2
, then multiply their functions together. For example, with person1
having between3
and7
things, and say person2
has at least5
things, and add a third person with at most10
things. Then we get:which can also be written as
The way to get information back from this is the following. The coefficient of
x^M
in the expansion of these terms gives the number of ways to distribute a total ofM
things 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.