参考自: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. MemSQL与MySQL不兼容问题总结

    1.数据行Update更新数据行时,如果数据行没有变化,MySQL返回受影响的数据行数为1,但MemSQL返回的数据行数为0. 2.MemSQL不支持唯一约束 3.MemSQL不支持外键约束

  2. 并发连接MySQL

    先吐槽一下libmysqlclientAPI的设计, 多个线程同时去connect居然会core掉. 后来Google了一番, 才发现mysql_real_connect不是线程安全的, 需要一些额外 ...

  3. 全文搜索引擎 Elasticsearch 入门教程

    全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选. 它可以快速地储存.搜索和分析海量数据.维基百科.Stack Overflow.Gi ...

  4. VirtualBox安装复制Centos6.6配置网络

    由于要搭建mongodb的集群,先用虚拟机做下相关实验,以前都用VM Vare,但是现在这个电脑的配置不是太好,VM Vare比较耗资源,所以选择VirtualBox. 1.下载VirtualBox和 ...

  5. Consecutive Subsequence CodeForces - 977F (map优化DP)·

    You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...

  6. 利用lnmp一键安装的php环境忘记mysql,root用户密码解决方法

    1.cd /lnmp1.5/tools/ 2.sh reset_mysql_root_password.sh 这样,即可完成修改!

  7. 黑客帝国效果赏析(包含ES6的语法)

    首先,看看效果吧. 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  8. openstack,docker,mesos,k8s关系

    openstack,docker,mesos,k8s什么关系? - 知乎https://www.zhihu.com/question/62985699 OpenStack + K8S 环境集成测试ht ...

  9. Tomcat集成Memcached Session Manager方案

    http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.2/memcached-session-ma ...

  10. vuex的购物车效果 index.js

    import Vue from 'vue'; import Vuex, { Store } from 'vuex'; import { stat } from 'fs'; Vue.use(Vuex); ...