Time limit: 3.000 seconds
限时3.000秒

Problem
问题

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.
在darkest(为岛或者大陆的名字,为防止被攻打已将其删除)上定居着一个名为“Eeny Meenys”的部落。这个部落名字的由来跟他们每年选举部落首领的方式有关。听说有一个新闻记者走访了该部落,并尝试将一些现代文明带给他们,然而不幸的结局使他的工作永远无法完成。因此该部落不再具有永久的首领;首领的任期只有一年。首领的任期结束后就会被人们将吃掉,然后重新选择首领。他们选择首领的方式名叫“Eeny meeny miny mo”,具体是:所有合格的部落成员(女性也有资格——部落文明之一)站成一个圈,选定一个起始位置,然后大巫师(他没有参选资格)开始绕圈计数‘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!’……每一次在‘o!’这个位置上的人将被推出圈外,然后闭合圆圈,从他的邻居(将被点到‘E’的那个人)重新开始计数。照此方法执行,直到剩下一个人,即被选为首领。

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 or equal than 1000000).
尽管一年任期的首领职位强烈地吸引着部落成员,但这短暂的荣耀并不能让你(你拥有一台几十年后才会发明出来的电脑)产生兴趣。你已经搞清今年的计数将从Mxgobgwq(一个非常高大的人)开始,你还想知道不能站在哪个位置。你不清楚计数的方向,也不清楚候选人数,但是你能估计到候选人数的范围(一定是小于或等于1000000的数)。

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).
写一个程序来确定第一个站立位置(即最靠近Mxgobgwq的位置),不论实际候选人数是多少或是以何种方向计数(顺时针或逆时针),该站位都是安全的。

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).
输入由一系列的行组成,每一行包含估计候选人数的上界和下界(包含边界值)。若一行输入两个0(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".
输出由一系列的行组成,每一行对应一行输入。每一行为一个数,该数表示对于估计候选人数范围内的每一个数,不论从哪个方向计数都不会被选为首领,且最靠近Mxgobgwq的位置。如果不存在这样的安全位置,则输出“Better estimate needed”。

Sample input
示例输入

80 150
40 150
0 0

Analysis
分析

人们站一个圈,每隔15个排除一个,直到剩下最后一个,这显然是典型的约瑟夫问题。约瑟夫问题的求解详见维基百科:Josephus problem
这里使用递归式:g(n,k)=(g(n-1,k)+k)mod n, g(1,k)=0进行求解。

Mxgobgwq就是开始计数的位置,我们定为0。由于计数的方向不定,对于某一个确定的候选人数,安全的位置只能以“离Mxgobgwq有多远”来表示。任何大于估计的最少人数一半的安全位置都是没有意义的。比如估计的最少人数是10,而算出的安全位置是8(从0开始编号),那么当实际的人数为10时,你的站位就变成了2(顺时针计数Mxgobgwq右边第8个即逆时针计数的左边第2个)。

对于任何一个确定的候选人数有以三种情况:一、最后剩下的人是Mxgobgwq,此时所有可能的位置都安全(注意题中两次强调closest to Mxgobgwq,即Mxgobgwq的位置非可行解);二、候选人数为偶数时剩下Mxgobgwq正对面的那个人,此时除去这个人的位置外的其他所有可能的位置都安全;三、除以上两种情况外的其它情况,会有两个不安全的位置,但由于圆圈的对称性这两个位置与Mxgobgwq的最短距离是相等的。综上所述,对于任一种候选人数不安全的位置最多只有一个,且安全的位置不能超过最少候选人数的一半。

对于一个估计的范围,可以建立一个标记数组,长度为最少候选人数的一半,其每个元素代表一个位置。所有元素初始化为0,若对应位置被选中,则置1。最后从第1个元素(首元素为第0个,此处需空过Mxgobgwq的位置)开始查找0,找到的第一个0的位置即为所求。

Solution
解答

#include <algorithm>
#include <iostream>
#include <vector> typedef std::vector<int>::iterator VECINT_ITER; int Josephus(int n, int k)
{
    static std::vector<int> vecJosephus(1, 0);
    if (n <= (int)vecJosephus.size())
        return vecJosephus[n - 1];
    int j = (Josephus(n - 1, k) + k) % n;
    vecJosephus.push_back(j);
    return j;
} int main(void)
{
    for (int i = 0; i < 100000; Josephus(++i, 15));
    for (int nSizeMin, nSizeMax; std::cin >> nSizeMin >> nSizeMax;) {
        if (nSizeMin > nSizeMax)
            std::swap(nSizeMin, nSizeMax);
        if (nSizeMin == 0 && nSizeMax == 0)
            break;
        std::vector<int> vecCover(nSizeMin / 2, 0);
        for (int nSize = nSizeMin, nJump = 15; nSize <= nSizeMax; ++nSize) {
            int j = Josephus(nSize, nJump);
            if (j > nSize / 2)
                j = nSize - j;
            if (j < vecCover.size())
                vecCover[j] = 1;
        }
        VECINT_ITER ir = std::find(vecCover.begin() + 1, vecCover.end(), 0);
        if (ir == vecCover.end())
            std::cout << "Better estimate needed" << std::endl;
        else
            std::cout << ir - vecCover.begin() << std::endl;
    }
    return 0;
}

UVa OJ 180 - Eeny Meeny的更多相关文章

  1. BNUOJ Eeny Meeny Moo

    Eeny Meeny Moo Time Limit: 1000ms Memory Limit: 65535KB                     大家都有这种经验,当太多的人同时使用互联网的时候 ...

  2. UVa OJ 197 - Cube (立方体)

    Time limit: 30.000 seconds限时30.000秒 Problem问题 There was once a 3 by 3 by 3 cube built of 27 smaller ...

  3. uva oj 567 - Risk(Floyd算法)

    /* 一张有20个顶点的图上. 依次输入每个点与哪些点直接相连. 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. bfs 水过 */ #include<iostream> # ...

  4. UVa OJ 194 - Triangle (三角形)

    Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...

  5. UVa OJ 175 - Keywords (关键字)

    Time limit: 3.000 seconds限时3.000秒 Problem问题 Many researchers are faced with an ever increasing numbe ...

  6. UVa OJ 140 - Bandwidth (带宽)

    Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...

  7. 548 - Tree (UVa OJ)

    Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...

  8. UVa OJ 10300

    Problem A Ecological Premium Input: standard input Output: standard output Time Limit: 1 second Memo ...

  9. UVa OJ 10071

    Problem B Back to High School Physics Input: standard input Output: standard output A particle has i ...

随机推荐

  1. 【转】【PNG压缩工具】PNG 图像的优化及压缩工具介绍

    图像格式有许多种不同类型,在互联网上最常见的有JPEG.GIF.BMP.TIFF和PNG.每一种图像格式都有它自己的用途,比如GIF是用于动画的,JPEG是用于高清图片的,这种图片在保存或者调整大小后 ...

  2. Java 8新特性——default方法(defender方法)介绍

    我们都知道在Java语言的接口中只能定义方法名,而不能包含方法的具体实现代码.接口中定义的方法必须在接口的非抽象子类中实现.下面就是关于接口的一个例子: 1 2 3 4 5 6 7 8 9 10 11 ...

  3. Django基础 - Debug设置为False后静态文件获取404

    当设置setting.py文件当中的DEBUG=FALSE后,Django会默认使用Web Server的静态文件处理,故若没设置好Web Server对静态文件的处理的话,会出现访问静态文件404的 ...

  4. flask 使用 SQLAlchemy 的两种方式

    1. 使用 flask-SQLAlchemy 扩展 # flask-ext-sqlalchemy.py from flask import Flask from flask.ext.sqlalchem ...

  5. CSS 实现加载动画之八-圆点旋转

    这篇文件介绍的动画是QQ邮箱APP里的加载动画,效果类似,但是不完全一样.实现过程不复杂,这里不详细解释了,直接上源码.另外还有一种实现方式,利用元素的3D转换加平移. 1. 先看截图 2. 源代码 ...

  6. ant命令总结

    ant命令总结 博客分类: 版本管理 svn , maven , ant   ant命令总结 1 Ant是什么?  Apache Ant 是一个基于 Java的生成工具. 生成工具在软件开发中用来将源 ...

  7. 20135202闫佳歆--week 7 深入理解计算机系统第七章--读书笔记

    参见上学期的学习笔记: http://www.cnblogs.com/20135202yjx/p/4836058.html

  8. 轻松理解JS基本包装对象

    今天来讨论一下JS中的基本包装对象(也叫基本包装类型),之前刚学到这里的时候,自己也是一头雾水,不明白这个基本包装对象到底是个什么鬼,后来找了很多资料,终于看清了它的真面目.首先呢,我们现在复习一下J ...

  9. Mininet建立topology zoo中的拓扑

    以前用Mininet建立拓扑都是在别人的代码上进行需求上的修改,这次从头开始将topology zoo(http://www.topology-zoo.org/)中的拓扑用Mininet建立,不失一般 ...

  10. SSRS用自定义对象绑定报表

    有一个报表的数据源是一个对象的List, 这个对象List中还有层级,其中还有其他的对象List,这样的层级有三层.其数据是从数据库中取出来的.其LINQ的操作太多了而且复杂,所以不太可 能从LINQ ...