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. How To Call An Ambulance

    How to Talk to the Emergency Dispatcher [minutesmatter.upmc稻糠亩] How To Call An Ambulance [askambulan ...

  2. 关于learning Spark中文版翻译

      在网上找了很久中文版,感觉都是需要支付一定金币才能下载,索性自己翻译算了.因为对Spark有一定了解,而且书籍前面写道,对Spark了解可以直接从第三章阅读,就直接从第三章开始翻译了,应该没有什么 ...

  3. Linux学习 - 权限管理命令

    一.chmod(change the permissions mode of a file) 1 功能 改变文件或目录权限 root 与 所有者 可进行此操作 2 语法 chmod  [(ugoa) ...

  4. Python enumerate():使用计数器简化循环

    摘要:当您需要计数和迭代中的值时,Pythonenumerate()允许您编写 Pythonicfor循环.最大的优点enumerate()是它返回一个带有计数器和值的元组,因此您不必自己增加计数器. ...

  5. git push大文件失败(write error: Broken pipe)完美解决

    问题 在使用git push推送大文件(超过了100MB)到GitHub远程仓库时提示异常,异常信息如下: fatal: sha1 file '<stdout>' write error: ...

  6. 4、Redis基础

    redis性能 1.关于测试性能 官方自带的测试性能的工具 redis-benchmark 压力测试工具 #进行压力测试.需求:测试:100个并发连接,100000个请求 #redis-benchma ...

  7. IOS 委托代理(delegate)实现页面传值

    LvesLi原创,转载请注明原文链接谢谢  http://www.androiddev.net/lvesli_delegate/  委托是指给一个对象提供机会对另一对象中的变化做出反应或者相应另一个对 ...

  8. springboot学习(一)

    最近想学习springboot所以在网上找了很多文章参考怎么构建springboot项目以及集成mybatis 集成mybatis的部分参考了这两篇文章 https://blog.csdn.net/t ...

  9. 批处理文件 .bat 并行Arcpy脚本提高效率的思路-提升版

    目录 前言 Arcpy脚本 导入库 函数定义 循环实现 批处理(.bat)脚本 前言 我在之前的博客园博客里,阐述了如何编写Arcpy脚本,如何利用Windows bat批处理脚本同时打开多个cmd窗 ...

  10. 微前端框架 qiankun 技术分析

    我们在single-spa 技术分析 基本实现了一个微前端框架需要具备的各种功能,但是又实现的不够彻底,遗留了很多问题需要解决.虽然官方提供了很多样例和最佳实践,但是总显得过于单薄,总给人一种&quo ...