这题是Lasker’s Nim.
Clearly the Sprague-Grundy function for the one-pile game satisfies g(0) = 0 and g(1) = 1. The followers of 2 are 0, 1 and (1,1), with respective Sprague-Grundy values of 0, 1, and 1⊕1 = 0. Hence, g(2) = 2. The followers of 3 are 0, 1, 2, and (1,2), with Sprague-Grundy values 0, 1, 2, and 1⊕2 = 3. Hence, g(3) = 4.
Continuing in this manner, we see
x 0 1 2 3 4 5 6 7 8 9 10 11 12...
g(x) 0 1 2 4 3 5 6 8 7 9 10 12 11...
We therefore conjecture that g(4k + 1) = 4k + 1,g(4k + 2) = 4k + 2,g(4k + 3) = 4k +4 and g(4k + 4) = 4k + 3, for all k ≥ 0.
代码如下:
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<iomanip> 5 #include<cmath> 6 #define in(x) scanf("%d",&x) 7 using namespace std; 8 int main(){ 9 int n,t,ans,k; 10 in(t); 11 while(t--){ 12 in(n); 13 ans=0; 14 for(int i=0;i<n;i++){ 15 in(k); 16 if(k%4==0) ans^=k-1; 17 else if(k%4==3) ans^=k+1; 18 else ans^=k; 19 } 20 puts(ans?"Alice":"Bob"); 21 } 22 return 0; 23 }