Codeforces Round #334 (Div. 1)C. Lieges of Legendre

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 2·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

Copy

2 1
3 4

output

Copy

Kevin

input

Copy

1 2
3

output

Copy

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.

分析:先直接跑一个SG函数打表

对于奇数堆,其后继只有x-1。

对于偶数堆:

当k为偶数时,分成偶数个x/2的堆,考虑其对称性,那么这个后继SG应为0。

当K为奇数时,后继应为x/2。

再加上x-1的后继,直接打表找规律。

打表函数:

int sg[10004];
int n,k;
int sg_dfs(int x) {
    if(sg[x]!=-1)return sg[x];
    if(x==0)return 0;
    if(x&1)
    {
        if(sg_dfs(x-1))return sg[x]=0;
        else return sg[x]=1;
    }
    else
    {
        bool vis[3];
        memset(vis,0,sizeof(vis));
        vis[sg_dfs(x-1)]=1;
        if(k&1)vis[sg_dfs(x/2)]=1;
        else vis[0]=1;
        for (int i = 0; i < 3; ++i) {
            if(!vis[i])return sg[x]=i;
        }
    }
    
}

可以发现,当x较大时,无论k的奇偶,奇数都为0,那么将SG函数简化一下可以得到下面:

int n,k;
int sg_dfs(int x) {
    if(k&1)
    {
        if(x==1)return 1;
        else if(x==2)return 0;
        else if(x==3)return 1;
        if(x==4)return 2;
        if(x&1)return 0;
        if(sg_dfs(x/2)==1)return 2;
        else return 1;
    }
    else
    {
        if(x==1)return 1;
        else if(x==2)return 2;
        else return (!(x&1));
    }
}

此函数复杂度为Log(n)

那么直接暴力跑一遍即可。

#include "bits/stdc++.h"

using namespace std;
int n, k;

int sg_dfs(int x) {
    if (k & 1) {
        if (x == 1)return 1;
        else if (x == 2)return 0;
        else if (x == 3)return 1;
        if (x == 4)return 2;
        if (x & 1)return 0;
        if (sg_dfs(x / 2) == 1)return 2;
        else return 1;
    } else {
        if (x == 1)return 1;
        else if (x == 2)return 2;
        else return (!(x & 1));
    }
}

int main() {
    cin >> n >> k;
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        int x;
        cin >> x;
        ans ^= sg_dfs(x);
    }
    if (ans)puts("Kevin");
    else puts("Nicky");
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/89424797