Game BZOJ1022: [SHOI2008] Little John's game John__nim game

Source: Internet
Author: User
the

Little John often plays a very interesting game with his brother: there are n heaps of stones on the table, Little John and his brother took turns to take stones, each person to take the time, can choose a pile of stones, in this heap of stones to take any many stones, but not a stone also do not take, we stipulate to take the last stone of the people calculate lose. Little John was quite stubborn, and he insisted that the first man had a great advantage, so he always took the stone first, and his brother was so much smarter that he never made mistakes in the game. He asked you to be his staff before he got angry. Naturally, you should first write a program that predicts who will win the game. Solving

Classic Anti-nim Game, a search on the internet can find a lot of information.
Just stick to the conclusion:
First, when all numbers are 1, special consideration is needed. (then you'll know why.)
In the case of a few, we need to focus on the difference or value of all the numbers.
If the difference or value is equal to 0, then win, otherwise the tempo will be defeated.
Why, then?
For a situation (A1,A2,..., an), if A1 xor A2 xor ... an!=0, there must be a legitimate move, the AI will be changed to AI ' after the A1 XOR A2 xor ... xor an=0, can be set A1 XOR A2 Xor...xor An=k, obviously there must be an AI, its binary representation at the highest level of K is 1. At this time Ai xor k< AI must be established. Then we can change the AI to Ai xor K.
For a situation (A1,A2,..., an), if A1 XOR A2 xor ... xor an=0, no matter how, the difference or value after that is not 0.
In other words, when the initial situation is different or the value is 0 o'clock, the control field ability. Specifically, each time the state of 0 to the opponent,0-> 0->0-> not 0 constantly changing, when now found in the non-0 status of all the number of only 1, the other is 1 o'clock, it is clear that victory can be won. (This state must be a non-0 state, which means that this state will not be left to the opponent)
When the initial situation is not 0 o'clock, the first move is to turn it into a 0 state for the opponent, so win.

 #include <cstdio> #include <algorithm> using namespace std; int _test,n; int main ()
    {freopen ("bzoj1022.in", "R", stdin);
    Freopen ("Bzoj1022.out", "w", stdout); 
    scanf ("%d", &_test);
        while (_test--) {scanf ("%d", &n); int xor=0; bool Pd=false; 
            for (int i=1;i<=n;i++) {int x; scanf ("%d", &x); Xor^=x;
        Pd|= (x!=1);
        } if (PD) {if (Xor) printf ("john\n"); else printf ("brother\n");
        else{if (Xor) printf ("brother\n"); else printf ("john\n");
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.