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;
}