HDOJ 5795 A Simple Nim (博弈Nim+sg函数)
2016-08-22 14:42
162 查看
A Simple Nim
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 195 Accepted Submission(s): 133
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 integer1≤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 integerss[0],s[1],....,s[n−1]
, representing heaps with s[0],s[1],...,s[n−1]
objects respectively.(1≤n≤10
6
,1≤s[i]≤10
9
)
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.
Author
UESTC
Source
2016 Multi-University Training Contest 6
思路:
这道题是一道典型的博弈论sg问题。
sg问题在这里有详解----->传送门
因为这个题数量级的范围是10的9次方级,所以没办法一个一个去求sg值,所以只能能过sg打表的形式来找出sg变化的规律,然后通过数学归纳总结出sg和x的一个关系式。再根据打表可以看出来规律,每当x到达8的倍数的时候,sg的值就等于x-1,如果x整除8等于7的时候,sg值就等于x+1,其他情况sg就等于x。然后就找到了sg与x之间的规律,那么就能推导所有的x与其所对应的sg之间的表达式。
至于sg值打表,先把vis数组全都初始化为0,然后遍历每个数的sg值vis[sg值出现过的数]都变为1,代表取石子的过程。然后将所有能拆分的都拆分,即用“^”来拆分sg值。然后当出现sg为0的时候,把其所作为sg的值所对应的那个第几个sg记录下来。所得sg打表如图:
然后就可以得到上面的规律啦。
代码:
[cpp] view
plain copy
//sg值打表程序
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
int sg[32];
bool vis[32];
int a[3];
int main()
{
for (int i=1; i<32; i++) {
memset(vis,0,sizeof(vis));
int j,k;
for (j=0; j<i; j++) vis[sg[j]]=1;
for (j=1; j<i; j++)
for (k=1; j+k<i; k++)
vis[sg[k]^sg[j]^sg[(i-k-j)]]=1;
for (j=0; j<32&&vis[j]; j++);
sg[i]=j;
}
for (int i=1; i<32; i++)
printf("%d %d\n",i,sg[i]);
return 0;
}
[cpp] view
plain copy
//主程序
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int find_sg(int x)
{
if(x%8==0)return x-1;
else if(x%8==7)return x+1;
return x;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int a,n,i,j,ans=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a);
ans=ans^find_sg(a);
}
if(ans==0)printf("Second player wins.\n");
else printf("First player wins.\n");
}
return 0;
}
相关文章推荐
- (HDU 5795)2016 Multi-University Training Contest 6 A Simple Nim (SG函数、博弈)
- hdu_5795_A Simple Nim(打表找规律的博弈)
- HDOJ 5795 A Simple Nim
- HDU-5795-A Simple Nim-博弈(SG函数)打表找规律
- HDU 5795 A Simple Nim 详解(SG打表找规律,博弈好题)
- HDU 5795 A Simple Nim【博弈】
- hdu 5795 A Simple Nim(2016 Multi-University Training Contest 6——博弈)
- hdu 5795 A Simple Nim 博弈sg函数
- HDU 5795 A Simple Nim (博弈 打表找规律)
- HDU 5795 A Simple Nim(博弈)
- HDU-5795 A Simple Nim(SG函数打表找规律)
- hdu 5795 A Simple Nim
- 【HDU5795】 A Simple Nim(博弈)
- 20140715 「博弈 - Nim(尼姆)博弈」 HDOJ 1850 Being a Good Boy in Spring Festival
- 20140715 「博弈 - Nim(尼姆)博弈」 HDOJ 2509 Be the Winner
- HDOJ 题目S-Nim(sg-博弈,模板)
- 20140715 「博弈 - Nim(尼姆)博弈」 HDOJ 1907 John
- HDOJ 1851 A Simple Game 博弈 SG函数中找规律
- sg函数+nim博弈+打表_______A Simple Nim(hdu 5795 2016多校第六场)
- hdu 5795 A Simple Nim (sg函数)