1. 程式人生 > >HDU 3544Alice's Game 【不平等博弈)】

HDU 3544Alice's Game 【不平等博弈)】

 

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先手,誰不能切則為輸

題解
http://www.cnblogs.com/AOQNRMGYXLMV/p/4462791.html 
原內容如下

切巧克力的遊戲,想得還是不是太明白。

後者會盡量選前著切後其中小的一塊來切,那麼先手須儘量取中間來切。

So?題解都是這麼一句話,不知道是真懂了還是從別人那抄過來的。 
後來找到一個官方題解,分析得比較認真,但我這智商還是沒懂太多,QAQ

本題我抄襲自《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,都是比較關鍵的巧克力。

弄一個表吧,再找不到規律„„


看錶格就知道   HP(i,j)=HP(\frac{i}{2},\frac{j}{2})
然後HP(1,j)和HP(i,1)都是直接求的。

import java.util.*;
import java.math.*;

public class Main{
	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		int T=cin.nextInt();
		int ca=1;
		while((T--)!=0) {
			int n=cin.nextInt();
			long a=0,b=0;
			for(int i=0;i<n;i++) {
				long x=cin.nextLong();
				long y=cin.nextLong();
				while(x>1&&y>1) {
					x=x>>1;
					y=y>>1;
				}
				if(y==1)	a+=(x-1);
				if(x==1)	b+=(y-1);
			}
			System.out.print("Case "+ ca++ +": ");
			System.out.println((a>b)?"Alice":"Bob");
		}
		cin.close();
	}
}