AcWing 894 拆分nim遊戲(求sg函數)

在這裏插入圖片描述

思路:問你放兩堆規模更小的石子,sg(bi,bj)=sg(bi)^sg(bj)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define space putchar(' ')
#define enter putchar('\n')
typedef pair<int,int> PII;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=1e5+10;


ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*(b/gcd(a,b));
}

template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
        write(x / 10);
    putchar('0' + x % 10);
}
int n,k;

int f[N],a[N];
int sg(int x)
{
    if(f[x]!=-1)
        return f[x];
    unordered_set<int> S;
    for(int i=0; i<x; i++)
    {
      for(int j=0;j<=i;j++)
      {
          S.insert(sg(i)^sg(j));
      }
    }

    for(int i=0;; i++)
        if(!S.count(i))
            return f[x]=i;

}



int main()
{
    memset(f,-1,sizeof f);
    read(n);
    int ans=0;
    for(int i=0; i<n; i++)
    {
        int x;
        read(x);
        ans^=sg(x);
    }
    if(ans)
        puts("Yes");
    else
        puts("No");

}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章