hdu 3951 Coin Game (博弈创建对称局势)

Coin Game

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

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

2
3 1
3 2

Sample Output

Case 1: first
Case 2: second

题意:给你一串石子,石子有编号,且是环,即1和 n 相连,每次可以取连续的 1k 个石子,谁最后取完谁赢。

思路:在游戏刚开始的时候,所有石子为一条环,在先手不可能一次取完的情况下,先手取之后,石子变成一条链,然后后手只需要创建一个对称的局势即可获得胜利,即把这条链分成两条相等的链,我们发现,在 k2 的时候,后手一定可创建出对称局势,此时先手必输;当 k=1 的时候对称局势对游戏无影响,直接判奇偶即可。

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-10-10.39 Wednesday
File Name    : D:\MyCode\2016-8月\2016-8-10.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int main()
{
    int t,cas=0;cin>>t;
    while(t--)
    {
        int n,k;cin>>n>>k;
        printf("Case %d: ",++cas);
        if(n<=k)
            printf("first\n");
        else
        {
            if(k==1)
            {
                if(n%2==0) printf("second\n");
                else printf("first\n");
            }
            else
                printf("second\n");
        }
    }
    return 0;
}

你可能感兴趣的:(博弈)