HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)

时间:2021-04-05 07:54:52

A simple stone game

                                                                                                      Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

                                                                                                             Total Submission(s): 312    Accepted Submission(s): 167

Problem Description
After he has learned how to play Nim game, Mike begins to try another stone game which seems much easier.
The game goes like this: Two players start the game with a pile of n stones. They take stones from the pile in turn and every time they take at least one stone. The one who goes first can take at most n-1 stones for his first move. From then on a player can take at most k times as many stones as his opponent has taken last time. For example, if one player take m stones in his turn, then the other player can take at most k × m stones next time. The player who takes the last stone wins the game. Suppose that those two players always take the best moves and never make mistakes, your job is to find out who will definitely win the game.
 
Input
The first line contains a integer t, indicating that there are t test cases following.(t<=20).
Each test case is a line consisting of two integer n and k.(2<=n<=10^8,1<=k<=10^5).
 
Output
For each test case, output one line starting with “Case N: ”, N is the case number. And then, if the first player can ensure a winning, print the minimum number of stones he should take in his first turn. Otherwise, print "lose". Please note that there is a blank following the colon.
 
Sample Input
5
 
16 1
 
11 1
 
32 2
 
34 2
 
19 3
 
Sample Output
Case 1: lose
Case 2: 1
Case 3: 3
Case 4: lose
Case 5: 4
 
Source
 
题意:
一堆石子两个人轮流抢,每次至少要取走一个。先取的人第一次可以取任意多个,但是不能全部取完,之后每个人取石子时,能取的数目最多不能超过对手刚取石子数的k倍(k为给定常量)。取走最后一个石子的人算赢。
在两人都以最优策略进行游戏时,先手要么必胜要么必负,必胜还是必负取决宇一开始有多少石子,本题就是告诉你开始的石子数目,你需要判断先手必胜还是必负,然后对先收必胜的情况,要算出先手一方第一次至少需要取多少个石子。
代码:
HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)
 #include<stdio.h>
#define maxn 1000000
int n , k ; int a[maxn+];
int r[maxn+];
void solve()
{
int i,j;
a[]=a[]=;
for(i=,j= ;i<=maxn;i++)
{
a[i]=r[i-]+;
while(j+<i && a[j+]*k<a[i])
j++;
r[i]=a[i]+r[j];
if(r[i]>=n) break;
}
if(i>maxn)
{
printf("un solvable\n");
return ;
}
if(a[i]==n)
{
printf("lose\n");
return ;
}
for( ; i>= ;i--)
{
if(n==a[i])
{
printf("%d\n",n);
return ;
}
else if(n>a[i]) n-=a[i];
}
printf("logic error\n");
}
int main()
{
int ca ,cc=;
scanf("%d",&ca);
while(ca-->)
{
scanf("%d %d",&n,&k);
printf("Case %d: ",++ cc);
solve();
}
return ;
}