HDU 5795 A Simple Nim (game)---2016 Hangzhou Electric Multi-school joint sixth field

Source: Internet
Author: User

A Simple Nim

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): Accepted submission (s): 48


problem Descriptionplayers take turns picking candies from N heaps,the player who picks the last one would win the game. On each turn they can pick any number of candies which come from the same heap (picking no candy are not allowed). The game more interesting,players can separate one heap into three smaller heaps (no empty heaps) instead of the pic King operation. Please find out which player would win the game if each of the them never make mistakes. 

InputIntput contains multiple test cases. The first line was an integer1≤T≤ , the number of test cases. Each case begins with a integer n, indicating the number of the heaps, the next line contains n integerss[ 0], S[1 ], . ,s [n− 1] , representing Heaps with  s [0] , s[ 1], . ,s [n− 1]  objects respectively. (1 ≤n ≤ 106 1≤ s[ I]≤109)  

OutputFor each test case,output a line whick contains either "first player wins." or "Second player wins". 

Sample Input224 431 2 4 

Sample OutputSecond player wins. First player wins. 

AuthorUESTC 

Sourcemulti-university Training Contest 6 

recommendwange2014Test Instructions: given n heap of stones, each time there are two operations, 1 is from any one of the heap to take any, 2 is a bunch divided into three piles and these three piles are not empty, ask who can win. the problem: First look at the topic of the data range is 1e9, then the calculation of each SG will definitely time out, so we think of playing table to find the law. First of all, we hit the table to calculate the value of the SG function within 200, for a heap of stones to deal with all his successors.
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intsg[10005]= {0};BOOLvis[10005];intt,cas=1;void Get() {memset (SG,0,sizeof(SG)); sg[0]=0;  for(intI=1; i<= $; i++) {memset (Vis,0,sizeof(VIS));  for(intj=1; j<=i;j++)        {             for(intk=1; j+k<=i;k++)            {                if(j!=0&&k!=0&& (i-j-k)! =0)) Vis[sg[j]^sg[k]^sg[i-j-k]]=1;//Operation1            }        }         for(intj=0; j<i;j++)//Operation2vis[sg[j]]=1;  for(intx=0; ; X + +)            if(!Vis[x]) {Sg[i]=x;  Break; }    }}intMain () {Get();  for(intI=1; i<= -; i++) printf ("I:%d%d\n", I,sg[i]); return 0;}

Run the code and we'll get the following result:

Note that in this table

We can see that when n is a multiple of 8, the SG (n) =n+1; When (n+1) is a multiple of 8, SG (n) =n-1; otherwise SG (n) =n;

So we can tell the difference between the SG value and the winner.

#include <iostream>#include<cstdio>#include<cmath>#include<cstring>using namespacestd;intMain () {intT; scanf ("%d",&t);  while(t--)    {        intN; Long Longx,ans=0; scanf ("%d",&N);  for(intI=0; i<n;i++) {scanf ("%lld",&x); if(%8==0) ans=ans^ (x1); Else if((x+1)%8==0) ans=ans^ (x+1); Elseans=ans^x; }        if(ANS) puts ("First player wins."); Elseputs ("Second player wins."); }    return 0;}

HDU 5795 A Simple Nim (game)---2016 Hangzhou Electric Multi-school joint sixth field

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.