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:
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
SampleOutput
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;
}
相关文章推荐
- [poj2975]Nim
- 《挑战程序设计竞赛》4.2.2 游戏必胜策略-Nim与Grundy数 POJ2975 1704 2311 3537 2315 CF138D(2)
- [poj2975]Nim_博弈论
- ZOJ3067 POJ2975 Nim,经典NIM游戏
- 【POJ2975】Nim (博弈)
- POJ2975 & ZOJ3067 Nim [基础Nim博弈]输出方法
- poj2975 Nim 博弈
- poj2975--Nim
- POJ2975 Nim
- 【POJ2975】Nim 博弈
- POJ2975(nim输出走法总数)
- poj2975 Nim(经典博弈)
- [POJ2975] Nim(Nim游戏,博弈)
- 【Nim游戏】POJ2975[Nim]题解
- 【poj2975】Nim
- POJ2975(Nim游戏)
- POJ2975(Nim博弈)
- NIM:一排石头的游戏
- 博弈论的一点资料,关于NIM和SG函数
- poj 3840 (nim博弈)