Ervin and Joker(简单博弈)

题目

Ervin and Joker are playing a game with rocks.

Initially, there are N rocks, indexed from 1 to N. In one move, the player chooses at least 1 and at most K consecutively indexed rocks (all of them should not have been removed) and removes them from the game.

Ervin always starts the game, and then Ervin and Joker take turns making moves. The player who is unable to make a move (because all rocks are removed) loses.

Given N,K, find who is going to win the game (assuming they are smart and are playing optimally).

Input
The first line contains two integers N,K(0≤N≤106,1≤K≤106).

Output
Print a name (“Ervin” or “Joker”, without the quotes) — the person who is going to win the game.

Examples
Input
1 1
Output
Ervin
Input
9 3
Output
Ervin

思路

本题的关键就是找出一种必胜的方法,题目要求只能移动连续的石子,所以考虑第一个人(Ervin)的最优取法,再不限制一次取走多少石子个数的情况下(即不限制K的情况下),首先取走中间所有石子中间的1个石子,把整个石子序列分成两部分(如果只有两个石子,那么就全部取走)。
因为只能取连续的石子序列,所以分成两部分后,无论对方怎么取,都会留下一部分给自己在下一次取,然后针对每段石子序列依然采用相同的取法,使得该石子序列分成两部分,这样一来,就形成了“复读机”型局势,只要保证还剩的石子序列数是偶数,自己就不会输。
如何保证石子序列数是偶数?如果对方操作后,序列数不变,自己取序列的末端或首端的石子,此时序列数不变,仍是偶数;若对方操作后,序列数增加,自己可将另一序列分成两半(取中间的石子),如果不能分成两半(如2,3—>2,1,1),那就取走整个序列,留下的序列数仍是偶数;若对方操作后,序列数减少,自己也可让序列数减少,或将某一序列分成两半。这样自己就立于不败之地了。
而自己输的情况只有两种,第一种是“N=0”;第二种是“K=1”时,自己每次只能取走1个石子,所以,结局取决于n的奇偶性,具体代码如下:

代码

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>

const int N=5e5+5;

using namespace std;
//注意到本题Joker获胜的情况只有两种。
int main()
{
    int N,K;
    scanf("%d%d",&N,&K);
    if(N==0){
        printf("Joker\n");
    }
    else if(K==1){
        if(N%2==0){
            printf("Joker\n");
        }
        else{
            printf("Ervin\n");
        }
    }
    else{
        printf("Ervin\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值