2 seconds
256 megabytes
standard input
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:
- 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.
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 the name of the winning player, either "Kevin" or "Nicky" (without quotes).
2 1
3 4
Kevin
1 2
3
Nicky
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,k,表示有n堆石子,石子数目分别为a1.a2...an,
每次一个人可以在任意一堆拿掉一个石子,或者,如果某堆石子是偶数个的话,设为2*x个,可以分为k堆x个的石子
取得最后一颗石子即为胜利。
问先手赢还是后手赢。
分析:显然,根据sg函数的定义,sg函数定义为当前状态第一个达不到的状态。
其中,石子游戏用石子数目当作当前状态。
如果有多堆石子,则将它们异或起来。
显然,如果k是偶数的话,考虑一堆的情况,那么奇数必胜,偶数必败,这个可以用数学归纳法证明。那么如果有多堆的情况就考虑一下奇数堆和偶数堆的数目即可。
如果k为奇数,就按照sg函数的定义做就好。
/**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} int n, k, state; inline int Sg(int x)
{
if(k & )
{
if(x < )
{
if(x == ) return ;
if(x == || x == ) return ;
return ;
}
if(x & ) return ;
return Sg(x / ) == ? : ;
}
else
{
if(x == ) return ;
if(x == ) return ;
return (x & ) ^ ;
}
} inline void Input()
{
scanf("%d%d", &n, &k);
for(int i = ; i < n; i++)
{
int x;
scanf("%d", &x);
state ^= Sg(x);
}
} inline void Solve()
{
puts(state ? "Kevin" : "Nicky");
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}