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. POJ_2387_最短路

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46859   Accepted ...

  2. day21-2 类的派生

    目录 类的派生 派生方法一 派生方法二 类的派生 派生:子类中新定义属性的这个过程叫做派生 派生方法一 指明道姓访问某一个类的函数:该方法与继承无关 class People: def __init_ ...

  3. arx 插入图片

    #include <ShLwApi.h> #pragma comment(lib, "ShLwApi.lib") //插入影像图 Acad::ErrorStatus i ...

  4. 13Microsoft SQL Server SQL 高级事务,锁,游标,分区

    Microsoft SQL Server SQL高级事务,锁,游标,分区 通过采用事务和锁机制,解决了数据库系统的并发性问题. 9.1数据库事务 (1)BEGIN TRANSACTION语句定义事务的 ...

  5. Invalid ON UPDATE clause for 'create_date' column

    高版本的mysql导数据到低版本出现的问题 日期类型报错 解决方式:将datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT  中的  ON ...

  6. 常用HTML5代码片段

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. python爬虫27 | 当Python遇到MongoDB的时候,存储av女优的数据变得如此顺滑爽~

    上次 我们知道了怎么操作 MySQL 数据库 python爬虫26 | 把数据爬取下来之后就存储到你的MySQL数据库. MySQL 有些年头了 开源又成熟又牛逼 所以现在很多企业都在使用 MySQL ...

  8. POJ 2217 Secretary

    Secretary Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID:  ...

  9. nyoj 93 汉诺塔(三)(stack)

    汉诺塔(三) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度 ...

  10. zoj 3768 Continuous Login

    Pierre is recently obsessed with an online game. To encourage users to log in, this game will give u ...