「SP122」STEVE - Voracious Steve 解题报告

本文介绍了一个关于甜甜圈的游戏策略问题,Steve和Digit通过制定最优策略来争夺尽可能多的甜甜圈。游戏规则是在限定条件下轮流取甜甜圈,目标是吃到最多的甜甜圈。文章提供了详细的解题思路和C++代码实现。

SP122 STEVE - Voracious Steve

题意翻译

Problem

Steve和他的一个朋友在玩游戏,游戏开始前,盒子里有 n个甜甜圈,两个人轮流从盒子里抓甜甜圈,每次至少抓 1个,最多抓m个。

最后一次将当盒子的甜甜圈抓完的人是这一轮游戏胜利者,他可以将所有抓到的甜甜圈吃完,另外一个人是这轮的失败者,他抓到的所有甜甜圈要重新放到盒子里。

下一轮游戏由上一轮的失败者开始抓,游戏继续。直到若干轮后,所有的甜甜圈被吃完,游戏结束。

游戏的目标是吃到尽量多的甜甜圈。游戏最开始,由Steve先抓,两个人均采用最优策略,请问Steve最多可以吃到多少甜甜圈。

Input Data

第一行输入t,表示有t组测试数据

第2~t行,两个整数n和m(1≤m≤n≤100)

Output Data

一行,一个整数,表示Steve最多可以吃到多少个甜甜 圈。

题目描述

Steve and Digit bought a box containing a number of donuts. In order to divide them between themselves they play a special game that they created. The players alternately take a certain, positive number of donuts from the box, but no more than some fixed integer. Each player's donuts are gathered on the player's side. The player that empties the box eats his donuts while the other one puts his donuts back into the box and the game continues with the "loser" player starting. The game goes on until all the donuts are eaten. The goal of the game is to eat the most donuts. How many donuts can Steve, who starts the game, count on, assuming the best strategy for both players?

Task

Write a program that:

  • reads the parameters of the game from the standard input,
  • computes the number of donuts Steve can count on,
  • writes the result to the standard output.

输入输出格式

输入格式:

The input begins with the integer t, the number of test cases. Then t test cases follow.

For each test case the first and only line of the input contains exactly two integers n and m separated by a single space, 1 <= m <= n <= 100 - the parameters of the game, where n is the number of donuts in the box at the beginning of the game and m is the upper limit on the number of donuts to be taken by one player in one move.

输出格式:

For each test case the output contains exactly one integer equal to the number of donuts Steve can count on.

输入输出样例

输入样例#1:

1
5 2

输出样例#1:

3

思路

\(a[all][one][tho]\)表示总共有\(all\)个甜甜圈,现在轮到的人已经取了\(one\)个甜甜圈,另一个人取了\(tho\)个甜甜圈,现在轮到的人最多能取到的甜甜圈数。

我们用记搜方式实现。

\(all=0\)时,直接返回0。

\(all\not=0\)时,我们枚举取的个数,从中选出最大值。

一些边界、细节请看代码。

代码

#include<bits/stdc++.h>
using namespace std;

int n, m;
int a[205][205][205];

int SG( int all, int one, int tho ){
    if ( all == 0 ) return 0;
    if ( a[all][one][tho] ) return a[all][one][tho];
    int ans(0);//ans为找出的最大值
    for ( int i = 1; i <= min( m, all - one - tho ); ++i ){
        if ( one + tho + i == all ) ans = max( ans, all - SG( tho, 0, 0 ) );//取完了,把没取到最后一个的人的甜甜圈拿来继续取
        else ans = max( ans, all - SG( all, tho, one + i ) );//没取玩,继续取
    }
    return a[all][one][tho] = ans;//记录答案
}

int main(){
    int T;
    scanf( "%d", &T );
    while( T-- ){
        memset( a, 0, sizeof a );//每次的m都不同,所以别忘了初始化
        scanf( "%d%d", &n, &m );
        printf( "%d\n", SG( n, 0, 0 ) );
    }
    return 0;
}

转载于:https://www.cnblogs.com/louhancheng/p/10239276.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值