Hakase and Nano【博弈】

题目传送门

题目描述

Hakase and Nano are playing an ancient pebble game (pebble is a kind of rock). There are n packs of pebbles, and the i-th pack contains ai pebbles. They take turns to pick up pebbles. In each turn, they can choose a pack arbitrarily and pick up at least one pebble in this pack. The person who takes the last pebble wins.
This time, Hakase cheats. In each turn, she must pick pebbles following the rules twice continuously.
Suppose both players play optimally, can you tell whether Hakase will win?

 

输入

The first line contains an integer T (1≤T≤20) representing the number of test cases.
For each test case, the fi rst line of description contains two integers n(1≤n≤106) and d (d = 1 or d = 2). If d = 1, Hakase takes first and if d = 2, Nano takes first. n represents the number of pebble packs.
The second line contains n integers, the i-th integer ai (1≤ai≤109) represents the number of pebbles in the i-th pebble pack.

输出

For each test case, print “Yes” or “No” in one line. If Hakase can win, print “Yes”, otherwise, print “No”.

样例输入

复制样例数据

2
3 1
1 1 2
3 2
1 1 2

样例输出

Yes
No

[提交][状态]

题目大意:有n个袋子,第个袋子里有a[i]个石子,Hakase和Nano轮流拿石子,每次可以从任意一个袋子中拿至少一个石子,最后把所有石子拿光的人获胜。问Hakase能否获胜,若能,输出Yes,否则输出No

解题思路:

  • 若n=1那么必然先手胜

  • 若n=2那么不管谁是先手一定是Hakase胜

  • 若n>=3那么需要进行分类讨论

  1. 若Hakase为先手,那么除了n%3==0并且所有袋子里的石子数目都为1以外,都是Hakase胜

  2. 若Nano为先手,那么Nano想要获胜必须保证Nano第一次操作之后出现n%3==0并且所有袋子里的石子数目都为1(即1状态),否则Hakase胜

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define mc(a,b) memcpy(a,b,sizeof(b))
#define inf 0x3f3f3f
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ULL;
const int mod=1e9+7;
const int N=1e5+7;
int t,n,d;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>d;
        int x;
        int num=0;
        for(int i=0;i<n;i++){
            cin>>x;
            if(x==1) num++;
        }
        if(n==1){
            if(d==1) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
        else if(n==2) cout<<"Yes"<<endl;
        else{
            if(d==1){
                if(n%3==0&&n==num) cout<<"No"<<endl;
                else cout<<"Yes"<<endl;
            }
            else{
                if(n%3==0){
                    if(num==n-1) cout<<"No"<<endl;
                    else cout<<"Yes"<<endl;
                }
                else if(n%3==1){
                    if(num==n||num==n-1) cout<<"No"<<endl;
                    else cout<<"Yes"<<endl;
                }
                else cout<<"Yes"<<endl;
            }
        }
    }
    return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值