官方题解就是直接SG函数求解,然后求的时候状压一下就行了
Then m integers
pj(1≤pj≤20)
followed, the position of each chess.
这个地方特别坏,总之读入比较和我想的不一样,要注意读入了。
关于SG函数:
如果一个游戏可以分解为若干个子游戏,每个子游戏可以用一个数字表示比如P。
P状态如果没有后续状态,他的SG函数值为0
否则,SG(P) 为所有的P的后续状态(比如a,b,c,d,e,f,g)中SG函数值没出现过的
比如sg(a)=1, sg(b)=2, sg(c)=3, sg(d)=0, sg(f)=10, sg(g)=11
那么sg(p)=4, 因为0,1,2,3,10,11.中间缺少4。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
int n;
LL a[2000];
LL SG[(1<<20) + 5];
void init()
{
scanf("%d", &n);
memset(a, 0, sizeof(a));
for (int i = 0; i != n; ++ i)
{
int m;
scanf("%d", &m);
while (m--)
{
int tmp;
scanf("%d", &tmp);
a[i] |= (1 << (20 - tmp));
}
}
}
void doit()
{
LL k=0;
for (int i = 0; i != n; ++ i)
{
k ^= SG[a[i]];
}
if (k ==0) printf("NO\n");
else printf("YES\n");
}
void pgbit(int k)
{
bitset<30>t = k;
cout << t << endl;
}
bool vis[30];
LL dp(int arg)
{
memset(vis, 0, sizeof(vis));
if (SG[arg] != -1) return SG[arg];
for (int i = 19; i >=1; --i)
{
if (arg & (1 << i))
{
int tmp = arg;
for (int j = i -1; j >= 0; -- j )
{
if (!(arg & (1 << j)))
{
tmp ^= ((1 << i) ^ (1 << j));
vis[SG[tmp]] = 1;
// if (arg==2 && i == 1) cout<<"@"<<" "<<tmp<<endl;
break;
}
}
}
}
for (int i = 0;i<=20;++i)
if (!vis[i])
{
SG[arg] = i;
break;
}
return SG[arg];
}
void getSG()
{
memset(SG, -1, sizeof(SG));
for (int i = 1; i<= 1<<20; i <<= 1)
SG[i - 1] = 0;
for (int i = 0; i <= (1<<20) - 1; i ++ )
dp(i);
//for (int i = 0; i <= 20; ++ i) cout<< i<<" "<<SG[i] << endl;
}
int main()
{
//cout<<"!"<<((1<<20) + 5)<<endl;
getSG();
int t;
scanf("%d", &t);
while (t--)
{
init();
doit();
}
return 0;
}