Permutation: divide 713 0's and 31 1's over 744 different positions [closed]

牧云@^-^@ 提交于 2019-12-20 07:56:27

问题


So I need to figure out all the possible permutations to divide 31 1's and 713 0's over 744 positions (all zeroes and ones are exactly the same). I created the following code to create all permutations of 0's and 1's over 744 positions and my idea was to delete those lines which do not sum to 31, but I get a memory overflow. Any way to do this more efficiently?

from itertools import product
comb = list(product([0,1], repeat=744))    
print(comb)

回答1:


This sounds like a combinatorics problem. Finding all of the permutations seems a like it would be quite expensive for the numbers that you are dealing with as it is a really large number. There are algorithms designed to determine permutations like Heap's algorithm. In this case, you could run the algorithm and only keep a representation of the positions that sum to thirty one. You would need to store these though which may cause the same error. A large part of the issue is you're trying to keep all of the possibilities which is a huge number.

You could also attempt to start with an array containing only 31 1's then you'd need shuffle them to obtain the permutations. This would be less expensive spatially than storing all of the permutations at least.

If you intend to find the number of potential combinations?

If that is the case you can consider n choose k maybe e.g. 744 choose 31.



来源:https://stackoverflow.com/questions/59058203/permutation-divide-713-0s-and-31-1s-over-744-different-positions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!