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为给定常量)。取走最后一个石子的人算赢。
在两人都以最优策略进行游戏时,先手要么必胜要么必负,必胜还是必负取决宇一开始有多少石子,本题就是告诉你开始的石子数目,你需要判断先手必胜还是必负,然后对先收必胜的情况,要算出先手一方第一次至少需要取多少个石子。
代码:
 #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 ;
}

HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)的更多相关文章

  1. 尼姆博弈扩展形式(一): 限定每次取物的上限。NYOJ-135,难度5~~~

    取石子(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 http://acm.nyist.net/JudgeOnline/problem.php?pid=135 描述 小 ...

  2. Being a Good Boy in Spring Festival 尼姆博弈

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  3. LightOJ 1247 Matrix Game (尼姆博弈)

    A - Matrix Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submi ...

  4. Light OJ 1253 Misere Nim (尼姆博弈(2))

    LightOJ1253 :Misere Nim 时间限制:1000MS    内存限制:32768KByte   64位IO格式:%lld & %llu 描述 Alice and Bob ar ...

  5. POJ 2234 Matches Game (尼姆博弈)

    题目链接: https://cn.vjudge.net/problem/POJ-2234 题目描述: Here is a simple game. In this game, there are se ...

  6. codeforces - 15C Industrial Nim(位运算+尼姆博弈)

    C. Industrial Nim time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...

  7. hdu----(1849)Rabbit and Grass(简单的尼姆博弈)

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 1849(Rabbit and Grass) 尼姆博弈

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 4315 Climbing the Hill (阶梯博弈转尼姆博弈)

    Climbing the Hill Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Su ...

随机推荐

  1. poj 1502 最短路+坑爹题意

    链接:http://poj.org/problem?id=1502 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  2. TCP三次握手及四次挥手详细 转

    一.TCP报文格式        TCP/IP协议的详细信息参看<TCP/IP协议详解>三卷本.下面是TCP报文格式图: 图1 TCP报文格式 上图中有几个字段需要重点介绍下:      ...

  3. js addEventListener attachEvent

    attachEvent方法,为某一事件附加其它的处理事件.(不支持Mozilla系列) addEventListener方法 用于 Mozilla系列 举例: document.getElementB ...

  4. Tuning 简介

    典型的不好的设计: 破坏了系统的可扩展性(韧性) Applications requiring significant concurrency management as user populatio ...

  5. js学习笔记-编写高效、规范的js代码-Tom

    编写高效.规范的js代码: 1.变量命名空间问题,尽量使用局部变量,防止命名冲突(污染作用域中的全局变量):全局空间命名的变量可以在对应的文档域任意位置中使用window调用. 2.尽量使用单var定 ...

  6. Python学习(16)File(文件)方法

    Python File(文件) 方法 file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读 ...

  7. JQuery Ajax 的简单使用

    简单判断用户名存在不存在,如果数据库存在此用户名,提示不能注册 前台代码如下: <!DOCTYPE html> <html lang="en"> <h ...

  8. Linux 中如何卸载已安装的软件

    Linux 中如何卸载已安装的软件. Linux软件的安装和卸载一直是困扰许多新用户的难题.在Windows中,我们可以使用软件自带的安装卸载程序或在控制面板中的“添加/删除程序”来实现.与其相类似, ...

  9. HBase分享会议笔记

    今天参加了一个关于HBase的分享,有一些内容是之前的知识的补充. 之前关于Hadoop家族,包括HBase的内容,可以参考:http://www.cnblogs.com/charlesblc/p/6 ...

  10. POJ 3260 多重背包+完全背包

    前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...