Bzoj 1874: [BeiJing2009 Wintercamp] Take stone game [NIM game SG function]

Source: Internet
Author: User

Little H and Little Z are playing a stone game. The rule of stone game is this, each person can take a few stones from a heap, each time the number of stones to be limited, who can not take Stones will lose the game. Small h first to operate, he wants to ask you if he has a winning strategy, if so, the first step how to take the stone.

N≤10 ai≤1000

Bare SG function AH

However, I do not even ask the SG function, WA a while later, according to other code changes to find the Vis common ...

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespaceStd;typedefLong Longll;Const intn= the, m=1005; inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;} intN,a[n],m,b[n];intSg[m],vis[m];intDfsintu) {//printf ("Dfs%d\n", u);    if(sg[u]!=-1)returnSg[u];//printf ("y\n");    BOOLVis[n];memset (Vis,0,sizeof(VIS));  for(intI=1; i<=m && b[i]<=u;i++) Vis[dfs (U-b[i])]=1;  for(intI=0;; i++)if(!vis[i]) {sg[u]=i; Break;} returnsg[u];}intMain () {//freopen ("in", "R", stdin);memset (sg,-1,sizeof(SG)); sg[0]=0; N=read ();  for(intI=1; i<=n;i++) a[i]=read (); M=read ();  for(intI=1; i<=m;i++) b[i]=read (); Sort (b+1, B +1+m); intsum=0;  for(intI=1; i<=n;i++) sum^=DFS (A[i]); if(sum!=0) {puts ("YES"); intflag=0;  for(intI=1; I<=n &&!flag;i++)             for(intj=1; J<=m && b[j]<=a[i];j++)                 if((Sum^dfs (A[i]) ^dfs (a[i]-b[j]) = =0) {printf ("%d%d\n", I,b[j]); flag=1; Break;} }ElsePuts"NO");}

Bzoj 1874: [BeiJing2009 Wintercamp] Take a stone game [NIM Games sg function]

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.