Alice's Game(HDU-3544)(博弈,找规律)

Alice and Bob have got a lot of chocolates. All the chocolates are rectangles of different shapes as X i * Y i.They decide to play an interesting game on the chocolates. They take turns choose a chocolate and split it into two pieces. The one who can not take operations lose. Due to the game is too simple, the additional rules apply. Alice is only allowed to split the chocolate vertically, and Bob is only allowed to split the chocolate horizontally. 
Specifically, for Alice, a chocolate X i * Y i, can only split into A * Y i, and B * Y i where A + B = X i and A, B > 0. And for Bob, a chocolate X i * Y i, can only split into X i * A, and X i * B where A + B = Y i and A, B > 0. 
Alice and Bob are clever enough to take the optimal operation, if Alice plays first, your are to decide who will win.

Input

The input contains multiple test cases. The first line of input contains a single integer denoting the number of test cases. 
For each test case, the first line contains an integer N, the number of pieces of chocolates. (1 <= N <= 100) 
For the next N lines, each line contains two integers X i and Y i, denoting the chocolate sized X i * Y i. (1 <= X i, Y i <= 1000000000)

Output

For each test cases, output "Alice" when Alice will win, and "Bob" otherwise. See sample test cases for further details.

Sample Input

4
1
1 1
1
2 1
2
2 2
2 1
1
3 2

Sample Output

Case 1: Bob
Case 2: Alice
Case 3: Alice
Case 4: Bob

题意:给一块n*m的巧克力,Alice只能垂直切,切成A*m和B*m,并且A+B=n,Bob只能横切,只能切成A*n和B*n,并且A+B=m。 Alice先手,谁不能切则为输。

思路:

本题我抄袭自《Winning Ways for your Mathematical Plays》 ,一本关于游戏论的科 
普类图书。 
这题是一个组合游戏,但是并不是一个对等的组合游戏,所以试图使用 SG 函数相关知 
识解答是会面临巨大的挑战的。 书中本题的做法描述得十分简单, 当然对于有这类组合游戏 
知识的同学来说这题也确实十分简单,如果没有相关背景知识,也没有关系,我们来慢慢分 
析这道题目。 
要成功地解答本题需要认真地分析这个游戏的规则,我们首先来考虑一些简单情况。 
(1) 只有 n*1 和 1*m 的巧克力的时候 
(2) 2*2 的巧克力 
(3) 2*3 和 3*2 的巧克力 
(4) n*2 和 2*m 的巧克力 
(5) n*3 和 3*m 的巧克力 
(6) 很多巧克力在一起的情况 
我们来一个一个分析这些情况,对于 n*1 和 1*m 的巧克力,显然 n*1 的巧克力对 alice 
有利, 而 1*m 的巧克力对 bob 有利。 假设 n*1 对于 alice 有 n-1 的 HP 贡献, 而 1*m 对于 bob 
有 m-1 的 HP 贡献。至于谁胜利?自然是谁 HP 多谁就胜利,当然考虑到先 alice 先扣 HP, 
所以如果 HP 一样多, alice 也输了。 为了方便, 我们定义 alice 的 HP 为正, bob 的 HP 为负。 
于是这个局面就可以通过简单的加法获得总的 HP 了。 
那 2*2 的巧克力呢, 认真分析就可以发现 2*2 在这个游戏中纯属幻觉! 谁也不愿意先拿 
他开刀,切一道送了对方两次切的机会,而自己却只切了一刀。于是我们可以说,2*2 的巧 
克力值 0 的 HP。 
同样 2*3 和 3*2 的巧克力也因为同样的道理就这么被无情地抛弃了。 
对于 n*2 的巧克力,根据直觉 alice 应该感到很高兴(当然不是 1*2) ,bob 自然不会傻 
到来切这个巧克力, 于是 alice 自己要想办法自己尽量多切几刀, 注意到切出 1*2 的巧克力 
是很不利的事情,于是每次都切 2*2 的,可以切(n/2)-1 刀。于是这就是 n*2 的巧克力的 HP 
贡献了。2*m 以及 n*3,3*m 的就不再赘述,都是一样。 
以此类推,4*4,8*8,16*16,都是比较关键的巧克力。

弄一个表吧,再找不到规律„„

å¨è¿éæå¥å¾çæè¿°

AC代码:(Java)

import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int t=cin.nextInt();
		int k=1;
		while(cin.hasNext())
		{
			int n=cin.nextInt();
			long a=0,b=0;
			for(int i=1;i<=n;i++)
			{
				long x=cin.nextLong();
				long y=cin.nextLong();
				while(x>1 && y>1)
				{
					x>>=1;
					y>>=1;
				}
				if(y==1) a+=(long)x-1;
				if(x==1) b+=(long)y-1;
			}
			System.out.print("Case "+k+++": ");
			if(a>b)
				System.out.println("Alice");
			else
				System.out.println("Bob");
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值