您的位置:首页 > 其它

codeforces 604 E. Lieges of Legendre (sg函数)

2017-12-26 00:06 375 查看


codeforces 604 E. Lieges of Legendre (sg函数)

标签: algorithm博弈
2015-12-06 21:29 378人阅读 评论(0) 收藏 举报


 分类:

博弈(5) 


版权声明:by whai

E. Lieges of Legendre

time limit per test
 2 seconds

memory limit per test
 256 megabytes

input
 standard input

output
 standard output

Kevin and
4000
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:

Remove a single cow from a chosen non-empty pile.

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 n, k,
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).

Sample test(s)

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.

思路:

挺常规的一道sg函数的题目,打个表就清楚多了

[cpp] view
plain copy

/*====================================================== 

# Author: whai 

# Last modified: 2015-12-04 14:22 

# Filename: e.cpp 

======================================================*/  

#include <iostream>  

#include <cstdio>  

#include <vector>  

#include <algorithm>  

#include <cstring>  

#include <string>  

#include <cmath>  

#include <set>  

#include <map>  

#include <queue>  

#include <stack>  

  

using namespace std;  

  

#define LL __int64  

#define PB push_back  

#define P pair<int, int>  

#define X first  

#define Y second  

  

const int N = 1e5 + 5;  

  

int a
;  

  

int get_sg(int x, int k) {  

    bool vis[3] = {0};  

    if(x == 0) return 0;  

    if(x == 1) return 1;  

    if(x >= 5 && x % 2 == 1) return 0;  

    vis[get_sg(x - 1, k)] = 1;  

    if(x % 2 == 0) {  

        if(k % 2 == 0) vis[0] = 1;  

        else vis[get_sg(x / 2, k)] = 1;  

    }  

    int ret = 0;  

    while(vis[ret]) ++ ret;  

    return ret;  

}  

  

void gao(int n, int k) {  

    int ans = 0;  

  

    for(int i = 0; i < n; ++i) {  

        ans ^= get_sg(a[i], k);  

    }  

      

    if(ans == 0) {  

        puts("Nicky");  

    } else {  

        puts("Kevin");  

    }  

}  

  

/*int sg[105]; 

bool vis[105]; 

 

void bf(int n, int k) { 

    sg[0] = 0; 

    for(int i = 1; i <= 100; ++i) { 

        memset(vis, 0, sizeof(vis)); 

        vis[sg[i - 1]] = 1; 

        if(i % 2 == 0) { 

            if(k % 2 == 0) vis[0] = 1; 

            else vis[sg[i / 2]] = 1; 

        } 

        int tmp = 0; 

        while(vis[tmp]) ++tmp; 

        sg[i] = tmp; 

    } 

    for(int i = 0; i <= 100; ++i) { 

        cout<<sg[i]<<' '; 

    } 

    cout<<endl; 

    for(int i = 0; i <= 100; ++i) { 

        cout<<get_sg(i, k)<<' '; 

    } 

    cout<<endl; 

}*/  

  

int main() {  

    int n, k;  

  

    while(scanf("%d%d", &n, &k) != EOF) {  

        //bf(n, k);  

        for(int i = 0; i < n; ++i) {  

            scanf("%d", &a[i]);  

        }  

        gao(n, k);  

    }  

  

    return 0;  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: