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. Lab 9-3

    Analyze the malware found in the file Lab09-03.exe using OllyDbg and IDA Pro. This malware loads thr ...

  2. SWUST OJ(1102)

    顺序表上数据的划分问题的实现 #include <iostream> #include <cstdlib> using namespace std; int main() { ...

  3. Mittag-Leffer函数, Matlab内部函数

    Mittag-Leffer函数: $E_{\alpha,\beta}(x) = \sum\limits^{\infty}_{k=0} \frac{x^k}{ \Gamma( \alpha k + \b ...

  4. 微信小程序分包跳转主包页面

    由于公司项目比较多,我们事业部的微信小程序就在一个分包里.那分包页面要回到主包的首页,该怎么跳转呢,有以下两种方法 wx.switchTab(Object object) 跳转到 tabBar 页面, ...

  5. AngularJs在ng-click函数中获取代表当前元素的DOM对象

    html代码 <div ng-click="test($event)">111</div> Controllers.js $scope.test= func ...

  6. DNS推荐

    在设置DNS地址之前我们需要知道DNS地址是什么? DNS地址是一个域名服务器地址,它可以把用户的网站地址解析成IP地址.如果这个服务器出现问题,可能就上不了网了咯. 查看DNS的方法如下: WIN+ ...

  7. 苹果手机的SB系列(2)为什么不能重命名?

    为什么没有重命名? 在手机端不能重命名,在WINOWS端文件是只读的,连他TM的只读属性都无法改,不能重命名,你让我怎么备份? 我怎么知道哪些照片上次备份过了?又重头来过?还是要用苹果的MAC?这种态 ...

  8. PDF 补丁丁 0.6.0.3427 版发布(修复提取图片问题)

    新的版本进一步改善了导出图片的问题.

  9. FastCGI 进程意外退出造成500错误

    在一台新服务器上,安装新网站,之前只放至了一个网站.是服务器商配置好的,非集成环境. 添加了一个新站,路径都制定好了,但是在访问时出现了500错误.提示貌似是php的问题,但是之前的网站,运行的是di ...

  10. Vue.js学习过程

    打开各大论坛,看到好多Vue.js的话题,可以说现在是非常火的框架,看到一个人这样评论Vue:“Vue.js兼容angular.js和 react.js的优点,并剔除他们的缺点.”因为现在公司不用Vu ...