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. C# 杂项

    1,函数访问等级必须高于参数等级,如函数等级是PUBLIC,则参数必须高于等于PUBLIC,若为INTERNAL 则不行.INTERNAL 低于PUBLIC, 用于同一个程序集内引用,PUBLIC则可 ...

  2. java.util.ConcurrentModificationException 解决办法

    在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除. 则使用会报以下异常:Java.util.ConcurrentModific ...

  3. [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

    7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟 ...

  4. unitty导出工程嵌入iOS原生工程中出现黑屏,但是模型还是可以扫。

    一般上出现这个问题,其实就是因为两个注意点没有搞清楚.我们分析一下,如果我们的模型或者视屏能够出来但是屏幕还是黑屏的.说明我们的unity的组件其实已经加载出来了.但是供我们交互的那个Layer类并没 ...

  5. 【JVM】模板解释器--如何根据字节码生成汇编码?

    1.背景 仅针对JVM的模板解释器: 如何根据opcode和寻址模式,将bytecode生成汇编码. 本文的示例中所使用的字节码和汇编码,请参见上篇博文:按值传递还是按引用? 2.寻址模式 本文不打算 ...

  6. [电子书] 《Android编程入门很简单》

    <Android编程入门很简单>是一本与众不同的Android学习读物,是一本化繁为简,把抽象问题具体化,把复杂问题简单化的书.本书避免出现云山雾罩.晦涩难懂的讲解,代之以轻松活泼.由浅入 ...

  7. android之文件权限问题

    当我们在手机上安装一个应用的时候,linux会为每个APP创建一个用户名和用户组 xidian.dy.com.chujia是系统为每个应用创建的一个独立的文件夹,我们可以看到这个文件的所有者为app_ ...

  8. java数组的增删改查

    import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.HashSet; p ...

  9. java操作xm——添加、修改、删除、遍历

    package com.xml.zh; import javax.xml.parsers.*; import javax.xml.transform.Transformer; import javax ...

  10. VS中两个常用辅助工具

    一. 首当推荐的是DPack 下载地址:http://www.usysware.com/dpack/ 快捷键:以下都是个人常用的热键.其他还有,我都用得比较少了,3个已经完全够了 Alt+U   查找 ...