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;
}
相关文章推荐
- codeforces 604 E. Lieges of Legendre (sg函数)
- 【Codeforces Round 334 (Div 2)E】【博弈-SG函数】Lieges of Legendre n个数 每次取数-1或者变2x为k个x的博弈
- [Codeforces 603C]Lieges of Legendre
- CodeForces 603C/604E Lieges of Legendre SG函数
- Codeforces Round #334 (Div. 2) C. Lieges of Legendre
- Codeforces Round #334 E. Lieges of Legendre (组合游戏)
- CodeForces 603C Lieges of Legendre (SG函数)
- codeforces 603C. Lieges of Legendre sg函数
- Codeforces Round #334 E. Lieges of Legendre(SG)
- CF# 334 Lieges of Legendre
- Codeforces-An impassioned circulation of affection (尺取法)
- 【66.47%】【codeforces 556B】Case of Fake Numbers
- Codeforces 337 D Book of Evil(树形dp,两遍dfs)
- CodeForces - 359D D. Pair of Numbers
- codeforces 724G. Xor-matic Number of the Graph
- 【CODEFORCES】 C. Number of Ways
- codeforces 768 E Game of Stones(博弈)
- Codeforces 509c Sums of Digits 贪心
- Codeforces 872 B Maximum of Maximums of Minimums
- CodeForces 702B: Powers of Two(遍历、穷举)