Hakase and Nano【博弈】

版权声明:版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/YT201758501112/article/details/89074745

Hakase and Nano

时间限制: 1 Sec  内存限制: 128 MB
提交: 533  解决: 155
[提交] [状态] [命题人:admin]

题目描述

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

题目大意:有两个人Hakase和Nano,先输入一个整数t,代表有t组数据,每组数据先输入两个整数n,d,代表有n堆石子,d为一代表Hakase先手,d为2代表Nano先手,下面一行输入n个整数,代表每堆石子的个数,由于Hakase作弊,所以每次Hakase都拿两次石子,而Nano每次拿一次,每次取石子最少取一个,最先拿完所有石子的人获胜,若最终Hakase获胜,则输出Yes,否则输出No

解决方法:博弈题目,对于n==1的情况,谁先手则谁获胜,对于n==2的情况,怎么都是Hakase获胜,对于n大于3的情况,当Hakase先手时,只要不是遇到n%3==0&&n堆石子个数均为1的情况,那么Hakase一定获胜,否则Nano获胜;当Nano先手时,若Nano想获胜,他只有想办法让情况变为n%3==0&&全为1,所以分类讨论即可。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int t,n,d;

int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>t;
    while(t--){
    	int x,num1=0,num2=0;
    	cin>>n>>d;
    	for(int i=1;i<=n;i++){
    		cin>>x;
    		if(x==1)num1++;
    		else num2++;
    	}
    	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(num1==n&&n%3==0) cout<<"No"<<endl;
    			else cout<<"Yes"<<endl;
    		}
    		else {
    			if(n%3==0) {
    				if(num1==n-1) cout<<"No"<<endl;
    				else cout<<"Yes"<<endl;
    			}
    			else if(n%3==1) {
    				if(num1==n||num1==n-1) cout<<"No"<<endl;
    				else cout<<"Yes"<<endl;
    			}
    			else {
    				cout<<"Yes"<<endl;
    			}
    		}
    	}
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/YT201758501112/article/details/89074745
今日推荐