ZOj 3964Yet Another Game of Stones(nim博弈)
来源:互联网 发布:三鹿事件的影响 知乎 编辑:程序博客网 时间:2024/06/05 06:48
Alice and Bob are playing yet another game of stones. The rules of this game are as follow:
- The game starts with n piles of stones indexed from 1 to n. The i-th pile contains ai stones and a special constraint indicated as bi.
- The players make their moves alternatively. The allowable moves for the two players are different.
- An allowable move of Bob is considered as removal of some positive number of stones from a pile.
- An allowable move of Alice is also considered as removal of some positive number of stones from a pile, but is limited by the constraint bi of that pile.
- If bi = 0, there are no constraints.
- If bi = 1, Alice can only remove some odd number of stones from that pile.
- If bi = 2, Alice can only remove some even number of stones from that pile.
- The player who is unable to make an allowable move loses.
Alice is always the first to make a move. Do you know who will win the game if they both play optimally?
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1 ≤ n ≤ 105), indicating the number of piles.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), indicating the number of stones in each pile.
The third line of each test case contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 2), indicating the special constraint of each pile.
It is guaranteed that the sum of n over all test cases does not exceed 106.
We kindly remind you that this problem contains large I/O file, so it's recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.
Output
For each test case, output "Alice" (without the quotes) if Alice will win the game. Otherwise, output "Bob" (without the quotes).
Sample Input
324 11 0132112
Sample Output
AliceBobBob
Hint
For the first test case, Alice can remove 3 stones from the first pile, and then she will win the game.
For the second test case, as Alice can only remove some even number of stones, she is unable to remove all the stones in the first move. So Bob can remove all the remaining stones in his move and win the game.
For the third test case, Alice is unable to remove any number of stones at the beginning of the game, so Bob wins.
想法:
此题的题意比较容易理解,可以简单的看着 Nim 博弈的变种。但问题在于 Alice 对第 i 堆石子的取法必须根据
考虑特判存在
- 如果存在第 i 堆石子,其
ai 为奇数且bi=2 ,则 Bob 必胜(Alice 在最优策略下无法取完该堆,但 Bob 可以)。 - 如果存在 2 个及以上
bi=2 或bi=1 且 ai>1 的情况,则 Bob 必胜。 - 如果只有一个
bi=2 (其余都为bx=0 ) 的情况,则 Alice 为了胜利,必须先将该堆石子取完(否则 Bob 只需取掉该堆 1 个石子, Bob 必胜)。此时问题等同于 n-1 堆石子,Bob 先手的 Nim 博弈。 - 如果只有一个
bi=1 且 ai>1 (其余都为bx=0 )的情况,Alice 同样需先将该堆石子取完(或只剩一个)。此时问题等同于 n-1(n) 堆石子,Bob 先手的 Nim 博弈。
对于只有
#include<stdio.h>
const int N = 1e5 + 10;
int a[N], b[N], n;
bool jug()
{
int cnt[3] = {0, 0, 0}, tot = 0;
for(int i=1;i<=n;i++)
{
if(a[i]%2 && b[i] == 2)
return false;
if(b[i] == 2)
cnt[2]++,cnt[0]++;
if(a[i] > 1 && b[i] == 1)
cnt[1]++,cnt[0]++;
}
if(cnt[0]>1)
return false;
if(cnt[1] == 1)
{
for(int i=1;i<=n;i++)
if(b[i] == 1 && a[i] > 1)
tot ^= (a[i]%2?0:1);
else tot ^= a[i];
return !(tot?1:0);
}
else if(cnt[2] == 1)
{
for(int i=1;i<=n;i++)
if(b[i] != 2)
tot ^= a[i];
return !(tot?1:0);
}
else {
for(int i=1;i<=n;i++)
tot ^= a[i];
return tot;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
printf("%s\n", jug() ? "Alice" : "Bob");
}
return 0;
}
- ZOj 3964Yet Another Game of Stones(nim博弈)
- ZOJ 3964 Yet Another Game of Stones(Nim博弈 待整理)
- ZOJ 3964 Yet Another Game of Stones (博弈)
- ZOJ 3964 Yet Another Game of Stones (博弈)
- ZOJ 3964 Yet Another Game of Stones(博弈论)
- Yet Another Game of Stones---ZOJ Problem 3964
- Yet Another Game of Stones
- 768E Game of Stones[Nim游戏][博弈]
- CodeForces 768E Game of Stones (Nim博弈)
- 2017浙江省赛:Yet Another Game of Stones(尼姆变形)
- ZOJ 3505 Yet Another Set of Numbers
- Codeforces 768E Game of Stones 博弈
- SPOJ GAME3 9842. Yet Another Fancy Game 博弈
- zoj 3610 Yet Another Story of Rock-paper-scissors
- ZOJ 3610 Yet Another Story of Rock-paper-scissors
- ZOJ 3610 Yet Another Story of Rock-paper-scissors
- ZOJ 3505 Yet Another Set of Numbers(DP+构造)
- ZOJ 3610 Yet Another Story of Rock-paper-scissors
- java基础之继承
- 服务器实时推送
- Linux操作快捷键整理
- linux 驱动 信号量 测试
- CodeForces 831A:Unimodal Array(模拟)
- ZOj 3964Yet Another Game of Stones(nim博弈)
- string类型字符串切割
- 架构
- C++学生成绩管理系统
- 浅谈产品设计五大层次
- B继承A的写法
- 素数环
- 【bzoj1058】[ZJOI2007]报表统计 平衡树
- python 高性能web框架 gunicorn+gevent