您的位置:首页 > 其它

URAL 1023 Buttons(巴什博弈水题)

2014-08-05 20:45 357 查看

1023.Buttons

Timelimit:2.0second
Memorylimit:64MB

Background

Asyousurelyalreadyknow,YekaterinburghasgottenitsrighttoholdTheSummerOlympicGamesofthe2032.ItisplannedthatitwillbeallowedtoRussiaasacountry-organizertoemendaprogramofthegamesabit.So,inordertoimprovethecommandresultithasbeendecidedtoreplacethecompetitioningymnasticsbythecompetitioninthenewgame"Buttons".

Therulesofthegameareverysimple.There’sasmallheapofKbuttonsbeforetwoplayers.Theplayersinturnstakebuttonsfromtheheap,moreover,atatimeonecantakeanumberofbuttonsfrom1uptoL.Theonewhotakesthelastbuttonisthewinner.

TherulesoftheOlympicGameswillbeabitharderthenusual.Theone,whoistomakeafirststepaccordingtoalot,hasanopportunitytofixanumberKwiththefollowingrestrictiontoit:3≤K≤108(thatistheexactnumberofbuttonsthathasbeenpreparedfortheOlympictournament).TheplayerwhoistomakethesecondstepfixesanumberLthatsatisfiesthefollowingconditions2≤L<K.

Problem

Averycrucialtaskisgiventoyourteam:youaretowriteaprogramthatshouldhelpthesecondplayertomakehischoice.Inotherwords,givenanumberKyourprogramistofindanumberLthatguarantiesavictorytothesecondplayerwithapropergameofbothsides.

So,forinstance,thereareonlythreebuttonsintheheap,thechoiceL=2providesforthevictoryofthesecondplayer.Really,ifthefirstplayertakesonlyonebuttonathisturn,thesecondonewins,takingthetwolastbuttons.Onthecontrary,ifthefirstonetakestwobuttons,thesecondonewins,takingthelastbutton.

Input

Thestandardinputconsistsofoneline,whichcontainsanonlyintegernumberK—anumberofbuttonsintheheap,thathasfixedthefirstplayerathisturn.

Output

TothestandardoutputyouaretowritetheonlynumberL—themaximalnumberofbuttonsthatcanbetakenatatimewhichprovidesforthevictoryofthesecondplayer.IfthereareseveralthosenumbersL,youshouldwritetheleast.Iftherearenosuchnumbers,youaretowrite0tothestandardoutput.

Sample

inputoutput
3

2


题意:总共K个纽扣,最多取L个,问L是多少能保证一定是第二个选手赢,求最小的L。

典型的巴什博弈啦~

#include<iostream>
#include<cstring>
usingnamespacestd;
intK,L;
intmain(){
while(~scanf("%d",&K)){
intjilu;
intfalg=false;
for(inti=3;i<=K;i++){
if(K%i==0){
falg=true;
printf("%d\n",i-1);
break;

}
}
if(!falg)
printf("0\n");
}
return0;
}



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