[Flokirie的测试赛]Roy&October之取石子 【sg函数】

题目

SG函数->博弈论 随记(SG函数)
看完之后还一脸蒙蔽的话可以尝试一下这篇论文 算法合集之《由感性认识到理性认识——透析一类搏弈游戏的解答过程》

这道题大概是打表找出规律,当然弄懂这类Nim游戏才是最重要的

经历了一天的学习终于懂了,然而感觉头发都要掉光了

代码(打表和最终输出都在里面)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;
const int N=500010;
int prime[N], cntp, is_not[N], n, sg[N], hash[N];

void getp()
{
    is_not[1]=1;
    for(int i=2; i <= n; i++)
    {
        if(!is_not[i]) prime[cntp++]=i;
        for(int j=0; j < cntp && i*prime[j] <= n; j++)
        {
            if(is_not[i*prime[j]]) break;
            is_not[i*prime[j]]=1;
        }
    }
}

void getsg()
{
    for(int i=1; i <= n; i++)
    {
        memset(hash, 0, sizeof(hash));
        hash[sg[i-1]]=1;
        for(int j=0; prime[j] <= i && j < cntp; j++)
        {
            int now=prime[j];
            while(now <= i) {hash[sg[i-now]]=1; now*=prime[j];}
        }
        for(int j=0; j <= n; j++) if(!hash[j]) {sg[i]=j;break;}
    }
}

int read(){
    int out=0, f=1; char c=getchar(); while(c < '0' || c > '9') {if(c == '-') f=-1; c=getchar();}
    while(c >= '0' && c <= '9') {out=(out<<1)+(out<<3)+c-'0'; c=getchar();}
    return out*f;
}

int main()
{
    int t=read();
    //getp();
    //getsg();
    //for(int i=1; i <= n; i++) if(!sg[i]) printf("N\n"); else printf("Y\n");
    for(int i=1; i<=t; i++)
    {
        int x=read();
        if(x%6) printf("October wins!\n");
        else printf("Roy wins!\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值