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

本文介绍了一个名为LiegesofLegendre的游戏,玩家通过修改游戏状态进行对决。文章详细解析了游戏规则及如何利用Sprague-Grundy函数确定最优策略下的胜者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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
  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. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值