codeforces 603C. Lieges of Legendre(博弈+SG函数)

9 篇文章 0 订阅
1 篇文章 0 订阅
C. Lieges of Legendre
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kevin and Nicky Sun have invented a new game called Lieges of Legendre. In this game, two players take turns modifying the game state with Kevin moving first. Initially, the game is set up so that there are n piles of cows, with the i-th pile containing ai cows. During each player's turn, that player calls upon the power of Sunlight, and uses it to either:

  1. Remove a single cow from a chosen non-empty pile.
  2. Choose a pile of cows with even size x (x > 0), and replace it with k piles of x cows each.

The player who removes the last cow wins. Given nk, and a sequence a1, a2, ..., an, help Kevin and Nicky find the winner, given that both sides play in optimal way.

Input

The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109).

The second line contains n integers, a1, a2, ... an (1 ≤ ai ≤ 109) describing the initial state of the game.

Output

Output the name of the winning player, either "Kevin" or "Nicky" (without quotes).

Examples
input
2 1
3 4
output
Kevin
input
1 2
3
output
Nicky
Note

In the second sample, Nicky can win in the following way: Kevin moves first and is forced to remove a cow, so the pile contains two cows after his move. Next, Nicky replaces this pile of size 2 with two piles of size 1. So the game state is now two piles of size 1. Kevin then removes one of the remaining cows and Nicky wins by removing the other.


删除n堆奶牛
两个玩家每次操作可以有两种决策:
(1)选择其中一堆奶牛,删去其中一只。
(2)选择奶牛数为2*x(x>0)的一堆,将这一堆变换为k堆,每堆奶牛数都为x。
整个游戏的SG函数也就是每堆奶牛的SG函数的Nim和,
决策(2)也是一样,变换后的状态的SG函数即k个SG(x)的Nim和,
那么比较容易发现:


一、当k为偶数时:k堆奶牛总的SG函数都为0,所以初始奶牛数为偶数的奶牛堆的
SG函数至少为1,事实上由于奇数除了1外的SG函数都是0,所以偶数除了0和2外
SG函数都是1,只有奶牛数为2的奶牛堆的SG函数是2。


二、当k为奇数时:k堆奶牛总的SG函数都为SG(x),手算一下会发现,奇数除了1
和3外,SG函数都为0。那么假设E(E>4)为偶数,而且SG(x)为0的话,SG(E)就
为1,同理若SG(x)为1的话,SG(E)就为2,若SG(x)为2,SG(E)就为1。所以它是
以1--2--1--...交替变换的,故现在就只需要找到方法将SG(E)递推或逆推出来
了,方法很多,我的方法先特判前几个特殊数据后,将E = X*(2^cnt)(X为奇数)
中的cnt求出来后,cnt为偶数,SG(E)就为2,反之为1。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define FOP2 freopen("data1.txt","w",stdout)
#define inf_LL 4223372036854775807
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

int n, k;
int a[maxn];

int solve1(int n)
{
    if(n == 0) return 0;
    if(n == 1) return 1;
    if(n == 2) return 2;
    if(n&1) return 0;
    else return 1;
}

int solve2(int n)
{
    if(n == 0) return 0;
    if(n == 1) return 1;
    if(n == 2) return 0;
    if(n == 3) return 1;
    if(n == 4) return 2;
    if(n == 6) return 2;
    if(n&1) return 0;
    int cnt = 0;
    while(n > 6 && n%2 == 0)
    {
        n/=2;
        cnt++;
    }
    if(cnt&1) return 1;
    else return 2;
}

int main()
{
    while(~scanf("%d%d", &n, &k))
    {
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        int ans, sg;
        for(int i = 1; i <= n; i++)
        {
            if(k&1) sg = solve2(a[i]);
            else sg = solve1(a[i]);
            ans = i==1 ? sg : ans^sg ;
        }
        if(ans) printf("Kevin\n");
        else printf("Nicky\n");
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值