[CF603C] Lieges of Legendre - SG函式
阿新 • • 發佈:2020-07-17
Description
有 \(n\) 堆石子,每次可以對一堆石子進行操作,如果當前石子是偶數,那麼可以選擇將這 \(2x\) 個石子分成 \(k\) 堆石子數為 \(x\) 的石子堆,還有一種沒有前提的操作是取走當前堆的一個石子,問先手贏還是後手贏。
Solution
若 \(x\) 為偶數,則 \(SG(x)=\text{mex}(\{ SG(\frac x 2)^k, SG(x-1) \})\)
若 \(x\) 為奇數,則 \(SG(x)=\text{mex} ( \{ SG(x-1) \} )\)
先暴力計算出 \(x \le 4\) 的情況,其餘滿足
\(x\) 為奇數時,\(SG(x)=0\)
\(x\) 為偶數時,若 \(k\) 也為偶數,則 \(SG(x)=1\),若 \(k\) 是奇數,則 \(SG(x)=\text{mex} (0,SG(\frac x 2))\)
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1000005; int mex(int x,int y=-1) { for(int i=0;i<2;i++) { if(x!=i && y!=i) return i; } return 2; } int a[N],n,k; int sg(int x) { if(x<=4) { if(x==0) return 0; if(x&1) { return mex(sg(x-1)); } else { if(k&1) return mex(sg(x/2),sg(x-1)); else return mex(0,sg(x-1)); } } else { if(x&1) { return 0; } else { if(k&1) return mex(0,sg(x/2)); else return 1; } } } signed main() { ios::sync_with_stdio(false); cin>>n>>k; int ans=0; for(int i=1;i<=n;i++) { cin>>a[i]; ans^=sg(a[i]); } cout<<(!ans?"Nicky":"Kevin")<<endl; }