BNUOJ 13098 约瑟夫环问题
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 |
#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 约瑟夫环问题的更多相关文章
- C#实现约瑟夫环问题
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace orde ...
- C语言数组实现约瑟夫环问题,以及对其进行时间复杂度分析
尝试表达 本人试着去表达约瑟夫环问题:一群人围成一个圈,作这样的一个游戏,选定一个人作起点以及数数的方向,这个人先数1,到下一个人数2,直到数到游戏规则约定那个数的人,比如是3,数到3的那个人就离开这 ...
- C语言链表实现约瑟夫环问题
需求表达:略 分析: 实现: #include<stdio.h> #include<stdlib.h> typedef struct node { int payload ; ...
- javascript中使用循环链表实现约瑟夫环问题
1.问题 传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第 ...
- tc 147 2 PeopleCircle(再见约瑟夫环)
SRM 147 2 600PeopleCircle Problem Statement There are numMales males and numFemales females arranged ...
- HDU 3089 (快速约瑟夫环)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...
- 约瑟夫环(Josehpuse)的模拟
约瑟夫环问题: 0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字,求出这个圆圈里剩下的最后一个数字. 这里给出以下几种解法, 1.用队列模拟 每次将前m-1个元 ...
- C++ 约瑟夫环问题
约瑟夫环比较经典了 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直 ...
- 约瑟夫环的java解决
总共3中解决方法,1.数学推导,2.使用ArrayList递归解决,3.使用首位相连的LinkedList解决 import java.util.ArrayList; /** * 约瑟夫环问题 * 需 ...
随机推荐
- R函数详解
字符串连接函数paste 1.字符串连接:paste(..., sep = " ", collapse = NULL)sep表示分隔符,默认为空格.collapse表示如果不指定值 ...
- 【前端路由】Vue-router 中hash模式和history模式的区别
咱们今天说说VUE路由的hash模式与history模式的区别,这个也是面试常问的问题,不要小看这道题其实问到这里的时候那个面试官应该是个大牛,开发经验丰富,这个题其实就是考验你的开发经验是否属实. ...
- nginx配置X-Forwarded-For 防止伪造ip
网上常见nginx配置ip请求头 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 风险: 用于可以通过自己设置请求头来伪造ip ...
- BigDecimal舍入规则
1.ROUND_UP 舍入远离零的舍入模式. 在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数字加1). 注意,此舍入模式始终不会减少计算值的大小. 2.ROUND_DOWN 接近零的舍入模 ...
- java环境初级部署及项目搭建
一.网页地址 Java各版本下载地址: http://www.oracle.com/technetwork/cn/java/archive-139210-zhs.html Eclipse官方下载地址: ...
- 57.fielddata预加载机制以及序号标记预加载
如果真的要对分词的field执行聚合,那么每次都在query-time的同时由es生成fielddata并加载到内存中来,速度可能会比较慢,性能很差,改善性能的方式就是预先生成fielddata值并加 ...
- bootstrap中栅格系统的原理
1.基本结构 Bootstrap采取12列的栅格体系,根据主流设备的尺寸进行分段,每段宽度固定,通过百分比和媒体查询实现响应式布局. Bootstrap划分了四种尺寸:超小屏(手机).小屏(平板竖屏) ...
- 洛谷 4246 BZOJ 1018 [SHOI2008]堵塞的交通
[题解] 原来线段树还可以这么玩.. 我们用线段树维护连通性.对于一个矩形,我们用4个标记维护4个点的联通情况,再用两个标记维护右边两个点与它们右边的与它们在同一行的点的联通情况. 画图表示,就是 另 ...
- [bzoj3106][cqoi2013][棋盘游戏] (对抗搜索+博弈论)
Description 一个n*n(n>=2)棋盘上有黑白棋子各一枚.游戏者A和B轮流移动棋子,A先走. l A的移动规则:只能移动白棋子.可以往上下左右四个方向之一移动一格. ...
- chrome浏览器中解决embed标签 loop="true" 背景音乐无法循环的问题。
今天试了下在html网页中加入背景音乐并设置为循环播放.一开始用<embed>标签,设置loop="true", 但是结果发现在IE浏览器可以,但是在chrome浏览器 ...