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
  1. #include<stdio.h>
  2. int f(int n, int m)
  3. {
  4. int r = ;//即f(1)=0;
  5. for(int i = ; i <= n; i++)
  6. r = (r + m) % i;//即f(i)=[f(i-1)+m]%n;
  7. return r + ; //即f(n)=1;
  8. }
  9. int main()
  10. {
  11. int t,n,k;
  12. int oo=;
  13. scanf("%d",&t);
  14. while(t--) scanf("%d%d",&n,&k),printf("Case %d: %d\n",oo++,f(n,k));
  15. return ;
  16. }

代码

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. ERwin 正向工程

    1.物理模型带字段备注 COMMENT ON 将模型切换至 physical 模式选择 Model ---> Domain Dictionary , 在 tab 标签中,切换至comment 然 ...

  2. 梦想CAD控件安卓控件事件

    MxDrawActivity.commandEvent 命令调用事件. 参数 说明 int iCommand 命令ID,这个ID用户自已来取的,只要多个命令ID不重复就可以 代码实现如下: publi ...

  3. 【Hadoop】一、HDFS简介及基本概念

      当需要存储的数据集的大小超过了一台独立的物理计算机的存储能力时,就需要对数据进行分区并存储到若干台计算机上去.管理网络中跨多台计算机存储的文件系统统称为分布式文件系统(distributed fi ...

  4. 「 HDU P2089 」 不要62

    和 HDOJ 3555 一样啊,只不过需要多判断个 ‘4’ 我有写 3555 直接去看那篇吧 这里只放代码 #include <iostream> #include <cstring ...

  5. idea+maven配置log4j详解

    经过上一篇的讲解,知道了实现log4j打印日志依赖的jar包共3个,在pom.xml中加入相关依赖: <!-- 添加log4j日志相关jar包:共3个jar--> <!-- http ...

  6. 第一章 Linux命令行简介

    1 Linux系统命令操作语法的格式 命令_[参数选项]_[文件或路径]    其中 _ 至少一个空格    如:rm -f /etc/hosts    其中/etc/hosts完整路径不带空格   ...

  7. Jet --theory

    (FIG. 6. A caricature of turbulent jet and the entrainment., Jimmy, 2012) Ref: Jimmy Philip, Phys. F ...

  8. __setitem__() __getitem__() __delitem__()

    #item系列 和对象使用[]访问值有联系 #obj={'k':'v'} #print(obj) #print(obj['k']) class B: def __getitem__(self, ite ...

  9. [luoguP1156] 垃圾陷阱(DP)

    传送门 先按照时间排序 f[i][j] 表示 前i个物品高度为j时所剩余的最大能量 显然每个物品有堆和吃两种选择 状态转移看代码 代码 #include <cstdio> #include ...

  10. hdu 1867 kmp匹配

    #include<stdio.h> #include<string.h> #define N 100100 void getnext(int next[],char s[]) ...