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. UWP开发中的方向传感器

    在UWP开发中,我们能使用的到方向有三种: OrientationSensor下的四元数:Compass罗盘的HeadingMagneticNorth:以及SimpleOrientationSenso ...

  2. Python数据结构与算法--数据类型

    从数据类型开始 Python支持面向对象的编程范式,这意味着Python把数据看成解决问题的关键. 在Python中,类似其他的面向对象的编程语言, 我们定义一个类,用来描述数据是什么 (状态) 和数 ...

  3. Android 网络编程基础之简单聊天程序

    前一篇讲了Android的网络编程基础,今天写了一个简单的聊天程序分享一下 首先是服务端代码: package com.jiao.socketdemo; import java.io.Buffered ...

  4. Android开发艺术探索学习笔记(十一)

    第十一章  Android的线程和线程池 从用途上来说,线程分为子线程和主线程,主线程主要处理和界面相关的事情,而子线程往往用于执行耗时的操作.AsyncTask,IntentService,Hand ...

  5. Effective Java 72 Don't depend on the thread scheduler

    Principle Any program that relies on the thread scheduler for correctness or performance is likely t ...

  6. hibernate取出count(*)的办法

    1.定义查询语句    String sql="select count(*) from ExcelInfor";2.获取count(*)返回结果: (1)int count=In ...

  7. 关于zend_parse_parameters函数

    PHP_FUNCTION(set_time_limit) { long new_timeout; char *new_timeout_str; int new_timeout_strlen; if ( ...

  8. Eclipse++Xdebug开发php环境配置

    一.php环境配置: 本次使用了appserv 2.5.10集成安装包.具体版本如下,安装后php版本是5.2.6 vc6,apache版本2.2 安装完成后,php配置文件在c:\windows目录 ...

  9. linux 创建和删除目录

    创建目录命令 mkdir 目录名 [root@wang whp]# mkdir catalog[root@wang whp]# lscatalog [root@wang whp]# mkdir cat ...

  10. centos下yum搭建安装linux+apache+mysql+php环境

    一.脚本YUM源安装: 1.yum install wget                                                     #安装下载工具wget 2.wge ...