C. Josephus Problem

题目链接:http://www.bnuoj.com/v3/contest_show.php?cid=7095#problem/C

题目描述

 

The historian Flavius Josephus relates how, in the Romano-Jewish conflict of 67 A.D., the Romans took the town of Jotapata which he was commanding. Escaping, Josephus found himself trapped in a cave with 40 companions. The Romans discovered his whereabouts and invited him to surrender, but his companions refused to allow him to do so. He therefore suggested that they kill each other, one by one, the order to be decided by lot. Tradition has it that the means for affecting the lot was to stand in a circle, and, beginning at some point, count round, every third person being killed in turn. The sole survivor of this process was Josephus, who then surrendered to the Romans. Which begs the question: had Josephus previously practiced quietly with 41 stones in a dark corner, or had he calculated mathematically that he should adopt the 31st position in order to survive?

Now you are in a similar situation. There are n persons standing in a circle. The persons are numbered from 1 to n circularly. For example, 1 and n are adjacent and 1 and 2 are also. The count starts from the first person. Each time you count up to k and thekth person is killed and removed from the circle. Then the count starts from the next person. Finally one person remains. Given nand k you have to find the position of the last person who remains alive.

 

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains two positive integers n (1 ≤ n ≤ 105) and k (1 ≤ k < 231).

 

Output

For each case, print the case number and the position of the last remaining person.

 

Sample Input

Sample Input

Output for Sample Input

6

2 1

2 2

3 1

3 2

3 3

4 6

Case 1: 2

Case 2: 1

Case 3: 3

Case 4: 3

Case 5: 2

Case 6: 3

题意:给你一个n个人的环,每次从编号1开始数,数到第k个人就杀死,问你最后一个杀死的人的编号是多少
题解:约瑟夫环问题,对于任意n,k
      http://www.cnblogs.com/AllenDuane/p/3748203.html
#include<stdio.h>
int f(int n, int m)
{
int r = ;//即f(1)=0;
for(int i = ; i <= n; i++)
r = (r + m) % i;//即f(i)=[f(i-1)+m]%n;
return r + ; //即f(n)=1;
}
int main()
{
int t,n,k;
int oo=;
scanf("%d",&t);
while(t--) scanf("%d%d",&n,&k),printf("Case %d: %d\n",oo++,f(n,k));
return ;
}

代码

BNUOJ 13098 约瑟夫环问题的更多相关文章

  1. C#实现约瑟夫环问题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace orde ...

  2. C语言数组实现约瑟夫环问题,以及对其进行时间复杂度分析

    尝试表达 本人试着去表达约瑟夫环问题:一群人围成一个圈,作这样的一个游戏,选定一个人作起点以及数数的方向,这个人先数1,到下一个人数2,直到数到游戏规则约定那个数的人,比如是3,数到3的那个人就离开这 ...

  3. C语言链表实现约瑟夫环问题

    需求表达:略 分析: 实现: #include<stdio.h> #include<stdlib.h> typedef struct node { int payload ; ...

  4. javascript中使用循环链表实现约瑟夫环问题

    1.问题 传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第 ...

  5. tc 147 2 PeopleCircle(再见约瑟夫环)

    SRM 147 2 600PeopleCircle Problem Statement There are numMales males and numFemales females arranged ...

  6. HDU 3089 (快速约瑟夫环)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...

  7. 约瑟夫环(Josehpuse)的模拟

    约瑟夫环问题: 0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字,求出这个圆圈里剩下的最后一个数字. 这里给出以下几种解法, 1.用队列模拟 每次将前m-1个元 ...

  8. C++ 约瑟夫环问题

    约瑟夫环比较经典了 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直 ...

  9. 约瑟夫环的java解决

    总共3中解决方法,1.数学推导,2.使用ArrayList递归解决,3.使用首位相连的LinkedList解决 import java.util.ArrayList; /** * 约瑟夫环问题 * 需 ...

随机推荐

  1. IOS7 状态栏和 Navigation Bar重叠的问题解决

    一 Status bar重叠问题: 方法一:隐藏Status bar   在plist里面增加2个变量  Status bar is initially hidden  -> YES   Vie ...

  2. 在网页中引用DWG控件,交互绘图,和响应鼠标点击对象的方法

    在网页中引用DWG控件,交互绘图,和响应鼠标点击对象的方法 [MXDRAW CAD控件文档] 下面帮助的完整例子,在控件安装目录的Sample\Ie\iedemo.htm中. 1.      主要用到 ...

  3. 06Oracle Database 数据类型

    Oracle Database 数据类型 字符型 char(n)最大2000个字节 定长 nchar(n)最大2000个字节 变长 varchar2(n) 最大4000个字节 变长 nvarchar2 ...

  4. for循环,isinstance() 函数

    #isinstance()的运用 #练习: 求值总和以及平均值. str_list = [1,2,3,4,5,6,'a',7,8,9,'b',10,'c'] my_tal = 0 my_var = 0 ...

  5. idea之查看类的上下级继承关系

  6. js实现汉字中文排序的方法 例如:省市列表的排序

    localeCompare() 1.数组内的元素是中文字符串的简单排序 var arr = ['南京', '北京', '上海', '杭州', '深圳']; function sortChinese ( ...

  7. [C++] 化学方程式的格式化算法

    网上普遍使用的化学方程式的格式普遍如下 例: KMnO4+FeSO4+H2SO4=Fe2(SO4)3+MnSO4+K2SO4+H2O 要把化学方程式格式化,单单一个正则表达式是非常反人类的,故可选用 ...

  8. 使用yolo3模型训练自己的数据集

    使用yolo3模型训练自己的数据集 本项目地址:https://github.com/Cw-zero/Retrain-yolo3 一.运行环境 1. Ubuntu16.04. 2. TensorFlo ...

  9. 虚拟机上CentOS-6.9-x86_64系统安装教程

    最近想学学Linux系统如何使用,于是想用VM安装虚拟机学习一下. linux系统比较多,我这里用的是CentOS-6.9-x86_64 一.下载系统 下载地址:https://www.centos. ...

  10. lastpass密码管理工具使用教程

    现在移动互联网发展异常空气,无论访问哪个平台或者网站必须要注册账号,日子久了就会发现最痛苦的就是记住这些网站的密码.因为我们不可能将所有的网站都是设置同样的的账号密码,因为国内网站用户数据库被泄露的事 ...