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. 一个简单的BypassUAC编写

    什么是UAC? UAC是微软为提高系统安全而在Windows Vista中引入的新技术,它要求用户在执行可能会影响计算机运行的操作或执行更改影响其他用户的设置的操作之前,提供权限或管理员‌密码.通过在 ...

  2. 学习java的第二十天

    一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...

  3. day29并发编程

    day29并发编程 1.进程锁 一.使用锁维护执行顺序 代码: from multiprocessing import Process,Lock import os import time def t ...

  4. 强化学习实战 | 表格型Q-Learning玩井字棋(二)

    在 强化学习实战 | 表格型Q-Learning玩井字棋(一)中,我们构建了以Game() 和 Agent() 类为基础的框架,本篇我们要让agent不断对弈,维护Q表格,提升棋力.那么我们先来盘算一 ...

  5. git提交指定文件

    1. 用git add 命令添加第一个commit需要的文件 git add file1 git add file2 2. 隐藏其他修改,git stash 的参数中 -k 开关告诉仓库保持文件的完整 ...

  6. oracle 预安装命令

     yum install oracle-rdbms-server-11gR2-preinstall-1.0-6.el6 

  7. ORACLE 获取执行计划的方法

    一.获取执行计划的6种方法(详细步骤已经在每个例子的开头注释部分说明了): 1. explain plan for获取: 2. set autotrace on : 3. statistics_lev ...

  8. 【编程思想】【设计模式】【行为模式Behavioral】策略模式strategy

    Python版 转自https://github.com/faif/python-patterns/blob/master/behavioral/strategy.py #!/usr/bin/env ...

  9. FastDFS的理解和分析

    FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务,如相 ...

  10. Springboot(1) helloworld 搭建环境

    一 .springboot 运行环境: 1. jdk1.8:Spring Boot 推荐jdk1.7及以上:java version "1.8.0_112" 2.–maven3.x ...