HDU 57242016多校Contest 1 B题【SG函数,题目input比较猥琐】

官方题解就是直接SG函数求解,然后求的时候状压一下就行了



Then m integers  pj(1pj20)  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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值