Joseph

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

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

分析:
1、先推了下公式:设存在k个好人为0,1,...,k-1;存在k个坏人k,k+1,...,2k;设x属于自然数,则m属于[k+1+2kx, 2k+2kx]。也就是说,m的【第一轮】命中一定在坏人段。(尝试再推第二轮命中,但是没有推出合适的公式)
2、这个题其实做了3次:
第一次用公式+打标签的方式。发现在k>8时,要跑很久很久。
第二次用公式+循环链表的方式。发现在k>10时,要跑很久很久。
第三次,忽然意识到,我只需要输出k,而不需要输出被踢掉的坏人序号,所以不需要维护坏人序号。那么比如0,1,2,3,4,5的人,第一次被干掉的是4,正常来说,队伍就变成0,1,2,3,5。由于不需要维护坏人的序号,那么就可以认为队伍为0,1,2,3,4。
3、存一下每个k对应的m值,poj的用例有重复。
 #include <stdio.h>

 typedef int BOOL;

 #define TRUE   0
#define FALSE 1 #define MAX_NUM 14 BOOL CheckM(int m, int k)
{
int i = , len = *k;
int badDead = ; while(badDead < k)
{
i = (i+m-)%len;
if(i < k) return FALSE;
len--;
i %= len;
badDead++;
}
return TRUE;
} int CalcM(int k)
{
int x, m = ; for(x = ; x < ; x++)
{
for(m = k++*k*x; m <= *k*x+*k; m++)
{
if(TRUE == CheckM(m, k)) return m;
}
}
return ;
} int main()
{
int k = ;
int rst = ;
int ans[MAX_NUM] = {}; while( == scanf("%d", &k) && k > )
{
if(ans[k] == )
{
rst = CalcM(k);
ans[k] = rst;
}
else
{
rst = ans[k];
}
printf("%d\n", rst);
} return ;
}

北大poj- 1012的更多相关文章

  1. 北大POJ题库使用指南

    原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...

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

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

  3. POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2

    http://poj.org/problem?id=1012 答案以954ms飘过,不过这道题可以轻松用打表过 思路:如果我们把每个人位于数组中的原始编号记为绝对编号,每次循环过后相对于绝对编号为0的 ...

  4. poj 1012——Toseph

    提交地址:http://poj.org/problem?id=1012                                                                 ...

  5. POJ 1012 Joseph 约瑟夫问题

    http://poj.org/problem?id=1012 早上去图书馆复习苦逼的复习....万恶的数逻.T T我还要自我安慰的说复习完了奖励回来刷水题~ 10点多的时候外面校运会大吼撑杆跳的那个. ...

  6. (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素)

    /* * POJ-1012.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...

  7. POJ 1012 Joseph

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44650   Accepted: 16837 Descript ...

  8. poj 1012 Joseph (约瑟夫问题)

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47657   Accepted: 17949 Descript ...

  9. POJ 1012

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6480880.html Joseph Time Limit: 1000MS   Memory Lim ...

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

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

随机推荐

  1. LOB

    一,LOB介绍 1,概念 LOB 是指用来存储大对象的数据类型,一般说LOB只是泛指,具体有BLOB,CLOB,NCLOB,BFILE.   根据你数据库的设置,一个LOB可以存储的最大大小从8TB到 ...

  2. [Linux] 关闭防火墙以及开放端口

    一. service iptables stop 临时关闭, chkconfig iptables off完全关闭 service iptables status状态, service iptable ...

  3. pycharm运行Django项目,提示UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6

    确认pycharm编码都是utf-8的情况下,需要修改项目中settings.py 'DIRS': [ ],默认是空,将路径加入即可解决. TEMPLATES = [ { 'BACKEND': 'dj ...

  4. Java使用Redis--jedis

    参考:菜鸟教程 http://www.runoob.com/redis/redis-java.html 1.Java 使用 Redis 开始在 Java 中使用 Redis 前, 我们需要确保已经安装 ...

  5. Jquery版本对IE浏览器的支持

    浏览器名称 Jquery最低 Jquery最高 IE6 1.0 1.12 IE7 1.0 1.12 IE8 1.0 1.13 jquery各版本下载:https://code.jquery.com/j ...

  6. 答题卡作文模块的一种方法-VSTO

    在开始做之前,首先百度了Word有没有简单的生成方法,果然有--页面布局->稿纸设置->方格式稿纸 效果如下图所示.很规范,但是不是答题卡所需要的,因为这样会把所有页面都设置为这样的稿纸. ...

  7. vue-组件注册

    <div id="app-7"> <ol> <!-- 现在我们为每个 todo-item 提供 todo 对象 todo 对象是变量,即其内容可以是动 ...

  8. 15、TypeScript-函数

    1.参数和返回值可以指定类型 2.可选参数:在参数上加上?,表示可选的,可传可不传 3.默认参数:如果你不传参数,默认为20,如果你传参,就是你传的参数 4.剩余参数:会把传进来的实参变成一个数组,可 ...

  9. JavaScript instanceof 运算符

    instanceof运算符简介 在 JavaScript 中 判断一个变量的类型常常会用 typeof 运算符 判断一个实例是否属于某种类型会使用instanceof 与 typeof 方法不同的是, ...

  10. Oracle Database 11g安装及报错处理(win7)

    稍后会将安装过程上传.Oracle数据库安装先决条件检查失败解决方案: 1,检查失败信息中,预期值:N/A  实际值:N/A ,并未出现具体的值  查看  “详细信息” . 引起失败的原因是:无法在指 ...