Joseph

Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 493  Solved: 311

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

HINT

 #include <stdio.h>

 int main()
{
//freopen ("a.txt" , "r" , stdin ) ;
int people[] = {}, k, Joseph[] = {};//Joseph用于打表,不然超时
while(scanf("%d", &k) != EOF && k != )
{
if (Joseph[k] != )
{
printf("%d\n", Joseph[k]);
continue;
}
int n = * k;
int m = ;
people[] = ;//people[0] = 0表示编号从0开始
for (int i = ; i <= k; i++)
{
//每次枚举完毕将剩下的人按从0到n - i编号,只要好人没有杀掉,则前面k - 1个编号是不变的
people[i] = (people[i - ] + m - ) % (n - i + );
// printf ("[%d] = %d , " , i , people[i] ) ;
if (people[i] < k)//第k个人的编号为k - 1,所以这里是<而不是<=
{
i = ;
m++;
}
}
// printf ("\n") ;
Joseph[k] = m;
printf("%d\n", m);
}
return ;
}

走过                                   时间

poj1012.Joseph(数学推论)的更多相关文章

  1. [POJ1012]Joseph

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50596   Accepted: 19239 Description T ...

  2. rsa 数学推论

    RSA加密算法是最常用的非对称加密算法,CFCA在证书服务中离不了它.但是有不少新来的同事对它不太了解,恰好看到一本书中作者用实例对它进行了简化 而生动的描述,使得高深的数学理论能够被容易地理解.我们 ...

  3. Lexicography(数学推论>>求按字典序排第k个排列)

    Lexicography Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit  ...

  4. zhx's contest (矩阵快速幂 + 数学推论)

    zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  5. HDU 4569 Special equations(数学推论)

    题目 //想不出来,看了解题报告 /* 题意:给你一个最高幂为4的一元多项式,让你求出一个x使其结果模p*p为0. 题解:f(x)%(p*p)=0那么一定有f(x)%p=0,f(x)%p=0那么一定有 ...

  6. 对 p 开 n 次方 (数学推论)

    #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> us ...

  7. 初探Stage3D(三) 深入研究透视投影矩阵

    关于本文 本文主要讲解从数学的角度如何推导出Stage3D中用到的两个投影矩阵 perspectiveLH public function perspectiveLH(width:Number,hei ...

  8. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  9. 2016 ACM/ICPC Asia Regional Dalian ICPC大连现场赛

    讲道理我挺想去大连的…… 毕竟风景不错…… 而且这次能去北京也是靠大连网络赛这一场拉开的优势…… 一道补图最短路一道数学推论简直爽爆…… 当然 除了这一场 其他场都非常划水…… 上次看到别人的博客用这 ...

随机推荐

  1. MVC5 + EF6 + Bootstrap3 (12) 新建数据

    Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-create.html 系列教程:MVC5 + EF6 + ...

  2. 自己写的一个关于Linq to Entity 动态查询的例子

    这两天一直想写一个动态查询的方式,先是晚上查询了一下,发现大家写的差不多都是一样的[如:http://www.cnblogs.com/ASPNET2008/archive/2012/10/28/274 ...

  3. bower入门

    一.bower简介 bower是一个基于js的包管理工具,类似于java的maven. 官方网站:http://bower.io/ 二.安装bower 使用bower,首先要安装node npm和gi ...

  4. Unity Networking API文档翻译(一):Networking概述

    写在翻译前的话:      我使用过Photon,研究过Ulink这些Unity提供的多人在线游戏服务器组件,这些商业组件虽然很好很强大.但是对于一个独立开发者来说,4000多软妹币还是点多.总想找一 ...

  5. SequoiaDB 系列之六 :源码分析之coord节点

    好久不见. 在上一篇SequoiaDB 系列之五   :源码分析之main函数,有讲述进程开始运行时,会根据自身的角色,来初始化不同的CB(控制块,control block). 在之前的一篇Sequ ...

  6. C#基础知识系列二(值类型和引用类型、可空类型、堆和栈、装箱和拆箱)

    前言 之前对几个没什么理解,只是简单的用过可空类型,也是知道怎么用,至于为什么,还真不太清楚,通过整理本文章学到了很多知识,也许对于以后的各种代码优化都有好处. 本文的重点就是:值类型直接存储其值,引 ...

  7. 编写高质量代码改善C#程序的157个建议[为类型输出格式化字符串、实现浅拷贝和深拷贝、用dynamic来优化反射]

    前言 本文已更新至http://www.cnblogs.com/aehyok/p/3624579.html .本文主要学习记录以下内容: 建议13.为类型输出格式化字符串 建议14.正确实现浅拷贝和深 ...

  8. Ibatis学习总结5--动态 Mapped Statement

    直接使用 JDBC 一个非常普遍的问题是动态 SQL.使用参数值.参数本身和数据列都 是动态的 SQL,通常非常困难.典型的解决方法是,使用一系列 if-else 条件语句和一连串 讨厌的字符串连接. ...

  9. awk多文件操作

    awk 多文件操作2种实现方法 我们经常会将2个有关联文本文件进行合并处理.分别从不同文件获取需要的列,然后, 整体输出到一起.awk进行多文件处理时候,常常会遇到2个方面问题,第一个是怎么样合并多个 ...

  10. 【POJ 2096】Collecting Bugs 概率期望dp

    题意 有s个系统,n种bug,小明每天找出一个bug,可能是任意一个系统的,可能是任意一种bug,即是某一系统的bug概率是1/s,是某一种bug概率是1/n. 求他找到s个系统的bug,n种bug, ...