题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795
题意:
有两个小孩玩Nim ,给你N堆糖果,可以从任意一堆中拿走全部,也可以把一堆分成3份(保证每份都必须不少于1个).问谁会赢.
个人感想:
今天博弈居然拉下了,居然一点都没感觉,完全不会做… 这么简单,实在尴尬.我是听了学弟说了我才恍然大悟,我一直把Nim的精髓理解错了,而且总是顺理成章的就觉得这个是答案,其实不然…不过也是给我们队伍当头一棒,我们的知识点都是有缺漏的.虽然今天成绩不太好,但我们队得起自己的成绩,所以无所谓啦.这是一种磨练.
分析:Nim博弈
代码:
/* Author:GavinjouElephant
* Title:
* Number:
* main meanning:
*
*
*
*/
//#define OUT
#include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <vector>
#include <set>
#include <cstdlib>
#include <map>
#include <queue>
//#include<initializer_list>
//#include <windows.h>
//#include <fstream>
//#include <conio.h>
#define MaxN 0x7fffffff
#define MinN -0x7fffffff
#define Clear(x) memset(x,0,sizeof(x))
const int INF=0x3f3f3f3f;
typedef long long ll;
ll T;
ll x;
ll N;
ll sg(ll x)
{
if(x%8==0)return x-1;
if(x%8==7)return x+1;
return x;
}
int main()
{
#ifdef OUT
freopen("coco.txt","r",stdin);
freopen("lala.txt","w",stdout);
#endif
scanf("%I64d",&T);
while(T--)
{
scanf("%I64d",&N);
ll ans=0;
for(int i=0;i<N;i++)
{
scanf("%I64d",&x);
ans^=sg(x);
}
if(ans)printf("First player wins.\n");
else printf("Second player wins.\n");
}
return 0;
}
打表代码:
/* Author:GavinjouElephant
* Title:
* Number:
* main meanning:
*
*
*
*/
//#define OUT
#include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <vector>
#include <set>
#include <cstdlib>
#include <map>
#include <queue>
//#include<initializer_list>
//#include <windows.h>
//#include <fstream>
//#include <conio.h>
#define MaxN 0x7fffffff
#define MinN -0x7fffffff
#define Clear(x) memset(x,0,sizeof(x))
const int INF=0x3f3f3f3f;
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;
}