【问题标题】:Combinations: Numbers 1 thru 22, totals of 4 number non-repeating组合:数字 1 到 22,共 4 个数字,不重复
【发布时间】:2012-02-14 17:03:04
【问题描述】:

如果我们在池中抽出 1 到 22 号的彩票。我们必须画四个数字。 4个数字组合的总数为7,315。

现在最困难的部分是,假设您只想保留 4 个中不超过 2 个相同的组合。

例如,你画出组合 1,2,3,4 那么 1,2,3,5 或 2,3,4,6 都不好。下一个有效组合是 1,2,5,6,因为没有三个数字与 1,2,3,4 中的相同。

如何从 1,2,3,4 到 19,20,21,22 中提取所有其他组合中只有两个相同数字的组合。

或者找出有多少有效组合的公式是什么?

以下是前几个有效组合。 (1,2,3,4):(1,2,5,6):(1,2,7,8):(1,2,9,10):(1,2,11,12): (1,2,13,14)...

感谢您提前提供的任何帮助, DJ

【问题讨论】:

  • 不,不是。这与彩票概率有关。
  • 我认为您对组合的估计不正确。如果我们查看每张图纸的组合。 21 x 20 x 19 x 18 = 143,640 不是 7,315
  • 我使用的等式是 (n!)/((r!)(n-r)!) 其中 n=22 和 r=4
  • 嗯...我刚刚去生成了每个唯一的集合,用于 1-22 之间的四位数字,然后我得到了 143,640。我现在正在发布我的代码。
  • 我使用了 MATLAB 函数 combntns(set,subset) mathworks.com/help/toolbox/map/ref/combntns.html

标签: combinations


【解决方案1】:

这是一种相当不雅的蛮力方法。

#!/usr/bin/python

from sets import Set

setlist = []

# Generate all sets
# Sets are guaranteed to have only unique numbers
# Note: Python ranges are inclusive on the left but exclusive on the right
for first in range(1,23):
  for second in range(first+1,23):
    for third in range(second+1,23):
      for fourth in range(third+1,23):
        possible = Set([first, second, third, fourth])
        # Only take sets of 4 digits
        if len(possible) == 4:
          setlist.append(possible)

# Print the total number of combinations
print len(setlist)

answers = []
for aset in setlist:
  # Append the first answer automatically
  if len(answers) == 0:
     answers.append(aset)

  ok = True
  #Check against all our previous answers
  for answer in answers:
    # We have more than two intersecting values, this is not an answer
    if (len(answer.intersection(aset)) > 2):
      ok = False
      break;

  # If our answer is ok
  if ok:
    answers.append(aset)

# Clean up our answers and sort them all
clean = []
for answer in answers:
  temp = list(answer)
  temp.sort()
  clean.append(temp)
# Print the clean answers
print clean


# Print the total
print len(answers)

【讨论】:

    【解决方案2】:

    您正在寻找的东西称为组合块设计。基本集合中的元素数为 22,而每个块中的元素数为 4。此外,块必须具有在多个块中不出现三元组(例如 {1,2,3})的属性,这是一种更准确的说法,即“您只想保留 4 个中只有 2 个相同的组合”。

    要获得块数的上限,首先考虑单个块(例如,{1,2,3,4})。在该块中有四个三元组:{1,2,3}、{1,2,4}、{1,3,4} 和 {2,3,4}。 22个元素的三元组总数为22C3 = 22x21x20/(3x2x1) = 1540。由于每个块包含4个三元组,最大块数为1540/ 4 = 385。这并不是说可以找到 385 个块,但我们稍后会看到。

    请注意,正如另一位发帖人所尝试的那样,通过“贪婪”方法生成设计几乎肯定注定要失败。贪心算法早期的选择会对以后的选择数量产生负面影响,这可能会导致在找到 385 个区块之前陷入僵局。

    如果您找到一个块设计,其中每个三元组恰好出现一次,而不是最多一次,那么您已经找到了一个 3-(22,4,1) 块设计。 3 表示三元组,22 是基本元素的数量,4 是块的大小,1 是每个三元组出现的次数。这种块设计也称为 Steiner 四联系统。

    已经对 Steiner 四重系统进行了一些学术工作。特别是,Hanani, H. “论四重系统”。加拿大。 J.数学。 12, 145-157, 1960 表明,当且仅当元素的数量与 2 或 4 mod 6 一致时,四重系统是可能的。由于 22 与 4 mod 6 一致,因此在您的情况下存在四重系统,所以可以找到完整的 385 个块。

    Hanani 的论文也给出了一个明确的结构,这里描述起来太复杂了,但幸运的是已经在 FOSS Sage 系统中实现了。您可以免费下载 Sage 并运行 print designs.steiner_quadruple_system(22) 以获取 385 个块的列表。请注意,编号从 0 开始,因此如果您希望您的数字从 1 到 22,只需在每个数字上加 1,如果您想要一个包含 {1,2,3,4} 以外的四个连续数字的块,您可以询问重新编号(或者只是通过将 k mod 22 添加到设计中的每个数字来自己做)。

    以下是 Sage 提供的方块:

    blocks=[[0, 1, 2, 3], [0, 1, 4, 5],
    [0, 1, 6, 21], [0, 1, 7, 8], [0, 1, 9, 17], [0, 1, 10, 16], [0, 1, 11,
    19], [0, 1, 12, 18], [0, 1, 13, 20], [0, 1, 14, 15], [0, 2, 4, 6], [0,
    2, 5, 21], [0, 2, 7, 9], [0, 2, 8, 17], [0, 2, 10, 15], [0, 2, 11, 20],
    [0, 2, 12, 19], [0, 2, 13, 18], [0, 2, 14, 16], [0, 3, 4, 21], [0, 3, 5,
    6], [0, 3, 7, 10], [0, 3, 8, 16], [0, 3, 9, 15], [0, 3, 11, 18], [0, 3,
    12, 20], [0, 3, 13, 19], [0, 3, 14, 17], [0, 4, 7, 11], [0, 4, 8, 19],
    [0, 4, 9, 20], [0, 4, 10, 17], [0, 4, 12, 15], [0, 4, 13, 16], [0, 4,
    14, 18], [0, 5, 7, 12], [0, 5, 8, 18], [0, 5, 9, 16], [0, 5, 10, 20],
    [0, 5, 11, 15], [0, 5, 13, 17], [0, 5, 14, 19], [0, 6, 7, 13], [0, 6, 8,
    15], [0, 6, 9, 18], [0, 6, 10, 19], [0, 6, 11, 16], [0, 6, 12, 17], [0,
    6, 14, 20], [0, 7, 14, 21], [0, 7, 15, 20], [0, 7, 16, 19], [0, 7, 17,
    18], [0, 8, 9, 10], [0, 8, 11, 12], [0, 8, 13, 14], [0, 8, 20, 21], [0,
    9, 11, 13], [0, 9, 12, 14], [0, 9, 19, 21], [0, 10, 11, 14], [0, 10, 12,
    13], [0, 10, 18, 21], [0, 11, 17, 21], [0, 12, 16, 21], [0, 13, 15, 21],
    [0, 15, 16, 17], [0, 15, 18, 19], [0, 16, 18, 20], [0, 17, 19, 20], [1,
    2, 4, 21], [1, 2, 5, 6], [1, 2, 7, 17], [1, 2, 8, 9], [1, 2, 10, 14],
    [1, 2, 11, 18], [1, 2, 12, 20], [1, 2, 13, 19], [1, 2, 15, 16], [1, 3,
    4, 6], [1, 3, 5, 21], [1, 3, 7, 16], [1, 3, 8, 10], [1, 3, 9, 14], [1,
    3, 11, 20], [1, 3, 12, 19], [1, 3, 13, 18], [1, 3, 15, 17], [1, 4, 7,
    19], [1, 4, 8, 11], [1, 4, 9, 16], [1, 4, 10, 20], [1, 4, 12, 14], [1,
    4, 13, 17], [1, 4, 15, 18], [1, 5, 7, 18], [1, 5, 8, 12], [1, 5, 9, 20],
    [1, 5, 10, 17], [1, 5, 11, 14], [1, 5, 13, 16], [1, 5, 15, 19], [1, 6,
    7, 14], [1, 6, 8, 13], [1, 6, 9, 19], [1, 6, 10, 18], [1, 6, 11, 17],
    [1, 6, 12, 16], [1, 6, 15, 20], [1, 7, 9, 10], [1, 7, 11, 12], [1, 7,
    13, 15], [1, 7, 20, 21], [1, 8, 14, 20], [1, 8, 15, 21], [1, 8, 16, 18],
    [1, 8, 17, 19], [1, 9, 11, 15], [1, 9, 12, 13], [1, 9, 18, 21], [1, 10,
    11, 13], [1, 10, 12, 15], [1, 10, 19, 21], [1, 11, 16, 21], [1, 12, 17,
    21], [1, 13, 14, 21], [1, 14, 16, 17], [1, 14, 18, 19], [1, 16, 19, 20],
    [1, 17, 18, 20], [2, 3, 4, 5], [2, 3, 6, 21], [2, 3, 7, 15], [2, 3, 8,
    14], [2, 3, 9, 10], [2, 3, 11, 19], [2, 3, 12, 18], [2, 3, 13, 20], [2,
    3, 16, 17], [2, 4, 7, 20], [2, 4, 8, 15], [2, 4, 9, 11], [2, 4, 10, 19],
    [2, 4, 12, 17], [2, 4, 13, 14], [2, 4, 16, 18], [2, 5, 7, 14], [2, 5, 8,
    20], [2, 5, 9, 12], [2, 5, 10, 18], [2, 5, 11, 17], [2, 5, 13, 15], [2,
    5, 16, 19], [2, 6, 7, 18], [2, 6, 8, 19], [2, 6, 9, 13], [2, 6, 10, 17],
    [2, 6, 11, 14], [2, 6, 12, 15], [2, 6, 16, 20], [2, 7, 8, 10], [2, 7,
    11, 13], [2, 7, 12, 16], [2, 7, 19, 21], [2, 8, 11, 16], [2, 8, 12, 13],
    [2, 8, 18, 21], [2, 9, 14, 19], [2, 9, 15, 18], [2, 9, 16, 21], [2, 9,
    17, 20], [2, 10, 11, 12], [2, 10, 13, 16], [2, 10, 20, 21], [2, 11, 15,
    21], [2, 12, 14, 21], [2, 13, 17, 21], [2, 14, 15, 17], [2, 14, 18, 20],
    [2, 15, 19, 20], [2, 17, 18, 19], [3, 4, 7, 14], [3, 4, 8, 20], [3, 4,
    9, 19], [3, 4, 10, 11], [3, 4, 12, 16], [3, 4, 13, 15], [3, 4, 17, 18],
    [3, 5, 7, 20], [3, 5, 8, 15], [3, 5, 9, 18], [3, 5, 10, 12], [3, 5, 11,
    16], [3, 5, 13, 14], [3, 5, 17, 19], [3, 6, 7, 19], [3, 6, 8, 18], [3,
    6, 9, 16], [3, 6, 10, 13], [3, 6, 11, 15], [3, 6, 12, 14], [3, 6, 17,
    20], [3, 7, 8, 9], [3, 7, 11, 17], [3, 7, 12, 13], [3, 7, 18, 21], [3,
    8, 11, 13], [3, 8, 12, 17], [3, 8, 19, 21], [3, 9, 11, 12], [3, 9, 13,
    17], [3, 9, 20, 21], [3, 10, 14, 18], [3, 10, 15, 19], [3, 10, 16, 20],
    [3, 10, 17, 21], [3, 11, 14, 21], [3, 12, 15, 21], [3, 13, 16, 21], [3,
    14, 15, 16], [3, 14, 19, 20], [3, 15, 18, 20], [3, 16, 18, 19], [4, 5,
    6, 21], [4, 5, 7, 15], [4, 5, 8, 14], [4, 5, 9, 17], [4, 5, 10, 16], [4,
    5, 11, 12], [4, 5, 13, 20], [4, 5, 18, 19], [4, 6, 7, 16], [4, 6, 8,
    17], [4, 6, 9, 14], [4, 6, 10, 15], [4, 6, 11, 13], [4, 6, 12, 19], [4,
    6, 18, 20], [4, 7, 8, 12], [4, 7, 9, 13], [4, 7, 10, 18], [4, 7, 17,
    21], [4, 8, 9, 18], [4, 8, 10, 13], [4, 8, 16, 21], [4, 9, 10, 12], [4,
    9, 15, 21], [4, 10, 14, 21], [4, 11, 14, 17], [4, 11, 15, 16], [4, 11,
    18, 21], [4, 11, 19, 20], [4, 12, 13, 18], [4, 12, 20, 21], [4, 13, 19,
    21], [4, 14, 15, 19], [4, 14, 16, 20], [4, 15, 17, 20], [4, 16, 17, 19],
    [5, 6, 7, 17], [5, 6, 8, 16], [5, 6, 9, 15], [5, 6, 10, 14], [5, 6, 11,
    18], [5, 6, 12, 13], [5, 6, 19, 20], [5, 7, 8, 11], [5, 7, 9, 19], [5,
    7, 10, 13], [5, 7, 16, 21], [5, 8, 9, 13], [5, 8, 10, 19], [5, 8, 17,
    21], [5, 9, 10, 11], [5, 9, 14, 21], [5, 10, 15, 21], [5, 11, 13, 19],
    [5, 11, 20, 21], [5, 12, 14, 16], [5, 12, 15, 17], [5, 12, 18, 20], [5,
    12, 19, 21], [5, 13, 18, 21], [5, 14, 15, 18], [5, 14, 17, 20], [5, 15,
    16, 20], [5, 16, 17, 18], [6, 7, 8, 20], [6, 7, 9, 11], [6, 7, 10, 12],
    [6, 7, 15, 21], [6, 8, 9, 12], [6, 8, 10, 11], [6, 8, 14, 21], [6, 9,
    10, 20], [6, 9, 17, 21], [6, 10, 16, 21], [6, 11, 12, 20], [6, 11, 19,
    21], [6, 12, 18, 21], [6, 13, 14, 15], [6, 13, 16, 17], [6, 13, 18, 19],
    [6, 13, 20, 21], [6, 14, 16, 18], [6, 14, 17, 19], [6, 15, 16, 19], [6,
    15, 17, 18], [7, 8, 13, 21], [7, 8, 14, 15], [7, 8, 16, 17], [7, 8, 18,
    19], [7, 9, 12, 21], [7, 9, 14, 16], [7, 9, 15, 17], [7, 9, 18, 20], [7,
    10, 11, 21], [7, 10, 14, 17], [7, 10, 15, 16], [7, 10, 19, 20], [7, 11,
    14, 18], [7, 11, 15, 19], [7, 11, 16, 20], [7, 12, 14, 19], [7, 12, 15,
    18], [7, 12, 17, 20], [7, 13, 14, 20], [7, 13, 16, 18], [7, 13, 17, 19],
    [8, 9, 11, 21], [8, 9, 14, 17], [8, 9, 15, 16], [8, 9, 19, 20], [8, 10,
    12, 21], [8, 10, 14, 16], [8, 10, 15, 17], [8, 10, 18, 20], [8, 11, 14,
    19], [8, 11, 15, 18], [8, 11, 17, 20], [8, 12, 14, 18], [8, 12, 15, 19],
    [8, 12, 16, 20], [8, 13, 15, 20], [8, 13, 16, 19], [8, 13, 17, 18], [9,
    10, 13, 21], [9, 10, 14, 15], [9, 10, 16, 17], [9, 10, 18, 19], [9, 11,
    14, 20], [9, 11, 16, 18], [9, 11, 17, 19], [9, 12, 15, 20], [9, 12, 16,
    19], [9, 12, 17, 18], [9, 13, 14, 18], [9, 13, 15, 19], [9, 13, 16, 20],
    [10, 11, 15, 20], [10, 11, 16, 19], [10, 11, 17, 18], [10, 12, 14, 20],
    [10, 12, 16, 18], [10, 12, 17, 19], [10, 13, 14, 19], [10, 13, 15, 18],
    [10, 13, 17, 20], [11, 12, 13, 21], [11, 12, 14, 15], [11, 12, 16, 17],
    [11, 12, 18, 19], [11, 13, 14, 16], [11, 13, 15, 17], [11, 13, 18, 20],
    [12, 13, 14, 17], [12, 13, 15, 16], [12, 13, 19, 20], [14, 15, 20, 21],
    [14, 16, 19, 21], [14, 17, 18, 21], [15, 16, 18, 21], [15, 17, 19, 21],
    [16, 17, 20, 21], [18, 19, 20, 21]]
    

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 2021-03-07
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多