Hakase and Nano 【思维博弈】

Hakase and Nano

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

以为是NIM,一直在想怎么控制数量相同,然后自己写了几组数据找规律发现总是能赢..

就去找会输的情况,发现只有很多1的时候才会输

先手的话只有全部是1,并且堆数得是3的倍数才会输

后手就是在先手的情况下加一步,

有一堆数量不为1的堆,其他全部为1 并且堆数得是3的倍数

或者 ,堆数得是3的倍数 + 1 ,有一堆的数量任意,其他全为1

#include<iostream>
#include<cstdio>     //EOF,NULL
#include<cstring>    //memset
#include<cstdlib>    //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath>           //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm>  //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip>             //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h>     //INT_MAX
#include<bitset> // bitset<?> n
using namespace std;
 
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long LL;
typedef long long ll;
const double eps=1e-8;
const double PI = acos(1.0);
const int INF = 0x3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 1e6+7;
const int maxm = 1;
const int maxn = 1000000+10;
const ll MOD = 998244353;
int T;
ll a[maxn];
int flag ;
int n,d;
int cnt ;
int main(){
  read(T);
  while(T--){
    read2(n,d);
    cnt = 0;
    for(int i = 0; i < n; i++){
      scanf("%lld",&a[i]);
      if(a[i] != 1) cnt ++;
    }
    flag = 1;
    if(d == 1){
      if(n % 3 == 0 && cnt == 0) flag = 0;
    }
    if(d == 2) {
      if(n % 3 == 0 && cnt == 1) flag = 0;
      else if(n % 3 == 1 && cnt <= 1) flag = 0;
    }
    if(flag) cout << "Yes" << endl;
    else cout << "No" << endl;
  }
}
 

猜你喜欢

转载自blog.csdn.net/kl782636177/article/details/89047224
今日推荐