杭电acm:4994 Revenge of Nim



Revenge of Nim

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 721    Accepted Submission(s): 370


Problem Description
Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.
---Wikipedia

Today, Nim takes revenge on you. The rule of the game has changed a little: the player must remove the objects from the current head(first) heap. Only the current head heap is empty can the player start to remove from the new head heap. As usual, the player who takes the last object wins.
 

Input
The first line contains a single integer T, indicating the number of test cases.

Each test case begins with an integer N, indicating the number of heaps. Then N integer Ai follows, indicating the number of each heap successively, and the player must take objects in this order, from the first to the last.

[Technical Specification]
1. 1 <= T <= 100
2. 1 <= N <= 1 000
3. 1 <= Ai <= 1 000 000 000
 

Output
For each test case, output “Yes” if the first player can always win, otherwise “No”.
 

Sample Input
  
  
2 1 2 2 1 1
 

Sample Output
  
  
Yes No
 

4994解题思路

题目翻译:

问题描述:

取物游戏(Nim)是一个关于战略问题的数学游戏。在这个游戏中两个玩家轮流将物体从不同的堆中取出。每个回合,玩家必须删除至少一个物体,可以删除来自同一个堆的任意数量的物体。

今天,Nim对你实施报复。游戏的规则已经改变了一点:玩家必须把物体从当前头堆(第一个)取出。只有当前头堆是空的时,玩家可以开始删除新头堆。像往常一样, 将最后一个物体取出的玩家获胜。

输入:

第一行包含一个整数,表示测试用例的数量。

每个测试用例始于一个整数N,指示堆的数量。紧跟着是N个整数Ai,相继表示每个堆的数量,玩家必须按此顺序来取物体,从第一个到最后一个。

输出:

对于每个测试组,如果第一个玩家总能赢输出Yes”,否则输出“No

样值输入:

2

1

2

2

1 1

样值输出:

Yes

No

思考:

列举几种情况如下:

N

1

2

2

3

3

3

3

Ai

m

1

m

n

m

1

1

m

1

n

m

n

1

m

n

n

m

r

yes

no

yes

yes

no

yes

yes

 

n

4

4

4

4

4

4

4

4

a

1

1

1

M

1

1

n

M

1

n

1

M

n

1

1

M

1

n

n

M

n

1

n

M

n

n

1

M

n

n

n

M

r

No

Yes

no

Yes

No

Yes

Yes

yes

 

5

5

5

5

5

5

5

5

1

1

1

1

M

1

1

1

N

M

1

1

N

1

M

1

1

N

N

M

1

N

1

1

M

1

N

1

N

M

1

N

N

1

M

1

N

N

N

M

Yes

No

Yes

Yes

No

No

No

No

5

5

5

5

5

5

5

5

N

1

1

1

M

N

1

1

N

M

N

1

N

1

M

N

1

N

N

M

N

N

1

1

M

N

N

1

N

M

N

N

N

1

M

N

N

N

N

M

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Yes

 

只要第一个拿的人能掌握主动权,那他就可以赢。总结上述规律可以发现只要前几个连续的1”的个数是偶数个,那么第一个拿取的人就占有主动权,就可以赢。用这个规律来解决问题就非常简单了。


代码:

#include <iostream>

using namespace std;

int main()
{
        int T,N,count,i,j;
        int A[1000];
        cin >> T;
        for(i=1;i<=T;i++)
        {
                cin >> N;
                for(j=1;j<=N;j++)
                {
                        cin >> A[j];
                }
                count=0;
                for(j=1;j<N;j++)
                {
                        if(A[j]==1)
                                count++;
                        else
                                break;
                }
                if(count%2==0)
                        cout << "Yes" <<endl;
                else
                        cout << "No" <<endl;
        }
        return 0;
}

祝大家好运!


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值