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

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

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

在MATLAB中,你可以创建一个函数来实现这个功能。首先,我们需要读取图像,然后使用`imwrite``imread`函数处理颜色通道,最后使用`imshow`显示结果。以下是详细的步骤函数示例: ```matlab function [imgSwapped] = swapGreenBlue(img) % 读取图像 originalImg = imread('your_image_file.jpg'); % 替换为你的图片文件名 % 检查图像是否为真彩色(RGB) if size(originalImg, 3) ~= 3 || strcmp(colorOrder(originalImg), 'truecolor') ~= 1 error('Input image must be a true color (RGB) image.'); end % 将图像转换为三通道数组 imgArray = mat2gray(originalImg); % 提取绿、蓝通道并交换它们 greenChannel = imgArray(:, :, 2); blueChannel = imgArray(:, :, 3); imgArray(:, :, 2) = blueChannel; imgArray(:, :, 3) = greenChannel; % 保存临时交换后的图像,因为imwrite不直接支持通道互换 tempFile = 'temp swapped.jpg'; imwrite(imgArray, tempFile); % 再次读取交换后的图像,因为imread可以自动识别颜色模式 swappedImg = imread(tempFile); % 清理临时文件 delete(tempFile); % 显示原图交换后的图像对比 subplot(1, 2, 1) imshow(originalImg); title('Original Image'); subplot(1, 2, 2) imshow(swappedImg); title('Image with Green and Blue Channels Swapped'); % 描述效果 disp('The original image has had its green and blue channels swapped. The red channel remains unchanged. This creates an interesting visual effect where the perceived color balance is altered.'); %
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值