Eeny Meeny
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 348 Accepted: 101

Description

In darkest < name of continent/island deleted to prevent offence > lived a tribe called the ‘‘Eeny Meenys’’. They got this name from their way of choosing a chief for a year. It appears that a newspaper reporter visited the tribe and managed to get across a few ideas of civilisation, but apparently came to an unfortunate end before finishing the job. Thus the tribe no longer had a permanent chief; the chief’s term was exactly one year. At the end of that time, they ate the current chief, and chose another chief. Their method of choosing a chief was the ``Eeny meeny miny mo’’ method. All eligible tribal members (women were also eligible–one of the blessings of civilisation the tribe had adopted) stood in a circle, a starting place was chosen, and the chief medicine man (who was ineligible for chieftainship) went around counting out ‘E’, ‘e’, ‘n’, ‘y’, ‘M’, ‘e’, ‘e’, ‘n’, ‘y’, ‘M’,‘i’, ‘n’, ‘y’, ‘M’, ‘o!’, ‘E’, ‘e’, ‘n’, ‘y’, ‘M’, ‘e’, ‘e’, ‘n’, ‘y’, ‘M’, ‘i’, ‘n’, ‘y’, ‘M’, ‘o!’, … At every ‘o!’, the person indicated was pushed out of the circle which then closed up and the count restarted with his neighbour (the one who would have been `E’ anyway). This process continued until only one was left–the new chief.

While the chance of glory for a year makes the job of chief highly attractive to tribal members, you (possessing a computer decades before they were invented) find the brevity of the glory unappealing. You have managed to find out that the count this year will start with Mxgobgwq (a very large person), so you would like to know where not to stand. You don’t know the direction, nor how many eligible people there are, but you can estimate the number (it is certainly less than 500).

Write a program that will determine the `first’ (i.e. closest to Mxgobgwq) safe position to stand, regardless of the actual number of people and the direction of count (clockwise or anti-clockwise).

Input

Input will consist of a series of lines, each line containing the upper and lower estimates of the number of eligible people (both numbers inclusive). The file will be terminated by a line containing two zeroes (0 0).

Output

Output will consist of a series of lines, one for each line of the input. Each line will consist of a single number giving the number of the position closest to Mxgobgwq that will not be chosen as chief for any number in the given range and for either direction of elimination. If no position is safe then print “Better estimate needed”.

Sample Input

80 150
40 150
0 0

Sample Output

1
Better estimate needed

Source

South Pacific 1993,uva 180

问题链接:POJ1212 HDU1650 UVA180 LA5240 Eeny Meeny
问题简述:(略)
问题分析:人们站一个圈,每隔15个排除一个,直到剩下最后一个,约瑟夫环问题。使用递归式:g(n,k)=(g(n-1,k)+k)mod n, g(1,k)=0来求解。有关约瑟夫环,参见维基百科的Josephus problem。
程序说明:程序不够简洁,改写了一版,改进了约瑟夫环递推式打表的程序代码。新程序代码比起那个记忆化递归的代码,简单易懂多了。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ1212 HDU1650 UVA180 LA5240 Eeny Meeny */#include <iostream>
#include <cstdio>
#include <cstring>using namespace std;const int K = 15;
const int N = 1e6;
int a[N + 1], b[N + 1];int main()
{a[0] = a[1] = 0;for(int n = 2; n <= N; n++)a[n] = (a[n - 1] + K) % n;int low, high;while(~scanf("%d%d", &low, &high)) {if(low > high) swap(low, high);if(low == 0 && high == 0) break;memset(b, 0, sizeof(b));for(int i = low; i <= high; i++) {int t = a[i];if(t > i / 2) t = i - t;if(t < low / 2) b[t] = 1;}bool flag = false;int k = 1;for(k = 1; k < low / 2; k++)if(b[k] == 0) {flag = true;break;}if(flag) printf("%d\n", k);else printf("Better estimate needed\n");}return 0;
}

AC的C++语言程序如下:

/* POJ1212 HDU1650 UVA180 LA5240 Eeny Meeny */#include <iostream>
#include <cstdio>
#include <cstring>using namespace std;const int N = 1e6;
int a[N + 1], b[N + 1];int jose(int n, int k)
{if(a[n] != -1) return a[n];else return a[n] = (jose(n - 1, k) + k) % n;
}int main()
{memset(a, -1, sizeof(a));a[0] = 0;for(int i = 1; i <= N; i++)jose(i, 15);int low, high;while(~scanf("%d%d", &low, &high)) {if(low > high) swap(low, high);if(low == 0 && high == 0) break;memset(b, 0, sizeof(b));for(int i = low; i <= high; i++) {int t = a[i];if(t > i / 2) t = i - t;if(t < low / 2) b[t] = 1;}bool flag = false;int k = 1;for(k = 1; k < low / 2; k++)if(b[k] == 0) {flag = true;break;}if(flag) printf("%d\n", k);else printf("Better estimate needed\n");}return 0;
}

POJ1212 HDU1650 UVA180 LA5240 Eeny Meeny【约瑟夫环】相关推荐

  1. python中约瑟夫环程序_Python实现约瑟夫环问题的方法

    本文实例讲述了Python实现约瑟夫环问题的方法.分享给大家供大家参考,具体如下: 题目:0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的 ...

  2. 约瑟夫环问题的两种解法(详解)

    约瑟夫环问题的两种解法(详解) 题目: Josephus有过的故事:39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓.于是决定了自杀方式,41个人排成一个圆 ...

  3. python约瑟夫环问题给十个学生编号报到3者出列_趣味算法--约瑟夫环问题(示例代码)...

    问题描述 已知n个人(以编号1,2,3,...,n分别表示)围坐在一张圆桌上.指定编号为k的人开始从1报数,数到m的那个人出列:出列那个人的下一位又从1开始报数,数到m的那个人出列:以此规则重复下去, ...

  4. 一文读懂约瑟夫环算法

    2020-05-25 20:13:40 作者 | 扬帆向海 责编 | 王晓曼 出品 | CSDN博客 问题描述 约瑟夫问题(有时也称为约瑟夫斯置换,是一个出现在计算机科学和数学中的问题.在计算机编程的 ...

  5. 面试题小记:1、统计字符串出现的次数,2、约瑟夫环问题

    今天面到了一个比较有意思的笔试题,先记录一下: 1.字符串类似'aaabbccddd',写个方法得出'3a2b2c3d',即统计字符串出现的个数 $arr = str_split('aaabbccdd ...

  6. java实现简单的约瑟夫环问题(二)

    Josephus(约瑟夫)问题的数学方法 前面的内容都是直接来来自于百度百科,后面才是我对这段话的理解 无论是用链表实现还是用数组实现都有一个共同点:要模拟整个游戏过程,不仅程序写起来比较烦,而且时间 ...

  7. 约瑟夫环双向链表c语言实,双向链表与约瑟夫环代码

    双向链表 //注意:该文件操作的链表为带头结点双向链表,头结点数据为-1 #include #include #include #define OK 1 #define ERROR 0 typedef ...

  8. python解决约瑟夫问题_Python实现约瑟夫环问题的方法

    本文实例讲述了Python实现约瑟夫环问题的方法.分享给大家供大家参考,具体如下: 题目:0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的 ...

  9. 约瑟夫环(约瑟夫问题)求最后出列的人数

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

最新文章

  1. 彻底搞懂MySQL分区
  2. python自定义全局异常_Django 自定义404 500等错误页面的实现
  3. 发那科机器人寄存器Ar_发那科机器人与TP参数的千丝万缕关系
  4. 题解 UVA10587 【Mayor's posters】
  5. 利用栈实现递归函数的非递归计算
  6. 确定不进来看看?分享一个插件,让敲代码不再枯燥,activate-power-mode
  7. PHP包管理器PEAR 中爆多个缺陷可发动供应链攻击,已潜伏15年
  8. mysql为什么用B 树做索引_mysql为什么用b+树做索引
  9. 10亿数据量只需要100MB内存,redis的位存储为什么这么牛?
  10. swoft使用redis
  11. matlab自适应遗传算法代码,matlab自适应遗传算法
  12. 数据库两个表关联查询
  13. 解决微信公众号accessToken白名单问题
  14. 页面显示\n\tat的问题
  15. 51CTO学院周年庆开始了!
  16. 快递查询单号查询追踪,一键查询全部物流
  17. Nginx用户访问及密码验证
  18. 我的那些年~小小少年~小屁孩
  19. 【2073】三角形面积
  20. 【CF比赛】Educational Codeforces Round 102 (Rated for Div. 2)

热门文章

  1. 如何通过svg代码还原图片_如何通过nginx反向代理来调试代码?
  2. hbuilderx的快捷键整理pdf_47个电脑快捷键大全,让你工作提升100倍,一般人我不告诉他...
  3. 2020-01-14 英文资料How to Set Up Intel® Ethernet Flow Director
  4. DataSet运用DES加解密到Xml
  5. css空心三角形_(12)把“可以动的盒子”更优雅地展示: “伪元素”妙用 | CSS...
  6. excel中去掉换行符的快捷键
  7. 科恒khs202温控器使用说明书_STC-9200温控器使用说明书——精创温控器
  8. Akka网络编程基本介绍
  9. 新泰一中2021年高考成绩查询,牛!泰安新泰一中2018年高考一班级62人全部上本科线...
  10. java数据库实体层封装_Java通过JDBC封装通用DAO层