ZOJ - 3964 Yet Another Game of Stones 博弈

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.
    Please note that there are no constraints on Bob.
  • 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

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

Sample Output

Alice
Bob
Bob

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 win

题意:先手Alice 标记为0的堆随意拿,标记为1的只能拿奇数,标记为2的只能拿偶数个

题解:1.当标记为2时并且该堆为奇数个,必输;

2.当有标记为2,奇数k个时,先手必须拿去k-1个,否则,后手会操作此堆变为偶数,那么先手必输,

3.当有标记为1,个数为1时,无影响;当大于1时,若为奇数,全部拿掉,不然,假设剩两个,后手既可以一次拿掉,也可以两次,而先手必须拿两次,后手可以根据此情况变为必胜状态;若为偶数,拿掉 总数-1 个,如果不只剩一个,那最少为3个,后手又可以根据自己的情况转化为必胜

也就是,当标记为2个数为奇数时必败,标记为2偶数个 和 标记为1 这样的情况大于1次时 也必败,其他情况要看每堆的个数了

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+10;
int n,a[N],b[N];
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]);
		int flag=1,cnt=0,ans=0,p;
		for(int i=1;i<=n;i++)
		{
			if(b[i]==2&&a[i]%2)
			{
				flag=0;
				break;
			}
			if(b[i]==2||(b[i]==1&&a[i]!=1)) cnt++,p=i;
			ans^=a[i];
		}
		if(cnt>=2)
                {
                    flag=0;
                }
                else if(cnt==1)
                {
                    ans=0;
                    for(int i=1;i<=n;i++)
                    {
                        if(p==i)
                        {
                            if(b[i]==1&&a[i]%2==0 || b[i]==2&&a[i]%2) ans^=1;
                        }
                        else ans^=a[i];
                    }
                    if(ans) flag=0;
                }
                else
                {
                    if(!ans) flag=0;
                }
        
		if(flag) cout<<"Alice"<<endl;
		else cout<<"Bob"<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/84585232