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

来源:互联网 发布:淘宝卖家等级怎么升 编辑:程序博客网 时间:2024/06/06 20:14

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

标签: algorithm博弈
 378人阅读 评论(0) 收藏 举报
 分类:
E. 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).

Sample test(s)
input
2 13 4
output
Kevin
input
1 23
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
  1. /*====================================================== 
  2. # Author: whai 
  3. # Last modified: 2015-12-04 14:22 
  4. # Filename: e.cpp 
  5. ======================================================*/  
  6. #include <iostream>  
  7. #include <cstdio>  
  8. #include <vector>  
  9. #include <algorithm>  
  10. #include <cstring>  
  11. #include <string>  
  12. #include <cmath>  
  13. #include <set>  
  14. #include <map>  
  15. #include <queue>  
  16. #include <stack>  
  17.   
  18. using namespace std;  
  19.   
  20. #define LL __int64  
  21. #define PB push_back  
  22. #define P pair<int, int>  
  23. #define X first  
  24. #define Y second  
  25.   
  26. const int N = 1e5 + 5;  
  27.   
  28. int a[N];  
  29.   
  30. int get_sg(int x, int k) {  
  31.     bool vis[3] = {0};  
  32.     if(x == 0) return 0;  
  33.     if(x == 1) return 1;  
  34.     if(x >= 5 && x % 2 == 1) return 0;  
  35.     vis[get_sg(x - 1, k)] = 1;  
  36.     if(x % 2 == 0) {  
  37.         if(k % 2 == 0) vis[0] = 1;  
  38.         else vis[get_sg(x / 2, k)] = 1;  
  39.     }  
  40.     int ret = 0;  
  41.     while(vis[ret]) ++ ret;  
  42.     return ret;  
  43. }  
  44.   
  45. void gao(int n, int k) {  
  46.     int ans = 0;  
  47.   
  48.     for(int i = 0; i < n; ++i) {  
  49.         ans ^= get_sg(a[i], k);  
  50.     }  
  51.       
  52.     if(ans == 0) {  
  53.         puts("Nicky");  
  54.     } else {  
  55.         puts("Kevin");  
  56.     }  
  57. }  
  58.   
  59. /*int sg[105]; 
  60. bool vis[105]; 
  61.  
  62. void bf(int n, int k) { 
  63.     sg[0] = 0; 
  64.     for(int i = 1; i <= 100; ++i) { 
  65.         memset(vis, 0, sizeof(vis)); 
  66.         vis[sg[i - 1]] = 1; 
  67.         if(i % 2 == 0) { 
  68.             if(k % 2 == 0) vis[0] = 1; 
  69.             else vis[sg[i / 2]] = 1; 
  70.         } 
  71.         int tmp = 0; 
  72.         while(vis[tmp]) ++tmp; 
  73.         sg[i] = tmp; 
  74.     } 
  75.     for(int i = 0; i <= 100; ++i) { 
  76.         cout<<sg[i]<<' '; 
  77.     } 
  78.     cout<<endl; 
  79.     for(int i = 0; i <= 100; ++i) { 
  80.         cout<<get_sg(i, k)<<' '; 
  81.     } 
  82.     cout<<endl; 
  83. }*/  
  84.   
  85. int main() {  
  86.     int n, k;  
  87.   
  88.     while(scanf("%d%d", &n, &k) != EOF) {  
  89.         //bf(n, k);  
  90.         for(int i = 0; i < n; ++i) {  
  91.             scanf("%d", &a[i]);  
  92.         }  
  93.         gao(n, k);  
  94.     }  
  95.   
  96.     return 0;  
  97. }  
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 嘴巴里没有味道怎么办 嘴巴没味道应该怎么办 dota2更新已暂停怎么办 dota2信使被杀了怎么办 招聘招不到人怎么办 男友戾气太重怎么办 老公戾气太重怎么办 感觉自己戾气重怎么办 心中戾气很重怎么办 小孩戾气太重怎么办 剃刀龟咬伤人怎么办 头发烫坏了怎么办 淹脖子烂了怎么办 婴幼儿脖子淹了怎么办 军训要剪头发怎么办 军训不想剪头发怎么办 信息泄露被贷款怎么办 法院被执行布控怎么办 高考没过省控线怎么办 蜂刺留在身上怎么办 饥荒牛太多了怎么办 冰箱高频噪音大怎么办 家里有大蜘蛛怎么办 幸福小丈夫结局怎么办 功放音响电流声怎么办 燕子掌叶子变软怎么办 13孩子叛逆期怎么办 七岁孩子叛逆怎么办 丈夫出轨了妻子怎么办 老公不行我应该怎么办 老公性无能我该怎么办 丈夫不尊重妻子怎么办 宠物蝎子生气了怎么办 被蝎子蛰了怎么办 宠物蚂蚁太多了怎么办 手机百度云限速怎么办 手机暴风没字幕怎么办 暴风影音没字幕怎么办 负数发票作废了怎么办 怎么办英语四六假证 换了环境睡不着怎么办