《SG函数练习》

HDU1848.

筛出每个值的SG函数值。

然后根据xxx博弈的定理:多个堆的博弈结果为每个堆结果的异或值。

为0先手必败,否则先手必胜。

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e5+5;
const int M = 1e4;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){  
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

int f[1005],cnt = 0,SG[1005],S[1005];
void init()
{
    f[1] = 1,f[2] = 2,cnt = 2;
    for(rg int i = 3;f[i-1] <= 1000;++i) f[i] = f[i-1]+f[i-2],cnt = i;
    cnt--;
}
void slove(int x)
{
    for(rg int i = 1;i <= x;++i)
    {
        memset(S,0,sizeof(S));
        for(rg int j = 1;j <= cnt && f[j] <= i;++j) S[SG[i-f[j]]] = 1;
        for(rg int j = 0;;++j) if(!S[j]){SG[i] = j;break;}
    }
}
int main()
{
    init();
    slove(1000);
    int m,n,p;
    while(m = read(),n = read(),p = read(),m || n || p)
    {
        LL ma = SG[m]^SG[n]^SG[p];
        if(ma == 0) printf("Nacci
");
        else printf("Fibo
");
    }
    system("pause");
}
View Code

POJ2234;

这里可以取任意数量的数。然后还是多堆。

那么直接将整个数看成SG函数即可。

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e5+5;
const int M = 1e4;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){  
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

int main()
{
    IO;CT0;
    int m,x;
    while(cin >> m)
    {
        LL ans = 0;
        for(rg int i = 1;i <= m;++i) cin >> x,ans ^= x;
        if(ans == 0) printf("No
");
        else printf("Yes
");
    }
    system("pause");
}
View Code
原文地址:https://www.cnblogs.com/zwjzwj/p/13563450.html