组合数学的实现和难题
在图像中发现了此谜题。根据我的想法,方式总数应该是
2*comb(7,i) for i <- 1 to 7
其中 comb
定义如下。我的做法正确吗?我关心的是我得到的结果,而不是下面写的函数。
def comb(N,k):
if (k > N) or (N < 0) or (k < 0):
return 0L
N,k = map(long,(N,k))
top = N
val = 1L
while (top > (N-k)):
val *= top
top -= 1
n = 1L
while (n < k+1L):
val /= n
n += 1
return val
不要介意我在短时间内问太多问题。我只是很热情。
Found this puzzle inside an image. According to my thinking the total number of ways should be
2*comb(7,i) for i <- 1 to 7
where comb
is defined as follows. Is my approach correct? I am concerned with the result that I get and not the function written below.
def comb(N,k):
if (k > N) or (N < 0) or (k < 0):
return 0L
N,k = map(long,(N,k))
top = N
val = 1L
while (top > (N-k)):
val *= top
top -= 1
n = 1L
while (n < k+1L):
val /= n
n += 1
return val
Don't mind me asking too many questions in a short time period. I am just enthusiastic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有7个!孩子们排队的方式(第一位置有 7 种选择,第二位置有 6 种选择,第三位置有 5 种选择,等等)
每个孩子可以面朝内或朝外。这就像每个位置都多了一点。
所以乘以 2**7。 (即每个地点有 2 个选择)。
现在,对于每个排序,如果旋转圆圈,您会得到“相同”的排序。有 7 次旋转,所有旋转都产生相同的顺序,因此除以 7。
答案是 2**7 * 7!/7 = 128* 6! = 92160。
There are 7! ways to line up the children (7 choices for the first spot, 6 for the second, 5 for the third, etc.)
Each child can face inward or outward. That's like an extra bit for each position.
So multiply by 2**7. (i.e. there are 2 choices for each spot).
Now for each ordering, if you rotate the circle, you get the "same" ordering. There are 7 rotations which all produce the same ordering, so divide by 7.
The answer is 2**7 * 7!/7 = 128* 6! = 92160.