Joseph
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 52097   Accepted: 19838

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

Source

大致题意:

有k个坏人k个好人坐成一圈,前k个为好人(编号1~k),后k个为坏人(编号k+1~2k)

现在有一个报数m,从编号为1的人开始报数,报到m的人就要自动死去。

问当m为什么值时,可以使得在出现好人死亡之前,k个坏人先全部死掉?

PS:当前一轮第m个人死去后,下一轮的编号为1的人 为 前一轮编号为m+1的人

前一轮恰好是最后一个人死掉,则下一轮循环回到开头那个人报“1”

解题思路:

经典的约瑟夫水题

由于k值比较少(1~13),暴力枚举m就可以了

递推公式为:

ans[i];  //第i轮杀掉 对应当前轮的编号为ans[i]的人

ans[0]=0;

ans[i]=(ans[i-1]+m-1)%(n-i+1);   (i>1  ,  总人数n=2k 则n-i为第i轮剩余的人数)

正规代码

#include<iostream>
#include<cstdio>
using namespace std;
int a[],ans[];//第i轮杀掉 对应当前轮的编号为ans[i]的人
int main(){
int n,k,m;
while(scanf("%d",&k)==&&k){
if(a[k]) {printf("%d\n",a[k]);continue;}//方便重复询问(数据范围太小)
m=;//所求的最少的报数
n=*k;//总人数
for(int i=;i<=k;i++){
ans[i]=(ans[i-]+m-)%(n-i+);//n-i为剩余的人数,+1从下一个人开始
if(ans[i]<k){//ans[i]=>[0..k) 把好人杀掉了,m值不是所求
i=;
m++;//枚举m值
}
}
a[k]=m;
printf("%d\n",m);
}
return ;
}

打表写法

#include<cstdio>
int k,a[]={,,,,,,,,,,,,,,};
int main(){
while(scanf("%d",&k)==&&k)
printf("%d\n",a[k]);
return ;
}

poj1012的更多相关文章

  1. 【poj1012】 Joseph

    http://poj.org/problem?id=1012 (题目链接) 半年前的考试题..任然清晰的记得那次差10分就AK... 题意 约瑟夫环,有前k个好人,后k个坏人,要求使得后k个坏人先死的 ...

  2. poj1012.Joseph(数学推论)

    Joseph Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 493  Solved: 311 Description The Joseph's prob ...

  3. poj1012约瑟夫

    #include<stdio.h>int a[14];int f(int k,int m){    int n,i,s;    n=2*k;s=0;    for(i=0;i<k;i ...

  4. [POJ1012]Joseph

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

  5. [poj1012]Joseph_Joseph

    Joseph 题目大意:给你2*k个人,前k个是好人,后k个是坏人,编号从1到2*k.每次从上一个死掉的人的下一个开始查m个人并将第m个人杀死.问最后剩下的全是好人的m是多少. 注释:$1\le k ...

  6. 北大poj- 1012

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56348   Accepted: 21526 Descript ...

  7. POJ1012(约瑟夫问题)

    1.题目链接地址 http://poj.org/problem?id=1012 2k个人,前面k个是好人,后面k个是坏人,找一个数t,每数到第t时就去掉,使所有坏人在好人之前被杀掉. 思路:约瑟夫公式 ...

  8. 约瑟夫环问题poj1012

    题意: 有k个坏人k个好人坐成一圈,前k个为好人(编号1~k),后k个为坏人(编号k+1~2k) 现在有一个报数m,从编号为1的人开始报数,报到m的人就要自动死去. 问当m为什么值时,可以使得在出现好 ...

  9. ACM训练计划建议(写给本校acmer,欢迎围观和指正)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

随机推荐

  1. 桥牌笔记L4D17:小心阻塞

    南打3NT. 西的首攻会有3墩黑桃.3墩方块.2付梅花,共8墩到手.看来方块如果3-2分布的话,非常容易就能超一完成. 所以要想着4-1分布的安全打法. 第一墩庄家拿了黑桃J后,明手的黑桃A会阻塞,庄 ...

  2. (转)解决ScrollView嵌套ListView或者GridView导致只显示一行的方法

    即动态获取ListView和GridView的高度 一.对于ListView ListView listview= new ListView(this); setListViewHeightBased ...

  3. 根据包名字符串跳转Activity

    /** * 跳转到对应activity */ public void toActivity(Context context,String fullName) { if (className != nu ...

  4. IOS常用的系统文件目录介绍

    iOS常用目录整理说明是本文要介绍的内容,虽然不同API全面,也算是在编程中常用到的存放目录,所以是必备文档,不多说,来看详细内容讲解. 1.[/Applications] 常用软件的安装目录 内建软 ...

  5. javascript中的 cookie对象

    Cookie 对象 是一种以文件(Cookie文件)的形式保存在客户端硬盘的Cookies文件夹中的数据信息(Cookie数据).Cookie文件夹中的用户数据信息(Cookie数据).Cookie文 ...

  6. 敏捷软件开发(1)--- STATE 模式

    如果状态在运行过程中,不停的切换和改变,我们怎么办? 状态的迁移是我们生活和工程中非常普遍的一个概念.于是在数学上有一种理论来分析和解决这个问题. 有限状态机理论是一个非常成熟的理论,所有动作和流程的 ...

  7. python启动服务器

    3.*             python -m http.server [port] & 2.*             python -m SimpleHTTPServer [port] ...

  8. oracle REGEXP_SUBSTR函数

    REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier) __srcst ...

  9. 浅谈 sql 中数据的约束

    数据约束 --对用户操作表的数据进行约束 1.默认值 --当用户对使用默认值的字段不插入值的时候,就使用默认值 1)对默认值字段插入null是可以的. 2)对默认值字段可以插入非null [例如:ad ...

  10. WebAPP与原生APP的交互设计区别

    WebAPP和原生APP同为移动端,很少有研究这两项的交互区别,最近公司做了一次从原生APP到WebAPP(HTML5 )的移植,故总结一下期间遇到的问题及不同点总结. 从使用场景上,WebAPP用户 ...