罐中红球与蓝球问题:
假设罐中有红球与蓝球若干,从罐中取球,规则是:
while (# of marbles inthe jar > 1) {
choose (any) twomarbles from the jar;
if (the two marblesare of the same color) {
toss them aside;
place a RED marbleinto the jar;
}
else { // one marble of each color was chosen
toss the chosenRED marble aside;
place the chosenBLUE marble back into the jar;
}
}
每次去两个球,如果是同颜色,把他们放到一边,向罐中放入一个红球;如果不同颜色,舍弃红球,把蓝球放入罐中。问:最终的状态是?
简单分析可得:每一次迭代之后,罐中球的数量都将减一,即如果原来罐中有N个球,经过N-1次迭代之后,罐中只有一个球。
定义:初始状态,红球数量K,蓝球数量M,最后一个球的颜色的函数f(K,M)。进一步分析,蓝球的数量或者减2(拿出的两个球的颜色均为蓝色),或者不变(取出的两个球颜色不同)。在一次循环结束后,蓝球的数量奇偶是不变的。此为,循环不变量。
f(K,M) = { RED if M is even
{ BLUE otherwise (i.e., if M is odd)
突然想起另外一个与球相关的问题:
You have 50 red marbles, 50 blue marbles and 2 jars. One of the jars is chosen at random and then one marble will be chosen from that jar at random. How would you maximize the chance of drawing a red marble? What is the probability of doing so? All 100 marbles should be placed in the jars.
答案:一个罐子只放一个红球,剩下的红球和蓝球放到另外一个罐子中。拿到红球的概率计算方法:
P( red) = P(Jar 1 ) * P( red marble in Jar 1 ) + P( Jar 2 ) * P( red marble in Jar 2 )
= 0.5 * 1 + 0.5 * 49/99
= 0.7474
或者从数学角度分析这个问题,假设一个罐中红球的数量为x,另外一个罐中的数量50-x。取得红球的概率为f(x)=0.5x+0.5*((50-x)/(100-x)),即求得f(x)max时, x的值。
网格封闭曲线游戏
游戏基于以下网格进行,两个玩家为红和蓝,红方画红线,水平或者垂直,连接两个点(i, j),(x, y)。蓝方亦然,画的是蓝线。红方的目标是画一个封闭的红色曲线,而蓝方则是阻止红方。问:红方或者蓝方有必胜策略吗?
6 . . . . . . .
5 . . . . . . .
4 . . . . . . .
3 . . . . . . .
2 . . . . . . .
1 . . . . . . .
1 2 34 5 6 7
是的,蓝方有必胜策略。
if (Red drew a horizontalline segment) {
let i and j be suchthat Red's line segment connects (i,j) with (i,j+1)
if (i>1) {
draw a verticalline segment connecting (i-1,j+1) with (i,j+1)
} else {
draw a line segmentanywhere
}
} else // Red drew a vertical line segment
let i and j be suchthat Red's line segment connects (i,j) with (i+1,j)
if (j>1) {
draw a horizontalline segment connecting (i+1,j-1) with (i+1,j)
} else {
draw a line segment anywhere
}
}
蓝方在红方所画曲线的“右上角”设定蓝线,阻止红方形成封闭曲线。由此,该游戏存在不变量,
There does not exist on the grid a pair of red line segmentsthat form an upper right corner.
当然不仅仅是右上角,其余的三个角都可以拿来作为不变量。
Ref:http://www.cs.uofs.edu/~mccloske/courses/cmps144/invariants_lec.html