HDOJ 3544 Alice's Game 博弈

来源:互联网 发布:运营商域名劫持 编辑:程序博客网 时间:2024/06/10 06:07

Alice's Game

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 406    Accepted Submission(s): 167


Problem Description
Alice and Bob have got a lot of chocolates. All the chocolates are rectangles of different shapes as Xi * Yi.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 Xi * Yi, can only split into A * Yi, and B * Yi where A + B = Xi and A, B > 0. And for Bob, a chocolate Xi * Yi, can only split into Xi * A, and Xi * B where A + B = Yi 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 Xi and Yi, denoting the chocolate sized Xi * Yi. (1 <= Xi, Yi <= 1000000000)
 

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

Sample Input
411 112 122 22 113 2
 

Sample Output
Case 1: BobCase 2: AliceCase 3: AliceCase 4: Bob
 

Author
HyperHexagon
 

Source
HyperHexagon's Summer Gift (Original tasks)
 

Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3553 3545 3546 3547 3548 
 

这题是一个组合游戏,但是并不是一个对等的组合游戏,所以试图使用 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 值加起来,我们这题就做完了? Oh yeah.

!!!!来自官方解题报告


#include <iostream>using namespace std;int main(){long long m,n,a,b,suma,sumb;cin>>m;for(int i=1;i<=m;++i){cin>>n;suma=sumb=0;while (n--){cin>>a>>b;while (a>1&&b>1){a>>=1;b>>=1;}if (a>b)suma+=a;elsesumb+=b;}cout<<"Case "<<i<<": ";cout<<(suma>sumb?"Alice\n":"Bob\n");}return 0;}


kdwycz的网站:  http://kdwycz.com/

kdwyz的刷题空间:http://blog.csdn.net/kdwycz


0 0