参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6480880.html

Joseph

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 53862   Accepted: 20551

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. 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 < 14.

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

分析:

  坏人被清除掉的先后顺序无关紧要,知道下一个除掉的是好人还是坏人就行了。由于好人一直都是k个,每除掉一个坏人,坏人数就-1,队列的总数就每次-1,而下一个被清除的人在队列中的“相对”序号为 s = (s+m-1)%(n-i)。只要s>=k,那么下一个被除掉的就是坏人。

  接下来说说m的取值范围:我们考察一下只剩下k+1个人时候情况,即坏人还有一个未被处决,那么在这一轮中结束位置必定在最后一个坏人,那么开始位置在哪呢?

  这就需要找K+2个人的结束位置,然而K+2个人的结束位置必定是第K+2个人或者第K+1个人,这样就出现两种顺序情况:GGGG.....GGGXB 或  GGGG......GGGBX (X表示有K+2个人的那一轮退出的人)所以有K+1个人的那一轮的开始位置有两种可能即第一个位置或K+1的那个位置。

  限定m有两种可能: t(k+1) 或 t(k+1)+1; t>=1; 若遍历每一个m必定超时,避免超时则需要打表和限制m的范围。

代码:

 #include <bits/stdc++.h>
using namespace std;
int f(int k,int m){//死亡判断 s=(s+m-)%(n-i);
if(s<k)return ;
}
return ;
}
int main(){
int i,k,n,a[];
for(k=;k<=;k++){
i=k+;
while(){
if(f(k,i)){
a[k]=i;
break;
}else if(f(k,i+)){
a[k]=i+;
break;
}
i+=k+;//再加一倍的k+1
}
}
while(scanf("%d",&n)&&n)cout<<a[n]<<endl;
return ;
}

POJ 1012的更多相关文章

  1. poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)

    题目链接: POJ  1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...

  2. POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2

    http://poj.org/problem?id=1012 答案以954ms飘过,不过这道题可以轻松用打表过 思路:如果我们把每个人位于数组中的原始编号记为绝对编号,每次循环过后相对于绝对编号为0的 ...

  3. poj 1012——Toseph

    提交地址:http://poj.org/problem?id=1012                                                                 ...

  4. POJ 1012 Joseph 约瑟夫问题

    http://poj.org/problem?id=1012 早上去图书馆复习苦逼的复习....万恶的数逻.T T我还要自我安慰的说复习完了奖励回来刷水题~ 10点多的时候外面校运会大吼撑杆跳的那个. ...

  5. (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素)

    /* * POJ-1012.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...

  6. POJ 1012 Joseph

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44650   Accepted: 16837 Descript ...

  7. poj 1012 Joseph (约瑟夫问题)

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47657   Accepted: 17949 Descript ...

  8. Joseph POJ - 1012 约瑟夫环递推

    题意:约瑟夫环  初始前k个人后k个人  问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...

  9. POJ 1012 Joseph(打表题)

    题意:约瑟夫环的变形.要求寻找到一个杀人循环节m使后半节的坏人先被所有杀光. 直接链表模拟出结果,再打表即可: 代码:(凝视的是打表码) #include<iostream> #inclu ...

随机推荐

  1. 常用的移动前端webapp交互细节

    #常用的移动前端webapp交互细节 ##select的表现方式 ###PC端 select控件在传统PC桌面已经存在多年,由于在IE6等低版本浏览器容易造成层级错乱,一直被一些UI框架所抛弃,而用d ...

  2. Item 26: 避免对universal引用进行重载

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 如果你需要写一个以名字作为参数,并记录下当前日期和 ...

  3. Jmeter(三十五)_分布式

    jmeter分布式简单步骤说明: 1:添加远程服务器IP到配置文件 在JMETER_HOME / bin / jmeter.properties中,找到名为“ remote_hosts ” 的属性,并 ...

  4. CF892/problem/C

    题目传送门: [http://codeforces.com/contest/892/problem/C] 题意: 给你一个长度为n的数组,相邻两个元素的GCD(最大公约数)可以取代二者的任意一个,问你 ...

  5. 大神教你Debian GNU/Linux 9.7 “Stretch” Live和安装镜像开放下载

    Debian项目团队于昨天发布了Debian GNU/Linux 9 "Stretch" 的第7个维护版本更新,重点修复了APT软件管理器中存在的安全漏洞.在敦促每位用户尽快升级系 ...

  6. Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  7. 书城项目第五阶段---book表的curd

    JavaEE三层架构分析 MVC

  8. 使用Charles对iPhone进行Http(s)请求拦截(抓包)

    首先准备工具 1> Charles (下载对应操作系统的安装包进行安装,本文使用 macOS 进行演示) 2> iPhone (本文使用SE,系统版本:iOS 10) 开始 首先,对Cha ...

  9. vue 开发依赖安装

    安装element-ui yarn add element-ui --save 使用element-ui main.js import Vue from 'vue'; import ElementUI ...

  10. ::class 意思

    自 PHP 5.5 起,关键词 class 也可用于类名的解析.使用 ClassName::class 你可以获取一个字符串,包含了类 ClassName 的完全限定名称.这对使用了 命名空间 的类尤 ...