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. mongodb .net driver

    1.介绍 The official MongoDB .NET Driver provides asynchronous interaction with MongoDB. Powering the d ...

  2. 卡尔曼滤波— Constant Velocity Model

    假设你开车进入隧道,GPS信号丢失,现在我们要确定汽车在隧道内的位置.汽车的绝对速度可以通过车轮转速计算得到,汽车朝向可以通过yaw rate sensor(A yaw-rate sensor is ...

  3. Date Picker Calendar For Oracle Forms 6i

    Giving date picker calendar option to user for date type fields in Oracle Forms. I am providing you ...

  4. DevOps到底是什么?

    本篇将讨论DevOps到底包含什么,今后的运维工程师应该朝什么方向努力.

  5. java运行期类型鉴定

    运行期类型识别?RTTI? 假如我们有一个基类的引用,这个引用也可以作为子类的引用嘛,现在我们想知道这个引用的类型到底是啥? 当从子类到基类之后有很多的信息都会丢失掉,比如有一个人类的对象可以看成普遍 ...

  6. [Effective Java]第四章 类和接口

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. 49个jquery代码经典片段

    49个jquery代码经典片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助你又快又好地 ...

  8. 保留ip: Reserved IP addresses

    Reserved IP addresses From Wikipedia, the free encyclopedia     In the Internet addressing architect ...

  9. 【Todo】单例模式各种实现方式及并发安全

    Java 40道面试题不错:http://www.tuicool.com/articles/VRVFZb 其中有一道题目: 单例模式的线程安全性 老生常谈的问题了,首先要说的是单例模式的线程安全意味着 ...

  10. bignum 大数模板

    今天无意间看到一个很好的大数模板,能算加.减.乘.除等基本运算,但操作减法的时候只能大数减小数,也不支持负数,如果是两个负数的话去掉符号相加之后再取反就可以了,一正一负比较绝对值大小,然后相减.我借用 ...