如何找到所有整数组合,其中最大“顺序”为…

huangapple go评论64阅读模式
英文:

How to find all integer combinations with a max "order"

问题

以下是您要翻译的部分:

问题如下:

考虑一个集合:[k0,k1,......,kn],其中所有元素都是整数。 "顺序" 被定义为 |k0| + |k1| + ... + |kn|。假设最大顺序为 m_max,我如何找到每个 k0、...、kn 的组合,使其顺序小于最大顺序 m_max?

我的尝试如下:

很容易找到每个 ki 的最大范围是从 -m 到 m,因此我可以遍历每个 ki 的这个范围,并检查每个可能的组合。然后,这个问题变成了将一个 (2m + 1)-进制数转换为十进制数的简单问题。

例如:n = 1,m = 2。很容易列出考虑顺序标准的每个可能组合:

  1. (-2, -2, -2) 顺序 = 6 -------(x) -> (0,0,0) -> "5进制" 000
  2. (-1, -2, -2) 顺序 = 5 -------(x) -> (1,0,0) -> "5进制" 001
  3. (0, -2, -2) 顺序 = 4 -------(x) -> (2,0,0) -> "5进制" 002
  4. (1, -2, -2) 顺序 = 5 -------(x) -> (3,0,0) -> "5进制" 003
  5. (2, -2, -2) 顺序 = 6 -------(x) -> (4,0,0) -> 004
  6. (-2, -1, -2) 顺序 = 5 -------(x) -> (0,1,0) -> 010
  7. (-1, -1, -2) 顺序 = 4 -------(x) -> (1,1,0) -> 011

......

  1. (2, 2, 2) 顺序 = 6 ----------(x)

我可以使用一个十进制范围 (0, 125) 进行此遍历。每个测试组合都是从十进制到5进制数的转换。
然而,我发现这种方法计算量相当大。

简而言之,我需要所有可能的组合,它们的绝对值之和受到 m 限制。

英文:

The question is as follows:

Considering a set: [k0, k1, ......, kn] and all the elements are integers. The "order" is defined as |k0| + |k1| + ... + |kn|. Assuming max order m_max, how can I find every combination of k0, ..., kn that has an order less than the max order m_max?

My own attempt is as follows:

It's easy to find that the max range of each ki is from -m to m, so I can just go through this range for every ki and check every possible combination. Then this problem becomes a simple problem that transform an (2m + 1)-nary number to decimal.

For example: n = 1, m = 2. It's easy to list every possible combinations without considering the order criteria:

  1. (-2, -2, -2) order = 6 -------(x) -> (0,0,0) -> "5-nary" 000
  2. (-1, -2, -2) order = 5 -------(x) -> (1,0,0) -> "5-nary" 001
  3. (0, -2, -2) order = 4 -------(x) -> (2,0,0) -> "5-nary" 002
  4. (1, -2, -2) order = 5 -------(x) -> (3,0,0) -> "5-nary" 003
  5. (2, -2, -2) order = 6 -------(x) -> (4,0,0) -> 004
  6. (-2,-1,-2) order = 5 -------(x) -> (0,1,0) -> 010
  7. (-1,-1,-2) order = 4 -------(x) -> (1,1,0) -> 011

......

  1. (2,2,2) order = 6 ----------(x)

I can do this traversal using a decimal range (0, 125). Every test combination is a transformation from the decimal to 5-nary number.
However, I found this method quite computational intensive.

In short, I need all possible combinations that have a sum of absolute values limited by m.

答案1

得分: 2

如何生成具有以下特性的所有组合:

  • n 个元素
  • 每个元素的绝对值限制为 m
  • 元素绝对值之和的限制为 order

首先生成所有不同的有序数字分割,将数字 0.. order 分成 n 个分项,如下所示:

n=4, m=2, order = 4
o=0: [0,0,0,0]
o=1: [0,0,0,1]
o=2: [0,0,0,2], [0,0,1,1] 
o=3: [0,0,0,3], [0,0,1,2], [0,1,1,1]  
o=4: [0,0,0,4], [0,0,1,3], [0,1,1,2], [0,0,2,2]  

编辑:值得改变阶段的顺序以避免重复问题:

对于每个分割,生成排列(不要忘记重复项,next_permutation 算法会正确处理它们)。

[0, 1, 1, 2] => [0, 1, 1, 2], [0, 1, 2, 1], [0, 2, 1, 1], 
                 [1, 0, 1, 2], [1, 0, 2, 1], [1, 1, 0, 2], [1, 1, 2, 0], [1, 2, 0, 1], [1, 2, 1, 0],
                 [2, 0, 1, 1], [2, 1, 0, 1], [2, 1, 1, 0]

然后,对于每个包含 k 个非零元素的排列,生成具有不同符号的 2^k 种最终组合结果。

[0, 1, 2, 1] => [0, 1, 2, 1], [0, 1, 2, -1], [0, 1, -2, 1], [0, 1, -2, -1],
                [0, -1, 2, 1], [0, -1, 2, -1], [0, -1, -2, 1], [0, -1, -2, -1]
英文:

How to generate all combinations with :

  • n elements
  • limit m per absolute value of element
  • limit order for sum of absolute values of elements

At first generate all distinct ordered partitions of numbers 0.. order into n summands like this:

n=4, m=2, order = 4
o=0: [0,0,0,0]
o=1: [0,0,0,1]
o=2  [0,0,0,2], [0,0,1,1] 
o=3  [0,0,0,3], [0,0,1,2], [0,1,1,1]  
o=4  [0,0,0,4], [0,0,1,3], [0,1,1,2],[0,0,2,2]  

Edit: It is worth to change order of stages to avoid duplicates problems:

For every parition generate permutations (don't forget about repeats, next_permutation algorithm does treat them properly).

[0, 1, 1, 2] =>  [0, 1, 1, 2], [0, 1, 2, 1], [0, 2, 1, 1], 
                 [1,0,1,2], [1,0,2,1], [1,1,0,2], [1,1,2,0], [1,2,0,1], [1,2,1,0],
                 [2, 0, 1, 1], [2, 1, 0, 1], [2, 1, 1, 0]

Then for every permutation containing k non-zero elements, make 2^k final combination results with distinct signs

[0,1,2,1] => [0,1,2,1],[0,1,2,-1],[0,1,-2,1],[0,1,-2,-1],
             [0,-1,2,1],[0,-1,2,-1],[0,-1,-2,1],[0,-1,-2,-1]

答案2

得分: 1

感谢MBo澄清问题...

你所要求的操作可能会生成大量输出,所以我对它是否是实现你更大目标的最佳方式表示怀疑。

不过,我知道一种有趣的方法来做。下面是一个带有Python注释的实现:

def print_combos(N, m, order):
    remaining = order
    elements = []
    while True:
        # 用字典序最小的值填充数组的其余部分
        while len(elements) < N:
            # 请注意,这是负数或零
            nextval = max(-remaining, -m)
            elements.append(nextval)
            remaining += nextval

        print(elements)

        # 找到最右边可以增加的元素
        while len(elements) > 0:
            val = elements.pop()
            remaining += abs(val)
            if val < 0 or (val < m and remaining > val):
                elements.append(val + 1)
                remaining -= abs(val + 1)
                break

        if len(elements) < 1:
            # 完成
            break

print_combos(3, 3, 6)
英文:

Thanks to MBo for clarifying the question...

What you're asking to do can generate a lot of output, so I'm doubtful that it's the best way to accomplish whatever your larger purpose is.

I know a fun way to do it, though. Here's an implementation with comments in python:

def print_combos(N,m,order):
    remaining = order
    elements=[]
    while True:
        # Fill remainder of the array with lexically least
        # valid sequence
        while len(elements) &lt; N:
            # note this is negative or 0
            nextval = max(-remaining, -m)
            elements.append(nextval)
            remaining += nextval
        
        print(elements)
        
        # find the right-most element that we can increment
        while len(elements) &gt; 0:
            val = elements.pop()
            remaining += abs(val)
            if val &lt; 0 or (val &lt; m and remaining &gt; val):
                elements.append(val+1)
                remaining -= abs(val+1)
                break
        
        if len(elements) &lt; 1:
            # all done
            break

print_combos(3, 3, 6)

huangapple
  • 本文由 发表于 2023年7月10日 22:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定