hdu 3951 Coin Game(对称博弈)

来源:互联网 发布:淘宝二手商品3c认证 编辑:程序博客网 时间:2024/06/04 08:28

Coin Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1756    Accepted Submission(s): 1018


Problem Description
After hh has learned how to play Nim game, he begins to try another coin game which seems much easier.

The game goes like this: 
Two players start the game with a circle of n coins. 
They take coins from the circle in turn and every time they could take 1~K continuous coins. 
(imagining that ten coins numbered from 1 to 10 and K equal to 3, since 1 and 10 are continuous, you could take away the continuous 10 , 1 , 2 , but if 2 was taken away, you couldn't take 1, 3, 4, because 1 and 3 aren't continuous)
The player who takes the last coin 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 is a number T(1<=T<=100), represents the number of case. The next T blocks follow each indicates a case.
Each case contains two integers N(3<=N<=109,1<=K<=10).
 

Output
For each case, output the number of case and the winner "first" or "second".(as shown in the sample output)
 

Sample Input
23 13 2
 

Sample Output
Case 1: firstCase 2: second
 

Author
NotOnlySuccess

题意:

      一个圈的硬币,两个人轮流取。每个人只能取连续的1~k个。比如,不能取1,3,4.  给你几个硬币和k,求谁赢?

解:

 可以这样想,刚开始先手的可以取k个石子,那么如果k>=m,那么先手必胜,这是毋庸置疑的。

然后如果先手不能一次性取完,那么后者可以取1-k中的某个数,使得剩下的分为两堆数量相同的石子,那么先手在一堆中取石子(无论他怎么取,取多少),那么后手就在另外的一个石子堆中模仿先手的动作,那么当先手把某堆石子取完时,后手也一定能够把另外一堆石子取完,即全部取完,后手必胜。
但是有一个特例,就是k=1,那我们只能取1个石子,这样的话,如果当初的堆中石子数量为奇数时先手必胜,反之则反。
这题巧妙的把一堆分成了两堆,而且巧妙地避开了连续取硬币这一个麻烦。。。原来一直想怎么解决连续取硬币的问题。。。而且他的思考方向只是两堆,在两堆分出来的新的堆不管。。。 我一开始想的就是分了许多“独立的堆”,这样“从大局”总的看成两堆,完美的解决了这个问题。。

#include <cstdio>#include <algorithm>using namespace std;int main(){    int t, Case = 0, n, k;    scanf("%d", &t);    while(t--)    {        scanf("%d%d", &n, &k);        if(k >= n)        {            printf("Case %d: first\n", ++Case);        }        if(k == 1)        {            if(n % 2)                printf("Case %d: first\n", ++Case);            else                printf("Case %d: second\n", ++Case);                continue;        }        if(k < n)        {             printf("Case %d: second\n", ++Case);        }    }    return 0;}


1 0
原创粉丝点击