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. inode满处理

    https://blog.csdn.net/liuxiao723846/article/details/79423581 [root@monitor251 bin]# for i in /boot; ...

  2. 雷林鹏分享:jQuery EasyUI 数据网格 - 动态改变列

    jQuery EasyUI 数据网格 - 动态改变列 数据网格(DataGrid)列可以使用 'columns' 属性简单地定义.如果您想动态地改变列,那根本没有问题.为了改变列,您可以重新调用dat ...

  3. jmockit 模拟同一个函数多次调用每次返回不同结果

    new Expectations(){{ calendarService.getGeneralCalendar((Date)any); returns(null, new AbrahamResult& ...

  4. numpy 数组索引数组

    在numpy中,数组除了可以被整数索引,还可以被数组索引. a[b]就是已数组b的元素为索引,读取数组a的值. 当被索引数组a是一维数组,b是一维或则多维数组时,结果维度维度与索引数组b相同. a = ...

  5. application.xml中配置文件properties导入

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Prop ...

  6. sentinel详解

    https://segmentfault.com/a/1190000002680804 sentinel端口 port #工作路径 dir "/usr/local/redis-6380&qu ...

  7. JavaScript判断苹果 iPhone X Series 机型

    原文链接: JavaScript 判断 iPhone X Series 机型 现状 iPhone X 底部是需要预留 34px 的安全距离,需要在代码中进行兼容. 现状对于 iPhone X 的判断基 ...

  8. 此博客不再更新和分享UiPath文章

    由于版权问题,将不再分享我的笔记 警告:任何人不得拷贝及挪用我的uipath笔记作为商业用途,一旦发现,立刻发律师函,请知悉及慎重!!!!

  9. npm run build 打包后,如何运行在本地查看效果(Nginx服务)

    这段时间,研究了一下vue 打包的很慢的问题.但是当我 npm run build 打包后,在本地查看效果的时候,活生生被我老大鄙视了,因为我打开了XAMPP.他说:你怎么不用Nginx啊?用这个一堆 ...

  10. 上海MVP见面会

    很愉快,很有收获的一次见面!