尼姆博弈——对于n堆石子轮流取,先取完者胜,和这道题蛮像的
将这道题改造成尼姆博弈然后进行位运算
原题意为s个石子,取小于原有石子c的平方数个石子
转为每堆任意取小于find(s,c)个石子
int find(int s,int c)
{
int p=sqrt(s+0.0);
while(p+p*p>=s)
p--;
if(c>p)
return s-c;
else
return find(p,c);
}
这一段,有很多讲解的,看了。。半懂半不懂
任意数p,p+p*p要小于s的最大p
然后。。如果c>p,这时先行者可以在s-c中任意拿,return ;
如果没有,就递归,至于为什么用p作s继续递归。。。不造欸。。
就酱吧,如果以后想明白了再来
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<math.h>
using namespace std;
int find(int s,int c)
{
int p=sqrt(s+0.0);
while(p+p*p>=s)
p--;
if(c>p)
return s-c;
else
return find(p,c);
}
int main()
{
int count=1;
int n;
while(scanf("%d",&n)&&n)
{
int ans=0;
int s,c;
while(n--)
{
scanf("%d%d",&s,&c);
ans^=find(s,c);
}
printf("Case %d:\n",count++);
if(ans) puts("Yes");
else puts("No");
}
}