Joseph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2126    Accepted Submission(s): 1291


Problem 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 思路:一开始看到数据很小,打表写的用vector容器模拟,这样模拟肯定超时,但由于只到13,所以,把数据打出来直接上表。 另一种方法是只记录好人的开头和结尾,然后每次出一个人就更新开头和结尾,如果出来的人在头和尾之间就不可行。 关键是更新开头和结尾: head=((((head-(p+1)%i)+1+i))%i); wei=((((wei-(p+1)%i)+i+1))%i);(p+1)%i为去掉前面的数,然后开始的第一个数的原下标,然后算出head,与原下标之间的距离 (head-(p+1)%i+i)%i,因为新的开头为1,所以加1就为当前标号head=((((head-(p+1)%i)+1+i))%i);
 1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<math.h> 6 #include<vector> 7 void run(); 8 using namespace std; 9 vector<int>my; int yy[14];10 int main(void)11 {12     int n,i,j,k,p,q;run();13     while(scanf("%d",&p),p!=0)14     {15         printf("%d\n",yy[p]);16     }17     return 0;18 }19 void run()20 {21 22     int n,i,j,k,p,q;23     for(k=1;k<14;k++)24     {25         for(j=k+1;; j++)26         {27             int head=1;28             int wei=k;29             int sum=0;30             for(i=2*k; i>=1; i--)31             {   p=(j)%i;32                 if(p==0)33                     p=i;34                 if(p>=head&&p<=wei)35                     break;36                 sum++;37                 head=((((head-(p+1)%i)+1+i))%i);38                 wei=((((wei-(p+1)%i)+i+1))%i);39                 if(head==0)head=i-1;40                 if(wei==0)wei=i-1;41             }42             if(sum==k)43                 break;44         }45        yy[k]=j;46     }47 }

Joseph(hdu1443)的更多相关文章

  1. 【模拟】【HDU1443】 Joseph

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  2. Joseph(JAVA版)

    package Joseph;//约瑟夫环,m个人围成一圈.从第K个人开始报数,报道m数时,那个人出列,以此得到出列序列//例如1,2,3,4.从2开始报数,报到3剔除,顺序为4,3,1,2publi ...

  3. HDU1443 模拟(难)

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. Hdu 1443 Joseph

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. 一道模拟题:改进的Joseph环

    题目:改进的Joseph环.一圈人报数,报数上限依次为3,7,11,19,循环进行,直到所有人出列完毕. 思路:双向循环链表模拟. 代码: #include <cstdio> #inclu ...

  6. POJ 1012 Joseph

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

  7. poj1012.Joseph(数学推论)

    Joseph Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 493  Solved: 311 Description The Joseph's prob ...

  8. hdu 1443 Joseph (约瑟夫环)

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. UVa 1363 (数论 数列求和) Joseph's Problem

    题意: 给出n, k,求 分析: 假设,则k mod (i+1) = k - (i+1)*p = k - i*p - p = k mod i - p 则对于某个区间,i∈[l, r],k/i的整数部分 ...

随机推荐

  1. ysoserial-CommonsBeanutils1的shiro无依赖链改造

    ysoserial-CommonsBeanutils1的shiro无依赖链改造 一.CB1利用链分析 此条利用链需要配合Commons-Beanutils组件来进行利用,在shiro中是自带此组件的. ...

  2. C语言中的字符和整数之间的转换

    首先对照ascal表,查找字符和整数之间的规律: ascall 控制字符  48  0  49  1  50  2  51  3  52  4  53  5  54  6  55  7  56  8 ...

  3. HDFS01 概述

    HDFS 概述 目录 HDFS 概述 HDFS的产生背景和定义 HDFS产生背景 HDFS定义 优缺点 优点 缺点 组成 NameNode DataNode Secondary NameNode(2n ...

  4. A Child's History of England.8

    CHAPTER 3 ENGLAND UNDER THE GOOD SAXON, ALFRED Alfred [born in 849 CE, 唐: 618年-907年] the Great was a ...

  5. addict, address, adequate

    addict Addiction is a biopsychosocial disorder characterized by repeated use of drugs, or repetitive ...

  6. 零基础学习java------day17------缓冲字节流,转换字节流,简化流,缓冲字符流,序列化和对象流

    1. 缓冲字节流 缓冲区:缓冲区实质上是一个数组.通常它是一个字节数组,但是也可以使用其他种类的数组.但是一个缓冲区不 仅仅 是一个数组.缓冲区提供了对数据的结构化访问,而且还可以跟踪系统的读/写进程 ...

  7. 零基础学习java------day6----数组

    0. 内容概览 补充:main方法中的数组 1. 数组的概述 概念: 用来存储一组相同数据类型的集合(或者叫容器) 注意事项: 1. 数组中的元素类型必须一致 2. 数组本身是引用数据类型,但是里面的 ...

  8. Shell脚本实现乱序排列文件内容的多种方法(洗牌问题)

    洗牌问题:洗一副扑克,有什么好办法?既能洗得均匀,又能洗得快?即相对于一个文件来说怎样高效率的实现乱序排列? ChinaUnix 确实是 Shell 高手云集的地方,只要你想得到的问题,到那里基本上都 ...

  9. QQ表情的添加

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

  10. Java poi导出设置 Excel某些单元格不可编辑

    小白的总结,大神勿喷:需要转载请说明出处,如果有什么问题,欢迎留言 一.需求: 1.某一列 .某一行或某些单元格不可编辑,其他列可以编辑 二.期间遇到的问题 1.无法设置成不可编辑 2.设置为不可编辑 ...