[原创]HDU 5795 A simple Nim [SG]【博弈】

2016-08-04 21:56:25 Tabris_ 阅读数:589


博客爬取于2020-06-14 22:43:59
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/52123533


题目连接 : http://acm.hdu.edu.cn/showproblem.php?pid=5795

-----------------------------------------------------------.
A Simple Nim

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 170 Accepted Submission(s): 111

Problem Description
Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.

Input
Intput contains multiple test cases. The first line is an integer 1≤T≤100, the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],…,s[n−1], representing heaps with s[0],s[1],…,s[n−1] objects respectively.(1≤n≤106,1≤s[i]≤109)

Output
For each test case,output a line whick contains either"First player wins.“or"Second player wins”.

Sample Input
2
2
4 4
3
1 2 4

Sample Output
Second player wins.
First player wins.

-------------------------------------------.

题目大意 : 有n堆糖果 两个人 轮班来从一堆中取任意不为0个石子或者把其中某一堆分成三堆(这时候不能取石子) 拿走最后一个石子的人赢 问 先手赢还是后手赢

解题思路:

//////////////***********************//////////////////
其实本次博客并不是写题解的 只是用来发泄一下弱鸡的自己 (淡然后面肯定有题解...)

做题的时候 首先看到s[i]<1e9 就放弃了用SG函数的思想来解决此问题 更没想打表 更更没想到打表找规律这种事情 于是 各种姿势各种WA 最后看到题解的我眼泪掉下来 明明很简单的 但还是GG了 其实还是对知识点的不了解 之前学习SG函数的时候就没讲SG函数吃透 才会整场只A了1001 还是在队友优化了题目之后才解决的  1002也和本题一样   想到了Lucas定理+容斥原理 可硬是没写出来 还是对容斥原理的理解不够 甚至还想用某图论算法来解决它
说白了就是菜 但这也终于发挥了多校的作用 毕竟300买的账号。。 对于算法了解多了固然好 但是每一个都要吃透 否则就是 明明应该AC的题目却WA … 行了 不发牢骚了

----------------------------------------------.
心情实在压抑 直接贴的网上大神代码

打表的代码


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int g[100];
bool f[100];

void init(int n)
{
memset(f,false,sizeof(f));
for(int i=0;i<n;i++)
{f[g[i]]=true;}

for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
if((i+j+k)==n)
{




f[(g[i]^g[j]^g[k])]=true;
}
}
}
}
int i;
for(i=0;;i++)
{
if(f[i]==false){break;}
}
g[n]=i;
printf("%d %d\n",n,i);
//cout<<"---->"<<endl;
}
int main()
{
# ifdef OUT
freopen("coco.txt","r",stdin);
freopen("lala.txt","w",stdout);
# endif
g[0]= 0;
memset(g,0,sizeof(g));
for(int i=1;i<=20;i++)
{
init(i);
}

return 0;
}

根据输出很容易看到 sg[x%8==0]=x-1;sg[x%8==7]=x+1;

最终的AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  int main(){
// freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--){
int n,a;
scanf("%d",&n);
int ans=0;
for(int i=0;i<n;i++){
scanf("%d",&a);
if(a%8==0){
ans^=a-1;
}else if(a%8==7)
ans^=a+1;
else
ans^=a;
}
if(ans!=0){
printf("First player wins.\n");
}else{
printf("Second player wins.\n");
}
}
return 0;
}