您的位置:首页 > 其它

zoj3599 Game----K倍动态减法游戏

2012-09-16 10:43 260 查看
Game

TimeLimit:3Seconds
MemoryLimit:65536KB

AliceandBoddecidetoplayanewstonegame.AtthebeginningAliceputs
n(n>1)stones(outofNinall)onthetable.AliceandBobremovethestonesinturn.Ateachsteptheplayershouldremovesomenumberofstones.Thenumberofstonestheplayerremovedshouldbeatleastone,andcannotexceed
mtimesofthenumberofstonestheplayerremovedatthelaststep.Theplayerwhoremovesthelaststonewinsthegame.Alicealwaysplaysfirstandofcourseatthefirstturn,shecannotremoveallthestones.Alicewantstoknowhowmanypositive
integersnshecanchoosetowinthegameifbothplayersplayoptimally.

Sinceyou'reanaceprogrammer,Alicewantsyoutohelpher.

Input

Therearemultipletestcases.Thefirstlineofinputcontainsaninteger
T(0<T≤500)indicatingthenumberoftestcases.ThenTtestcasesfollow.

Eachtestcaseisalineof2integersmandN(0<m≤2012,1<
N<231)

Output

ForeachtestcaseoutputthenumberofpositiveintegersAlicecanchoosefor
ntomakeherwinthegame.

SampleInput

3
110
210
310

SampleOutput

6
5
4


注意:要用longlong,数组开太大卡内存~


[code]#include<iostream>
#include<cstdlib>
#include<stdio.h>
usingnamespacestd;
constintN=3000000;
longlonga
,b
;
#defineLLlonglong
intmain()
{
intt,k;
longlongn;
scanf("%d",&t);
while(t--)
{
scanf("%d%lld",&k,&n);
a[0]=b[0]=1;
inti=0,j=0;
while(a[i]<n)
{
i++;
a[i]=b[i-1]+1;
while(a[j+1]*k<a[i])
j++;
if(a[j]*k<a[i])b[i]=b[j]+a[i];
elseb[i]=a[i];
}
LLans;
if(a[i]==n)
ans=(LL)n-i-1;
else
ans=(LL)n-i;
cout<<ans<<endl;
}
}

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