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. Sonatype Nexus高级配置

    Sonatype Nexus的安装配置参见:CentOS系统中安装Nexus并导入已有的构件库.Nexus内置了Jetty容器,${NEXUS_HOME}/bin/jsw目录下包含了各个操作系统的启动 ...

  2. Android杂谈--Activity、Window、View的关系

    转自 http://www.cnblogs.com/loulijun/archive/2012/02/09/2344681.html Activity其实更像一个控制单元,控制window上显示的Vi ...

  3. [转载]使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理

    转载,原文来自 http://blog.csdn.net/hawksoft/article/details/21776009 最近调试原来的微信模拟登陆时发生了“基础连接已关闭,发送时发生错误”的错误 ...

  4. Moqui学习Day1

    运行时目录以及Moqui的xml配置文件 Moqui框架部署运行主要有三个核心部分: 可执行的war包文件 运行时目录 Moqui配置文件XML格式 不管怎么使用这个可执行的war文件,你必须拥有一个 ...

  5. 读代码之private construtor

    private 构造函数 private修饰构造函数在Singleton设计模式中经常使用.但是今天在读到EntityUtils时,发现这是一个final类.final很好理解:EntityUtils ...

  6. 【BZOJ 3196】二逼平衡树 线段树套splay 模板题

    我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...

  7. BZOJ-4300 绝世好(蛋疼)题 DP(递推)

    翻zky学长的blog时翻出来的..... 4300: 绝世好题 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 736 Solved: 393 [Sub ...

  8. SQLServer用sql语句怎么返回一个月所有的天数

    可用如下sql语句: select convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) day from (select '2015-07'+ ...

  9. POJ1523 SPF

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8254   Accepted: 3772 Description Consi ...

  10. [NOIP2008] 提高组 洛谷P1155 双栈排序

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...