题目链接http://7xjob4.com1.z0.glb.clouddn.com/2e6726b559b7e18ea2f5402159885f8b
题目描述:There are stones numbered from 1 to N, lined up orderly in a single line with stone #1 at the left mostand stone #N at the right most. Udin and Ucok play alternately, and Udin plays first. In each turn,player selects exactly one stone among the existing one. The selected stone and its immediate adjacentstones to the left and right (if any), are thrown away. Player who cannot make any move loses thegame; in other words, player who makes the last move wins.
题意:
就是2人博弈取石子
每次取走一颗的时候要把周围的相邻两颗都取走,谁无法取石子谁就输了
这是一个Nim游戏
可以用NIM和(sg函数)和SG定理
SG函数:
sg(x) = mex { sg(x-2), sg(x-3), sg(1)XORsg(x-1-3), sg(2)XORsg(x-2-3) ...}
如果不理解可以参照大白书P138例题13(uva10561)
打印出结果以后就可以开始找规律了
发现 以34为循环,MOD34 结果为 4 8 20 24 28 的是必败
14和34是特例【没错,就是这个坑爹的特例】
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
const int maxn = 1005;
int sg[maxn];
int vis[maxn];
//先利用sg函数找到规律
void init()
{
memset(sg,0,sizeof(sg));
//初始状态
sg[0] = 0;
sg[1] = 1;
sg[2] = 1;
for(int i = 2 ; i < maxn ;i++)
{
memset(vis,0,sizeof(vis));
//对于所有i状态,如果他的i-2/i-3状态是必败的话,那状态i就是必胜状态
vis[sg[i-2]] = 1;
vis[sg[i-3]] = 1;
for(int j = 0;j<= i-3;j++)
{
vis[sg[j]^sg[i-j-3]] = 1;
//对于每一种sg值都遍历一遍
}
for(int j = 0 ;;j++)
{
if(!vis[j])//找出后继状态中mex(sg[x]);
{
sg[i] =j;
break;
}
}
if(sg[i]==0)
cout<<"i:"<<i<<" sg[i]"<<sg[i]<<endl;
}
}
int main()
{
//init();
//函数打印规律
int k;
int N;
int n;
cin>>N;
for(k=1;k<=N;k++)
{
cin>>n;
int t = n%34;
if(n==14||n==34)
cout<<"Case #"<<k<<": UCOK"<<endl;
else if(t==4||t==8||t==20||t==24||t==28)
cout<<"Case #"<<k<<": UCOK"<<endl;
else
cout<<"Case #"<<k<<": UDIN"<<endl;
}
return 0;
}