Yet Another Game of Stones---ZOJ Problem 3964

Yet Another Game of Stones

Time Limit: 1 Second       Memory Limit: 65536 KB

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 a1a2, ..., an (1 ≤ ai ≤ 109), indicating the number of stones in each pile.

The third line of each test case contains n integers b1b2, ..., 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 wins.3

题意:

有n堆石子,Bob每次可以在其中一堆中取走任意数量的石子,但不能不取,而对Alice却有额外要求,对于某些石子

堆,Alice只能取走偶数个石子(也不能不取),对于某些石子堆,Alice只能取走奇数个石子,现输入一个数n表示

有n堆石子,接下来n个数分别表示每堆石子的个数,再接下来的n个数表示对Alice的限制,第i个数为1表示对于第i堆

石子Alice只能取走奇数个,第i个数为2表示对于第i堆石子Alice只能取走偶数个,第i个数为0表示没有限制(Alice和

Bob一样可以随便取)现必定Alice先手,不能取者输,问谁必胜


思路:

这个题目很有意思,刚刚拿到的时候感觉没什么头绪,仔细想了一后会发现,这道题目就是简单的尼姆游戏变异版只是需要考虑几种由于bi造成的Alice

必败的局面就行了,我把这道题分成了几种情况(既然有奇偶要求所以思路可以往这上面发散):

1.每堆石子ai与bi只有四种情况    a 奇/偶   b 奇/奇   c 偶/奇    d 偶/偶  (个数/取得方式),没写的就是怎么取都行,即bi=0,那么就是一个nim游戏了。

2.对于这四种情况可以知道一旦出现a情况则Alice必输,因为对于这一堆最后的一块一定是Bob取走,就算当前对于Bob是必输态(不看这一堆),他只要取了这一堆的最后一粒,那么必输态就转换给力Alice,而Alice一点办法也没有。

3.而其他三种状态如d,Alice需要一次性把这堆取完,不然Bob只需要在这一堆去取走一个石子,那么b情况就变成了a情况。对于c和b,应当知道Alice应该一次全部取完或者剩下一个,因为Bob会利用bi的限制,使Alice不能一次性取完这一堆,在上面浪费步骤。而对于这三种情况,可以看出来,虽然Alice是先手,但是也只能改变一次,如果这三种状态存在1种以上,那么Alice就一定会输,因为Bob不会放过这个机会,即两人第一是最聪明的这一前提。

4.如果没有a状态并且b,c,d中只有一种,那么Alice需要把b,c,d状态中存在的全部取完或者只剩一个,然后剩下的石子,在判断就是一个简单的nim问题了,但是变为Bob为先手。

5.如果a,b,c,d四种状态都没有即对Alice没有限制,那么就是直接对堆异或,就是一个Alice先手的nim游戏。

下面代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int a[100100],b[100100];
int main()
{
    int t,n;
    int flag1,flag2,flag3;
    scanf("%d",&t);
    while(t--)
    {
         int sum=0;
        flag1=flag2=flag3=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&b[i]);

        for(int i=0;i<n;i++)
        {
            if((a[i]%2)&&(b[i]==2)) flag1=1;///奇偶
            if((!(a[i]%2)&&(b[i]==2)||!(a[i]%2)&&(b[i]==1)||(a[i]%2)&&(b[i]==1))&&(a[i]>1)) flag2++;
               ///偶偶                ///偶奇                  ///奇奇
        }

        if(flag1||flag2>1)
        {
            printf("Bob\n");
            continue;
        }
        else if(flag1==0&&flag2==0)///四态都没有
        {
            sum=0;
            for(int i=0;i<n;i++)
            sum^=a[i];
            if(sum!=0) printf("Alice\n");
            else printf("Bob\n");
            continue;
        }

        for(int i=0;i<n;i++)
        {
            if(!(a[i]%2)&&(b[i]==2))  a[i]=0;
            if(!(a[i]%2)&&(b[i]==1))  a[i]=1;
            if((a[i]%2)&&(b[i]==1)&&(a[i]>1))  a[i]=0;
        }

        for(int i=0;i<n;i++)
           // if()
            sum^=a[i];
        if(sum==0) printf("Alice\n");
        else printf("Bob\n");

    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值