[英]Find number of combinations of ordered set using multiple summations
This is in regards to a problem for finding a number of combinations of an ordered set, where the elements have constraints. 这是关于寻找其中元素具有约束的有序集合的多个组合的问题。
As an example: 举个例子:
a+b+c+d+e=635, which may be... a + b + c + d + e = 635,可能是...
[0-90] + [1-120] + [50-150] + [20-200] + [30-250] = 635 [0-90] + [1-120] + [50-150] + [20-200] + [30-250] = 635
One solution uses multiple summations, as it was answered in the mathematics stack exchange. 一种解决方案使用多次求和,正如在数学堆栈交换中所回答的那样。
https://math.stackexchange.com/questions/159197/combinatorics-using-constraints-and-ordered-set https://math.stackexchange.com/questions/159197/combinatorics-using-constraints-and-ordered-set
Can someone please give a general idea of the procedure or pseudo-code to solving this type of problem? 有人可以对解决此类问题的过程或伪代码给出一个总体思路吗?
Thank you very much! 非常感谢你!
Look at the solution posted on the math exchange page. 查看数学交换页面上发布的解决方案。 Each sigma sign is a nested for
loop. 每个sigma符号都是嵌套的for
循环。 The inner-most term, x
, is given as an if
. 最里面的项x
作为if
给出。 Your algorithm should therefore be four nested loops around an if. 因此,您的算法应该是围绕if的四个嵌套循环。
A bunch of nested for-loops is the simplest way to do it. 一堆嵌套的for循环是最简单的方法。
Pseudocode: 伪代码:
let combinations = 0;
for a = 0 to 90
for b = max(a+1, 1) to 120
for c = max(b+1, 50) to 150
for d = max(c+1, 20) to 200
let e = 635 - a - b - c - d;
if max(d+1, 50) <= e <= 250
let combinations = combinations + 1
Update 更新资料
The above can be optimised a bit, but you end up with a specific, rather than general, solution. 可以对上面的内容进行一些优化,但最终会得到特定的解决方案,而不是一般的解决方案。
You can observe that (a+1) >= 1
is always true, so we can get rid of the max
call in the assignment to b
. 您可以观察到(a+1) >= 1
始终为true,因此我们可以摆脱对b
的赋值中的max
调用。 Likewise, (c+1) >= 20
is always true, so the assignment to d
can be simplified. 同样, (c+1) >= 20
始终为true,因此可以简化对d
的分配。
You can also see that the maximum possible value of a + b + c + d
is 540, which gives a minimum possible value of 95 for e
. 您还可以看到a + b + c + d
的最大可能值为540,这为e
给出了最小可能值为95。 This is greater than the stated lower bound for e
, so we just have to check that e >= (d+1)
. 这大于e
的规定下限,因此我们只需要检查e >= (d+1)
。
We end up with: 我们最终得到:
let combinations = 0;
for a = 0 to 90
for b = a+1 to 120
for c = max(b+1, 50) to 150
for d = c+1 to 200
let e = 635 - a - b - c - d;
if d+1 <= e <= 250
let combinations = combinations + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.