題目鏈接:點擊打開鏈接
題目大意:兩個人玩一個遊戲,在n*20的棋盤上有若干個棋子,規定每個棋子只能向右移,並且當右邊有棋子的時候只能跳過那些棋子,同一個格子只能有一枚棋子,最後不能操作的人勝。
解題思路:算是簡單的SG函數求組合博弈的問題,思路早就有了,但是sg函數總是打不對,SG(x)=mex{SG(y)|y是x的後繼},mex表示集合中沒有出現的最小正整數,將每一組的sg值抑或就可以得到結果。
代碼:
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<ctime>
#include "cstdio"
#include "string"
#include "string.h"
using namespace std;
const int maxn = 3e6 + 50;
int sg[maxn];
int getBit(int x, int i)
{
return ((x >> i) & 1);
}
int getSuf(int x, int i)
{
x ^= (1 << i);
for (int j = i - 1;j >= 0;j--)
if (!getBit(x, j))
return (x | (1 << j));
return -1;
}
int getSG(int now)
{
//cout << now << endl;
if(sg[now]!=-1)
return sg[now];
int ans = 0;
bool vis[20] = { 0 };
for (int i = 0;(1 << i) <= now;i++)
{
if (getBit(now, i))
{
int suf = getSuf(now, i);
if (suf == -1)
continue;
int temp = sg[suf];
if (temp == -1)
{
temp = sg[suf] = getSG(suf);
}
vis[temp] = 1;
}
}
for (int i = 0; ;i++)
{
if (!vis[i])
{
ans = i;
break;
}
}
return sg[now] = ans;
}
int main()
{
memset(sg, -1, sizeof(sg));
sg[1] = 0;
for (int i = (1 << 20);i >= 1;i--)
if(sg[i]==-1)
sg[i] = getSG(i);
int T, n, num, temp;
scanf("%d", &T);
while (T--)
{
int ans = 0;
scanf("%d", &n);
while (n--)
{
int now = 0;
scanf("%d", &num);
for (int i = 0;i < num;i++)
{
scanf("%d", &temp);
now |= 1 << (20 - temp);
}
temp = getSG(now);
//cout << temp << endl;
ans ^= temp;
}
if (ans>0)
puts("YES");
else
puts("NO");
}
return 0;
}