Interesting Game SG函数 有一堆石子,每次将一堆石子分解成连续几个数的和,不能分解的人输。问先手胜,败。

本文介绍了一款涉及策略的游戏,玩家需要将初始的一堆石头通过特定规则拆分成若干堆以获得胜利。文章详细解释了游戏规则,并提供了一个算法实现,用于判断先手玩家是否能赢及最优的石头拆分策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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?

Input

The single line contains a single integer n (1 ≤ n ≤ 105).

Output

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).

Sample test(s)
input
3
output
2
input
6
output
-1
input
100
output
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值