python 苹果放入10个篮子 任意数量_python:获取所有可能的组合,以便根据约束将x个苹果分配给y个篮子...

这是一个基于年轻图表的方法。例如,4个篮子,6个鸡蛋,每个篮子最多3个鸡蛋。如果我们根据篮子的装满程度来订购篮子,我们就会得到年轻的图表。

x x x x x x x x x x x x x x x x

x x x x x x x x x x

x x x x

下面的代码列举了所有可能的年轻图表,并为每个图表列举了所有可能的排列。

同样的逻辑也可以用来计数。

from itertools import product, combinations

from functools import lru_cache

import numpy as np

def enum_ord_part(h, w, n, o=0):

if h == 1:

d = n

for idx in combinations(range(w), d):

idx = np.array(idx, int)

out = np.full(w, o)

out[idx] = o+1

yield out

else:

for d in range((n-1)//h+1, min(w, n) + 1):

for idx, higher in product(combinations(range(w), d),

enum_ord_part(h-1, d, n-d, o+1)):

idx = np.array(idx)

out = np.full(w, o)

out[idx] = higher

yield out

def bc(n, k):

if 2*k > n:

k = n-k

return np.prod(np.arange(n-k+1, n+1, dtype='O')) // np.prod(np.arange(1, k+1, dtype='O'))

@lru_cache(None)

def count_ord_part(h, w, n):

if h == 1:

return bc(w, n)

else:

return sum(bc(w, d) * count_ord_part(h-1, d, n-d)

for d in range((n-1)//h+1, min(w, n) + 1))

几个例子:

>>> for i, l in enumerate(enum_ord_part(3, 4, 6), 1):

... print(l, end=' ' if i % 8 else '\n')

...

[3 3 0 0] [3 0 3 0] [3 0 0 3] [0 3 3 0] [0 3 0 3] [0 0 3 3] [3 2 1 0] [2 3 1 0]

[3 1 2 0] [2 1 3 0] [1 3 2 0] [1 2 3 0] [2 2 2 0] [3 2 0 1] [2 3 0 1] [3 1 0 2]

[2 1 0 3] [1 3 0 2] [1 2 0 3] [2 2 0 2] [3 0 2 1] [2 0 3 1] [3 0 1 2] [2 0 1 3]

[1 0 3 2] [1 0 2 3] [2 0 2 2] [0 3 2 1] [0 2 3 1] [0 3 1 2] [0 2 1 3] [0 1 3 2]

[0 1 2 3] [0 2 2 2] [3 1 1 1] [1 3 1 1] [1 1 3 1] [1 1 1 3] [2 2 1 1] [2 1 2 1]

[2 1 1 2] [1 2 2 1] [1 2 1 2] [1 1 2 2]

>>>

>>> print(f'{count_ord_part(2, 26, 30):,}')

154,135,675,070

>>> print(f'{count_ord_part(50, 30, 1000):,}')

63,731,848,167,716,295,344,627,252,024,129,873,636,437,590,711

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值