Two best friends Serozha and Gena play a game.
Initially there is one pile consisting of n stones on the table. During one move one pile should be taken and divided into an arbitrary number of piles consisting of a1 > a2 > ... > ak > 0 stones. The piles should meet the condition a1 - a2 = a2 - a3 = ... = ak - 1 - ak = 1. Naturally, the number of piles k should be no less than two.
The friends play in turns. The player who cannot make a move loses. Serozha makes the first move. Who will win if both players play in the optimal way?
The single line contains a single integer n (1 ≤ n ≤ 105).
If Serozha wins, print k, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game.
If Gena wins, print "-1" (without the quotes).
3
2
6
-1
100
8
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=100001;
int sg[maxn],flag[maxn],dv[maxn];
void init()
{
memset(sg,0,sizeof(sg));
dv[1]=dv[2]=1;
for(int i=3;i<maxn;i++)
{
//这里不能用memset(flag,0,sizeof(flag)) 否则要超时
int _min=(1<<28);
for(int j=2;;j++)
{
int mt=j*(j-1)/2;
if(mt>=i) break;
if((i-mt)%j==0)
{
int a=(i-mt)/j;
int cnt=0;
for(int k=a;k<=a+j-1;k++)
{
cnt^=sg[k];
}
flag[cnt]=i;//important
if(cnt==0)
{
_min=min(_min,j);
}
}
}
dv[i]=_min;
for(int j=0;;j++)
{
if(flag[j]!=i)//important
{
sg[i]=j;break;
}
}
}
}
int main()
{
init();
int n;
while(scanf("%d",&n)==1)
{
if(sg[n]==0) printf("-1\n");
else printf("%d\n",dv[n]);
}
return 0;
}
游戏策略分析
本文介绍了一款涉及策略的游戏,玩家需要将初始的一堆石头通过特定规则拆分成若干堆以获得胜利。文章详细解释了游戏规则,并提供了一个算法实现,用于判断先手玩家是否能赢及最优的石头拆分策略。
1664

被折叠的 条评论
为什么被折叠?



