POJ 1012 Joseph 题解

Joseph,cpp 约瑟福环用c语言解决,同时是数据结构的实验题 立即下载

POJ 1012 Joseph 题解

题目解读:

The Joseph’s problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, …, n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

本道题目的背景是约瑟夫环问题。约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3,4…n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依次规律重复下去,直到圆桌周围的人全部出列。通常解决这类问题时我们把编号从0—n-1,最后结果+1即为原问题的解。
在本道题中对约瑟夫环问题作了一个简单的变形,通过求解最小的报数m,来使得所有的bad guy先于good guy被淘汰。

解决算法

我们在这里假设环的编号从0开始,以下的计算与公式推导均满足这一点。
注意观察到报数m具有延续性,对于n个人而言,第一轮约瑟夫环淘汰了一个人后,剩下的n-1个人其实又组成了一个新的约瑟夫环,唯一区别的就是两个约瑟夫环中的成员编号是不同的。因此,我们可以理解为这里需要解决的是不同编号序列之间的一个映射关系,即将第i+1轮中被淘汰的人的编号映射到第i轮中对应的编号序列。这与动态规划思想中状态转移的思想有相近的地方,因此考虑使用DP算法。现在先给出状态转移方程:

ans[0
约瑟夫joseph问题源码 Description The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved. Suppose that there are k good guys and k bad guys (n = 2*k). In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy. Input The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 8. Output The output file will consist of separate lines containing m corresponding to k in the input file. Sample Input 3 4 0 Sample Output 5 30 立即下载

相关推荐

poj 1012代码

北大poj1012题的代码,已经AC,请放心下载

基于FPGA的DDS信号发生器

之前的博客讲到了DDS的基本原理,现在用Verilog代码实现DDS,能够产生四种波形,方波,三角波,正弦波,锯齿波,用按键来控制频率和选择波形。其中按键消抖模块来自小梅哥的FPGA自学笔记设计与验证这本书。 module DDS_Module( Clk, Rst_n, EN, Fword, Key1, Pword, DA_Clk, DA_Data ); input Clk...

qq_43534451的博客 9957

Ansys Zemax实战:5步搞定衍射光学元件(DOE)设计,附赠超透镜(metalens)避坑指南

本文详细介绍了使用Ansys Zemax进行衍射光学元件(DOE)和超透镜(metalens)设计的5步实战流程。从基础光学系统搭建到相位面设计验证,再到制造准备与实测对比,全面解析了OpticStudio中的关键技巧和常见避坑指南,帮助工程师高效完成复杂光学元件设计。

orange的博客 631

Atlas 300I Pro推理卡性能深度测试:对比业界同类产品的能效比与算力表现

本文对昇腾Atlas 300I Pro推理卡进行了深度性能评测,重点对比了其与业界同类产品的能效比与算力表现。实测数据显示,该卡在运行BERT等模型时展现出高达39.6句子/秒/瓦的卓越能效比,电力成本可降低40%以上,同时其达芬奇架构和硬件加速特征检索能力,为高密度在线推理和向量检索场景提供了强大支撑。

weixin_29031161的博客 585

K8S证书过期解决办法之替换证书

k8s证书过期解决方案之替换证书

q_hsolucky的博客 1万+

【lua实战】lua中pairs和ipairs的区别

很久以前,我在使用lua的过程中,对于pairs和ipairs的理解还处于表层,认为我了解的就是全部。 ipairs就是对表中元素进行顺序排序,pairs就是对表中元素进行随机排序。

weixin_42206314的博客 1659

RT-Thread Studio 使用笔记(七)| 配合STM32CubeMX添加裸机驱动(以ADC为例)

1. 配置STM32CubeMX 2. 复制stm32xxxx_hal_msp.c文件 将 CubeMx 生成的代码 stm32f7xx_hal_msp.c 函数复制到 RT-Thread Studio 生成的工程中,并参与工程编译: 由于我们并没有使用 CubeMx 生成的工程,所以这里需要将 stm32f7xx_hal_msp.c 文件中 #include “main.h” 替换为 #i...

Mculover666的博客(嵌入式) 9639

Matlab并行运算

今天搞了一下matlab的并行计算,效果好的出乎我的意料。 本来CPU就是双核,不过以前一直注重算法,没注意并行计算的问题。今天为了在8核的dell服务器上跑程序才专门看了一下。本身写的程序就很容易实现并行化,因为beamline之间并没有考虑相互作用。等于可以拆成n个线程并行,要是有550核的话,估计1ms就算完了。。。 先转下网上找到的资料。 一、Matlab并行计算原理梗概 Matl

wenyusuran的专栏 1万+

RK3588&RK3576实现 HW-ID DTB(动态加载不同的Kernel DTB)功能

RK平台的U-Boot⽀持检测硬件上的GPIO或者ADC状态动态加载不同的Kernel DTB,暂称为HW-ID DTB(Hardware id DTB)功能。

Wynter 851

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

上一篇: PAT 1004 Counting Leaves
下一篇: POJ 1753 Flip Game 题解
ComeTender
博客等级 码龄11年 2粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值