# hdu 1849Rabbit and Grass(一维nim游戏,sg函数)

Posted by 111qqz on Tuesday, September 22, 2015

# Rabbit and Grass

**Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3058    Accepted Submission(s): 2261 **

Problem Description

Input

Output

Sample Input

2 3 5 3 3 5 6 0

Sample Output

Rabbit Win! Grass Win!

Author

lcy

Source

ACM Short Term Exam_2007/12/13

``````/*************************************************************************
> File Name: code/hdu/1849.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年09月22日 星期二 20时37分46秒
************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#include<cctype>
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define ms(a,x) memset(a,x,sizeof(a))
#define lr dying111qqz
using namespace std;
#define For(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef double DB;
const int inf = 0x3f3f3f3f;
int m;
int main()
{
#ifndef  ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while (scanf("%d",&m)!=EOF&&m)
{
int sum = 0 ;
for ( int i = 0 ;i < m ; i++)
{
int x;
scanf("%d",&x);
sum ^= x;
}
//cout<<sum<<endl;
if (sum)
{
puts("Rabbit Win!");
}
else
{
puts("Grass Win!");
}
}

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
``````

``````/*************************************************************************
> File Name: code/hdu/1849.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年09月22日 星期二 20时37分46秒
************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#include<cctype>
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define ms(a,x) memset(a,x,sizeof(a))
#define lr dying111qqz
using namespace std;
#define For(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef double DB;
const int inf = 0x3f3f3f3f;
const int N=1E3+7;
bool vis[N];
int sg[N];
int m;

void sg_init()
{
for ( int i = 1 ; i < N ; i++)
{
ms(vis,false);
for ( int j = 0 ; j <= i ; j++)
vis[sg[i-j]] = true;

for ( int j = 0 ; ; j++)
if (!vis[j])
{
sg[i] = j;
break;
}
}
}
int main()
{
#ifndef  ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif

sg_init();
while (scanf("%d",&m)!=EOF&&m)
{
int sum = 0 ;
for ( int i = 0 ;i < m ; i++)
{
int x;
scanf("%d",&x);
sum ^= sg[x];
}
//cout<<sum<<endl;
if (sum)
{
puts("Rabbit Win!");
}
else
{
puts("Grass Win!");
}
}

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
``````

「真诚赞赏，手留余香」