[BZOJ3895] take stones (+ good thinking title game theory to find the law)

Title Description

Alice and Bob are two good friends began to play with friends to take the stones. When the game starts, there are N stones piled
in a row, then they turn operation (Alice upper hand), optionally following a rule from each operation:
· a removal of the stones from a heap
-merge any two piles of stones
operation can not lose. Alice wondered if she could have a winning strategy.

Input
The first line enter T, for showing the number of data sets.
For each test, the first row is read N.
Next, N positive integers a1, a2 ... an, represents the number of each pile of stones.
Output
For each test case, the output line.
Output YES means Alice has a winning strategy, Alice output NO means no winning strategy.
Sample input
. 3
. 3
. 1. 1 2
2
. 3. 4
. 3
2. 3. 5
sample output
YES
NO
NO
data space constraints range
T <= 100, n <= 50, ai <= 1000;
Time limit: 1 Sec Memory Limit: 128 MB
ideas:
a few examples of pre-enumeration, the introduction of the law

originally did not want to write ideas, but found that many online solution to a problem of memory are used in the search, but this is not very good but can not handle larger data write, I think in training a simple rule of points, introduce the following:
a processing n = 1, n = 2, n = 3, the simulation can be manually calculated results, and the situation is addressed below n> = 4 in the case.
2 When a [i] are all 1, if n is a multiple of 3, certainly lose or win.
prove:
n = 1 or n = 2 can be directly think of borrowing, n ='ll find that no matter how their own operations, the other party can be carried out at 3 in one operation so the play was only a pile of stones and pebbles number is 2, this situation is losing state, when 4 n =, the other side can take a stone face and n = 3 each lot of stones are a number of cases, the win n = 4, n = a will first find out if a removed 5 stones, then B then removed, then a stone, a shall lose, so everyone to not try to take stones, then 5 stones piled into a heap of stones will merge four times, and the number of stones is 5, then a the upper hand, it is also found that a win, n = time. 6, acquire a pile of stones, it will win the rest state 5 gravel pile, a pile of stones to merge, b to remove a number of stones 1 stones, appearing in a 5 will encounter stones piled in a state of losing, so losing in a 6 n =, thereafter analogy can withdraw.
3 When a [i] is not all 1
Consider first the case without any of 1, and will find the total number of stones == 2, if the stack number modulo value modulo 2, then the win, or will lose.
Proved that
n = 1, can be directly observed that an odd number of stones wins the upper hand, the flip wins or,
when n = 2, if the sum is an even number of stones, the upper hand can be directly combined piles square stones, win, or it is doomed to failure, because no matter what side the upper hand this time the operation can not change the flip side of the one-operation on the field for the emergence of a number of heaps of stones and gravel sum is an even number of cases.
When n =. 3 is the same reason, and because at this time is an odd number, upper hand side can make the field by combining two piles of stones losing state n = 2 occurs, and also the upper hand side is an even number no matter how operation can be prevented flip through one side makes the face of losing a state.
n> = 4 may be inductive proof.
When a [i] is not all 1, set s is the number 1, can be found
when s is an even number, the subsequent party can be canceled so as to achieve an impact on the operating side of the upper hand for the operation of any one of the upper hand side.
For example, just get away side a 1, then flip side also took a 1
Alternatively, the two upper hand side binning, merging just put the flip side obtained on a non-2 added to a number, so that neither the non-change stack a number of stones, the stones did not change the number of a non-integrated parity.
Or, the upper hand side a 1 and a non-merge a pile of stones, on the flip side put another 1 applied immediately combined heap of stones to restore a non-parity of the sum of the number of stones.
So let s take on more than 2, and then on to the last one if else code, the inference here has been basically completed, if there is a problem in the comments below or private I qq1807458974;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n,x,s=0,sum=0,t;
    cin>>t;
    while(t--){
        cin>>n;
        s=0;sum=0;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            if(x==1)
                s++;
            sum+=x;
        }
        if(s==n)
        {
            if(sum%3!=0)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
            continue;
        }
        if(n==1)
        {
            if(sum%2==0)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
            continue;
        }
        if(n==2)
        {
            if(s==0&&sum%2==1)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
            continue;
        }
        if(n==3)
        {
            if(s==1)
                cout<<"YES"<<endl;
            if(s==2)
            {
                if(sum%2==0&&sum!=4)
                    cout<<"NO"<<endl;
                else
                    cout<<"YES"<<endl;
            }
            if(s==0)
            {
                if(sum%2==n%2)
                    cout<<"YES"<<endl;
                else
                    cout<<"NO"<<endl;
            }
            continue;
        }
        if(s%2==1)
        {
            sum-=s;
            n-=s;
            if(sum==2)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
            continue;
        }
        else
        {
            sum-=s;
            n-=s;
            //cout<<sum<<endl;
            if(sum==2||sum%2==n%2)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}

Published 18 original articles · won praise 6 · views 952

Guess you like

Origin blog.csdn.net/qq_43559193/article/details/105319831