Joseph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2094    Accepted Submission(s): 1273

Problem 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
题解:F(i)=(F(i-1)+m-1)%(n-i+1)由于去的后k个所以当F小于k就不符合。需要打表,否则超时;
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int k;
int dp[15];
bool solve(int m){
int cur=0;
for(int i=1;i<=k;i++){
if((cur+m-1)%(2*k-i+1)<k)
return false;
cur=(cur+m-1)%(2*k-i+1);
}
return true;
}
int main(){
for(k=1;k<15;k++){
int i;
for(i=1;;i++){
if(solve(i))break;
}
dp[k]=i;
}
while(~scanf("%d",&k),k){
printf("%d\n",dp[k]);
}
return 0;
}

Joseph(约瑟夫环)的更多相关文章

  1. UVA 305 Joseph (约瑟夫环 打表)

     Joseph  The Joseph's problem is notoriously known. For those who are not familiar with the original ...

  2. 约瑟夫环(Joseph)的高级版(面向事件及“伪链表””)

    约瑟夫环问题: 在一间房间总共有n个人(下标0-n-1),只能有最后一个人活命. 按照如下规则去杀人: 所有人围成一圈 顺时针报数,每次报到q的人将被杀掉 被杀掉的人将从房间内被移走 然后从被杀掉的下 ...

  3. poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)

    题目链接: POJ  1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...

  4. Joseph POJ - 1012 约瑟夫环递推

    题意:约瑟夫环  初始前k个人后k个人  问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...

  5. hdu 1443 Joseph【约瑟夫环】

    题目 题意:一共有2k个人,分别为k个好人和k个坏人,现在我们需要每隔m个人把坏人挑出来,但是条件是最后一个坏人挑出来前不能有好人被挑出来..问最小的m是多少 约瑟夫环问题,通常解决这类问题时我们把编 ...

  6. 小小c#算法题 - 12 - Joseph Circle(约瑟夫环)

    约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数(从1开始报数),数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又 ...

  7. hdu1443(约瑟夫环游戏的原理 用链表过的)

    Problem Description The Joseph's problem is notoriously known. For those who are not familiar with t ...

  8. 约瑟夫环(java面向对象实现)

    /* * 作者:zhaop * 功能:约瑟夫环 * */ public class Joseph { public static void main(String[] args) { // TODO ...

  9. POJ 3517 And Then There Was One( 约瑟夫环模板 )

    链接:传送门 题意:典型约瑟夫环问题 约瑟夫环模板题:n个人( 编号 1-n )在一个圆上,先去掉第m个人,然后从m+1开始报1,报到k的人退出,剩下的人继续从1开始报数,求最后剩的人编号 /**** ...

  10. 约瑟夫环问题详解 (c++)

    问题描述: 已知n个人(以编号0,2,3...n-1分别表示)围坐在一起.从编号为0的人开始报数,数到k的那个人出列:他的下一个人又从1开始报数,数到k的那个人又出列:依此规律重复下去,直到圆桌周围的 ...

随机推荐

  1. 上传python包到PyPI

    一.前言 由于项目需要将API响应的XML内容解析成python对象,写了一个简单的xml转python的库,因为功能简单,细节处理也不好,文档也没有,没想也不好意思上传到pypi. 后来由于多个不同 ...

  2. ThinkPHP框架下基于RBAC的权限控制模式详解

    这几天因为要做一个项目,需要可以对Web应用中通用功能进行封装,其中一个很重要的涉及到了对用户.角色和权限部分的灵活管理.所以基于TP框架自己封装了一个对操作权限和菜单权限进行灵活配置的可控制模式. ...

  3. Mono For Android中AlarmManager的使用

    最近做了一个应用,要求如下: 程序运行之后的一段时间,分别触发3个不同的事件.当然很快就想到了Android中的AlarmManager和BroadcastReceiver.但是毕竟Mono环境和Ja ...

  4. ecshop后台添加菜单项,权限问题

    ecshop后台自定义菜单涉及到几个重要的权限控制的文件,先做如下总结: 后台添加菜单项,并设置权限的步骤.:(以在系统模块添加申请友链菜单为例)commn.php       : \language ...

  5. C# 分析搜索引擎url 得到搜索关键字

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. Struts2 单文件上传

    Struts2 提供了更为简便的文件上传机制,将文件上传的复杂操作都封装到commons-fileupload.jar .commons-io.jar两个jar包中,然后再程序中使用简单的几句代码就能 ...

  7. 【Lucene3.6.2入门系列】第10节_Tika

    首先贴出来的是演示了借助Tika创建索引的HelloTikaIndex.java PS:关于Tika的介绍及用法,详见下方的HelloTika.java package com.jadyer.luce ...

  8. asp.net预览图片

    Aspx code <table> <tr> <td class="style3"> <asp:Label ID="Label1 ...

  9. 编程实现任意长度整数的加法(整数可以长度超出C++中int范围)

    #include <iostream> #include<string> using namespace std; string add(string s1,string s2 ...

  10. getchar()用法

    getchar() .从缓冲区读走一个字符,相当于清除缓冲区 .前面的scanf()在读取输入时会在缓冲区中留下一个字符'\n'(输入完s[i]的值后按回车键所致),所以如果不在此加一个getchar ...