HDU 5795 A Simple Nim ——(Nim博弈 + 打表)

  题意:在nim游戏的规则上再增加了一条,即可以将任意一堆分为三堆都不为0的子堆也视为一次操作。

  分析:打表找sg值的规律即可。

  感想:又学会了一种新的方法,以后看到sg值找不出规律的,就打表即可~

  打表代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <set>
 5 using namespace std;
 6 
 7 int sg[100+5];
 8 
 9 int main()
10 {
11     sg[0] = 0;
12     for(int i=0;i<=100;i++)
13     {
14         set<int> S;
15         for(int j=0;j<i;j++) S.insert(sg[j]);
16         for(int a=i-2;a>0;a--)
17         {
18             for(int b=a;b>0;b--)
19             {
20                 for(int c=a;c>0;c--)
21                 {
22                     if(a+b+c == i)
23                     {
24                         S.insert(sg[a]^sg[b]^sg[c]);
25                     }
26                 }
27             }
28         }
29         int now = 0;
30         while(S.count(now)) now++;
31         sg[i] = now;
32         printf("sg[%d] = %d
",i,now);
33         //if(sg[i] != i) printf("sg[%d] = %d
",i,now);
34     }
35 }

  AC代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 using namespace std;
 5 const int N = (int)1e6 + 5;
 6 
 7 int main()
 8 {
 9     int T;scanf("%d",&T);
10     while(T--)
11     {
12         int n;scanf("%d",&n);
13         int temp = 0,now;
14         for(int i=1;i<=n;i++)
15         {
16             scanf("%d",&now);
17             if(now%8 == 0)
18             {
19                 temp ^= (now-1);
20             }
21             else if(now%8 == 7)
22             {
23                 temp ^= (now+1);
24             }
25             else temp ^= now;
26         }
27         puts(temp?"First player wins.":"Second player wins.");
28     }
29 }
原文地址:https://www.cnblogs.com/zzyDS/p/5791020.html