Hakase and Nano—博弈

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

好生气呀!!!明明推对了,怎么比赛时敲错了呢!!!!关键是自己推对了,但是不大相信自己。。。。

一直以为是自己的结论错了。。。

Problem C. 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?
Input The first line contains an integer T (1 ≤ T ≤ 20) representing the number of test cases. For each test case, the first 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.
Output For each test case, print “Yes” or “No” in one line. If Hakase can win, print “Yes”, otherwise, print “No”.
Example
standard input standard output
2

3 1

1 1 2

3 2

1 1 2

Yes

No

开始一眼还以为是裸的nim博弈呢。
题意:

就是haka必须要连续取两次,然后另一个人只能取一次。m==1:haka先手,m==2另一个人先手。

然后谁能够取最后一个石子谁就赢了。每一次只能选一堆,并选出大于等于1个石子。

思路:

首先haka先手的话,haka是有优势的,他有两次选择的机会,除了全为1,这种情况他无法更改,只要有一堆不是1,他就完全可以变成他想要的情况。就是全为1的个数。

haka后手的话:如果另一个人想要赢,那么他就一定要第一次拿就要把局面控制住。这就有限制了,如果有大于等于两堆的石子是大于1的,那么第一次取完后就变成了haka先手的那一种情况了,肯定就是haka   win了。

如果只有一堆的石子是大于1的,那么另一个人第一次拿石子就可以掌控局面,一次就定输赢了。

全为1看n%3后的数了。

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,t,x,flag;
    scanf("%d",&t);
    while(t--){
        flag=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            if(x>1)
                flag++;
        }
        if(m==1){///haka先手
            if(flag==0)///全是1
            {
                if(n%3==0)
                    cout<<"No"<<endl;
                else
                    cout<<"Yes"<<endl;
            }
            else{
                cout<<"Yes"<<endl;
            }
        }
        else{///haka后手
            if(flag==0){///全是1
                if(n%3==1)
                    cout<<"No"<<endl;
                else{
                    cout<<"Yes"<<endl;
                }
            }
            else{
                if(flag>1){
                    cout<<"Yes"<<endl;
                }
                else{
                    if(n%3==0 || n%3==1)
                        cout<<"No"<<endl;
                    else
                        cout<<"Yes"<<endl;
                }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/83146197
今日推荐