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. 【java基础】IOC介绍及其简单实现

    控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依赖注入 ...

  2. 闭包拾遗 & 垃圾回收机制

    闭包拾遗 之前写了篇<闭包初窥>,谈了一些我对闭包的浅显认识,在前文基础上,补充并且更新些对于闭包的认识. 还是之前的那个经典的例子,来补充些经典的解释. function outerFn ...

  3. Django1.8教程——从零开始搭建一个完整django博客(一)

    第一个Django项目将是一个完整的博客网站.它和我们博客园使用的博客别无二致,一样有分类.标签.归档.查询等功能.如果你对Django感兴趣的话,这是一个绝好的机会.该教程将和你一起,从零开始,搭建 ...

  4. 我从腾讯那“偷了”3000万QQ用户数据,出了份很有趣的独家报告!

    声明: 1.目前程序已停止运行!QQ空间也已升级访问安全机制. 2.本“分析”数据源自部分用户的公开信息,并未触及隐私内容,广大网友无需担心. 3.QQ空间会不定期发布大数据分析报告,感兴趣的朋友关注 ...

  5. Windows Azure 名词定义(Glossary)

    Glossary(名词) Definition(定义) Availability Set 可用性组 refers to two or more Virtual Machines deployed ac ...

  6. 第八章:Javascript函数

    函数是这样一段代码,它只定义一次,但可能被执行或调用任意次.你可能从诸如子例程(subroutine)或者过程(procedure)这些名字里对函数概念有所了解. javascript函数是参数化的: ...

  7. RequireJS学习资料汇总

    入门系列 [1]阮一峰 RequireJS用法 [2]RequireJS入门指南 文档系列 [1]RequireJS中文文档 [2]RequireJS英文文档 代码实践 知识扩展 [1]计算机干了什么

  8. 每天一个linux命令(27):ln 命令

    ln 是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在 每一个需要的目录下都放一个必须相同的文件,我们只 ...

  9. jquery indexOf使用方法

    当无法确定在某个字符串中是否确实存在一个字符的时候,就可调用 indexOf() 和 lastIndexOf() 方法 indexOf() 和 lastIndexOf() 是js的用法,与jquery ...

  10. Fence 设备

    RHCS中必须有Fence设备,在设备为知故障发生时,Fence负责让占有浮动资源的设备与集群断开. REDHAT的fence device有两种, 内部fence设备: IBM RSAII卡,HP的 ...