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

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

2008
Asia Regional Beijing

题意:

一堆石子两个人轮流抢,每次至少要取走一个。先取的人第一次可以取任意多个,但是不能全部取完,之后每个人取石子时,能取的数目最多不能超过对手刚取石子数的k倍(k为给定常量)。取走最后一个石子的人算赢。

在两人都以最优策略进行游戏时,先手要么必胜要么必负,必胜还是必负取决宇一开始有多少石子,本题就是告诉你开始的石子数目,你需要判断先手必胜还是必负,然后对先收必胜的情况,要算出先手一方第一次至少需要取多少个石子。

代码:

 1 #include<stdio.h>
2 #define maxn 1000000
3 int n , k ;
4
5 int a[maxn+1];
6 int r[maxn+1];
7 void solve()
8 {
9 int i,j;
10 a[0]=a[0]=0;
11 for(i=1,j=0 ;i<=maxn;i++)
12 {
13 a[i]=r[i-1]+1;
14 while(j+1<i && a[j+1]*k<a[i])
15 j++;
16 r[i]=a[i]+r[j];
17 if(r[i]>=n) break;
18 }
19 if(i>maxn)
20 {
21 printf("un solvable\n");
22 return ;
23 }
24 if(a[i]==n)
25 {
26 printf("lose\n");
27 return ;
28 }
29 for( ; i>=1 ;i--)
30 {
31 if(n==a[i])
32 {
33 printf("%d\n",n);
34 return ;
35 }
36 else if(n>a[i]) n-=a[i];
37 }
38 printf("logic error\n");
39 }
40 int main()
41 {
42 int ca ,cc=0;
43 scanf("%d",&ca);
44 while(ca-->0)
45 {
46 scanf("%d %d",&n,&k);
47 printf("Case %d: ",++ cc);
48 solve();
49 }
50 return 0;
51 }

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

时间: 2024-12-22 07:52:51

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

[hdu-5795]A Simple Nim 博弈 尼姆博弈 SG函数打表找规律

[题目]题目链接 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more int

杭电 1536(尼姆博弈)

S-Nim <span size="+0"><strong><span style="font-family:Arial;font-size:12px;color:green;FONT-WEIGHT: bold">Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4479 

Matrix Game(尼姆博弈)

A - Matrix Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Given an m x n matrix, where m denotes the number of rows and n denotes the number of columns and in each cell a pile of stones is gi

hdu 1907 尼姆博弈

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3745    Accepted Submission(s): 2116 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

hdu 1850 Being a Good Boy in Spring Festival (尼姆博弈)

Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4658    Accepted Submission(s): 2781 Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场悄悄给爸爸买

Being a Good Boy in Spring Festival(尼姆博弈)

Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 51 Accepted Submission(s): 45   Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场悄悄给爸爸买个小礼物主动地

最强头脑决定战中的尼姆博弈

最近看了一个日本综艺——最强头脑王决定战,顿觉以前看过的国内烧脑节目,类似“一站到底”之流与之相比真是相形见绌.“一站”说到底只是比谁题库背得熟,而日本这个真的是记忆.计算.观察.推理等各种能力的综合考察,比赛选手们各显神通,观众如我看得也是如痴如醉. 其中比赛到第二轮时有一道题如下:有四堆不同色的棋子,每堆分别有3.5.6.7枚,由选手和电脑开始依次从其中取出一些棋子,每次只可取同一堆的任意枚,如此交替直至取完所有棋子,谁取到最后一枚棋子谁输. 当时水上小哥不假思索地取子很轻松就赢了,让评委们

nyoj 取石子(三)(尼姆博弈的定理运用)

取石子(三) 时间限制:1000 ms |  内存限制:1000 KB 难度:6 描述 小王喜欢与同事玩一些小游戏,今天他们选择了玩取石子. 游戏规则如下:共有N堆石子,已知每堆中石子的数量,两个人轮流取子,每次只能选择N堆石子中的一堆,取一定数量的石子(最少取一个),取过子之后,还可以将该堆石子中剩下的任意多个石子中随意选取几个放到其它的任意一堆或几堆上.等哪个人无法取子时就表示此人输掉了游戏.注意,一堆石子没有子之后,就不能再往此处放石子了. 假设每次都是小王先取石子,并且游戏双方都绝对聪明

hdu 1849(Rabbit and Grass) 尼姆博弈

Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3864    Accepted Submission(s): 2919 Problem Description 大学时光是浪漫的,女生是浪漫的,圣诞更是浪漫的,但是Rabbit和Grass这两个大学女生在今年的圣诞节却表现得一点都不浪漫:不去逛商场,不去逛