Hakase and Nano(思维+博弈)

本文介绍了一个有趣的博弈问题,Hakase与Nano进行古老石子游戏,探讨了不同规则下Hakase能否获胜的策略。通过分析发现,游戏结果取决于石子堆的数量及每堆石子的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题 C: Hakase and Nano

时间限制: 1 Sec  内存限制: 128 MB
提交: 500  解决: 142
[提交] [状态] [命题人: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

题意是每次输入n,d,n代表有n堆石子,d为1表示Hakase先拿,d为2 表示Nano先拿,Hakase必须拿两次,Nano必须拿一次(不管谁拿,每次都可以取任意个数)谁能把这是自全都拿完,谁就胜

经过分析我们可以知道,当Hakase先拿的时候,只有在n为3的倍数,全部堆数都是1 的情况下才会输,

所以,在Nano先拿的时候,要尽量把情况变成对方会输的情况,所以就有三种可能:

1.n是3 的倍数,只有一对石子不是1(可以把那不是1 的石子变成1)

2.n%3=1,全部石子都是1(可以先取走一堆)

3.n%3==1,有一堆石子不是1(可以把那一堆不是1 的石子取走)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int main(){
	int t,n,d,x;
	scanf("%d",&t);
	while(t--){
		int count=0;
		scanf("%d%d",&n,&d);
		for(int i=0;i<n;i++){
			scanf("%d",&x);
			if(x==1)
			    count++;
		}
		if(d==1){
			if(n%3==0&&count==n)
			    printf("No\n");
			else
			    printf("Yes\n");
		}
		else{
			if((n%3==1&&count==n)||(n%3==0&&count==n-1)||(n%3==1&&count==n-1))
			    printf("No\n");
			else
			    printf("Yes\n");
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值