洛谷 P2575 高手过招(SG函数)

题意:略

思路:感觉这个题有点暴力啊,(主要是我的做法太暴力了),打出(1<<20)的所有sg函数值,但是在POJ的阶梯博弈例题中好像去不能这样,好像网上也有按照阶梯博弈的解法,(还不是很理解阶梯博弈),这里充分了利用了异或的性质,挺有趣的,看了网上聚聚的代码(传送门

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
inline LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int sg[(1<<20)+7];
int vis[55];

void getsg(int x)
{
    memset(vis,0,sizeof(vis));
    int pre=0;
    for(int i=1;i<=20;i++){
        if(x&(1<<i-1)){
            if((x&(1<<i-2))==0&&i>=2)vis[sg[x^(1<<i-1)^(1<<i-2)]]=1;
            else if(pre)vis[sg[x^(1<<i-1)^(1<<pre-1)]]=1;
        }
        else pre=i;
    }
    for(int i=0;i<50;i++){
        if(!vis[i]){
            sg[x]=i;return ;
        }
    }
}

int a[1005];
int main()
{
    memset(sg,0,sizeof(sg));
    for(int i=1;i<(1<<20);i++)getsg(i);
    int T=read();
    while(T--){
        int n=read();
        int ans=0;
        for(int i=1;i<=n;i++){
            int x=read();
            int res=0;
            for(int j=1;j<=x;j++){
                int as=read();
                res+=(1<<(20-as));
            }
            ans^=sg[res];
        }
        if(ans)puts("YES");
        else puts("NO");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lalalatianlalu/p/9861516.html