LA5059 Playing With Stones

Playing With Stones

 大意:n堆石子,每堆a1,a2....an个,两人轮流操作,每次选一堆,减少至少一个至多石子数一般下取整个。不能拿的人输。给定n,a,问先手必胜/必败

打表求得一堆的sg函数:

0 1 0 2 1 3 0 4 2 5 1 6 3 7 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15

不难发现其中有‘’1 2 3 5.....15"

即sg(2n) = n

考虑sg(2n + 1) 为0 1 0 2 1 3 0 4 2 5 1 6 3 7

恰好是原数列,即sg(2n+1) = sg(n) = sg([(2n+1)/2])

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <vector>
 8 #include <cmath> 
 9 #define min(a, b) ((a) < (b) ? (a) : (b))
10 #define max(a, b) ((a) > (b) ? (a) : (b))
11 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
12 inline void swap(long long &a, long long &b)
13 {
14     long long tmp = a;a = b;b = tmp;
15 }
16 inline void read(long long &x)
17 {
18     x = 0;char ch = getchar(), c = ch;
19     while(ch < '0' || ch > '9') c = ch, ch = getchar();
20     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
21     if(c == '-') x = -x;
22 }
23 
24 const long long INF = 0x3f3f3f3f;
25 const long long MAXN = 100 + 10;
26 
27 long long t,n,a;
28 
29 long long sg(long long x)
30 {
31     if(x == 0) return 0;
32     return (x & 1) == 0 ? x/2 : sg(x/2);
33 }
34 
35 int main()
36 {
37     read(t);
38     for(;t;--t)
39     {
40         read(n);long long tmp = 0;
41         for(register long long i = 1;i <= n;++ i) 
42         {
43             read(a);
44             tmp ^= sg(a);
45         }
46         if(tmp) printf("YES
");
47         else printf("NO
");
48     }
49     return 0;
50 } 
LA55059
原文地址:https://www.cnblogs.com/huibixiaoxing/p/8309191.html