您的位置:首页 > 其它

POJ2975:Nim

2013-12-18 19:25 134 查看
Description

Nimisa2-playergamefeaturingseveralpilesofstones.Playersalternateturns,andonhis/herturn,aplayer’smoveconsistsofremoving
oneormorestonesfromanysinglepile.Playendswhenallthestoneshavebeenremoved,atwhichpointthelastplayertohavemovedisdeclaredthewinner.GivenapositioninNim,yourtaskistodeterminehowmanywinningmovesthereareinthat
position.

ApositioninNimiscalled“losing”ifthefirstplayertomovefromthatpositionwouldloseifbothsidesplayedperfectly.A“winningmove,”then,isamovethatleavesthegameinalosingposition.Thereisafamoustheoremthatclassifiesalllosing
positions.SupposeaNimpositioncontainsnpileshavingk1,
k2,…,knstonesrespectively;insuchaposition,thereare
k1+k2+…+knpossiblemoves.Wewriteeach
kiinbinary(base2).Then,theNimpositionislosingifandonlyif,amongallthe
ki’s,thereareanevennumberof1’sineachdigitposition.Inotherwords,theNimpositionislosingifandonlyifthe
xoroftheki’sis0.

Considerthepositionwiththreepilesgivenbyk1=7,
k2=11,andk3=13.Inbinary,thesevaluesareasfollows:

111
1011
1101


Thereareanoddnumberof1’samongtherightmostdigits,sothispositionisnotlosing.However,suppose
k3werechangedtobe12.Then,therewouldbeexactlytwo1’sineachdigitposition,andthus,theNimpositionwouldbecomelosing.Sinceawinningmoveisanymovethatleavesthegameinalosingposition,itfollowsthatremovingone
stonefromthethirdpileisawinningmovewhenk1=7,
k2=11,andk3=13.Infact,thereareexactlythreewinningmovesfromthisposition:namelyremovingonestonefromanyofthethreepiles.

Input

Theinputtestfilewillcontainmultipletestcases,eachofwhichbeginswithalineindicatingthenumberofpiles,1≤
n≤1000.Onthenextline,therearenpositiveintegers,1≤ki≤1,000,000,000,indicatingthenumberofstonesineachpile.Theend-of-fileismarkedbyatestcasewith
n=0andshouldnotbeprocessed.

Output

Foreachtestcase,writeasinglelinewithanintegerindicatingthenumberofwinningmovesfromthegivenNimposition.

SampleInput
3
71113
2
10000000001000000000
0

SampleOutput
3
0


与HDU1850一样输出先手能赢第一步有几种取法。。

一样的代码。。



#include<stdio.h>
#include<string.h>
#include<algorithm>
usingnamespacestd;

intmain()
{
intn,a[1005],i,j,cnt;
while(~scanf("%d",&n),n)
{
cnt=0;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
ints=0;
for(j=0;j<n;j++)
{
if(i!=j)
s^=a[j];
}
if(a[i]>s)
cnt++;
}
printf("%d\n",cnt);
}

return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: