Joseph问题

来源:互联网 发布:sql 商业智能 编辑:程序博客网 时间:2024/05/16 05:51
Josephus问题:
假设n个竞赛者排成一个环形,依次顺序编号1,2,…,n。从某个指定的第1号开始,沿环计数,每数到第m个人就让其出列,且从下一个人开始重新计数,继续进行下去。这个过程一直进行到所有的人都出列为止。最后出列者为优胜者。
无论是用链表实现还是用数组实现来解约瑟夫问题都有一个共同点:要模拟整个游戏过程,不仅程序写起
来比较麻烦,而且时间复杂度高达O(nm),当n,m非常大(例如上百万,上千万)的时候,几乎是没有办法在短时间内出结果的。注意到原问题仅仅是要求出最后的胜利者的序号,而不是要模拟整个过程。因此如果要追求效率,就要打破常规,实施一点数学策略。

为了讨论方便,先把问题稍微改变一下,并不影响原意:

问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。

我们知道第一个人(编号一定是m%n-1) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环
(以编号为k=m%n的人开始):
k k+1 k+2 ... n-2, n-1, 0, 1, 2, ... k-2
并且从k开始报0。

现在我们把他们的编号做一下转换:
k     --> 0
k+1   --> 1
k+2   --> 2
...
...
k-2   --> n-2
k-1   --> n-1

变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x
是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?变
回去的公式很简单:x'=(x+k)%n

如何知道(n-1)个人报数的问题的解?显然,只要知道(n-2)个人的解就行了。(n-2)个人的解
呢?当然是先求(n-3)的情况 ---- 这显然就是一个倒推问题!

递推公式:

令f[i]表示i个人玩游戏报m退出最后胜利者的编号,最后的结果自然是f[n]

递推公式
f[1]=0;
f[i]=(f[i-1]+m)%i; (i>1)

有了这个公式,我们要做的就是从1-n顺序算出f[i]的数值,最后结果是f[n]。因为实际生
活中编号总是从1开始,我们输出f[n]+1

由于是逐级递推,不需要保存每个f[i],程序也是很简单:

#include <stdio.h>
main()
{
  int n, m, i, s=0;
  printf ("N M = "); 
  scanf("%d%d", &n, &m);
  for (i=2; i<=n; i++) s=(s+m)%i;
  printf ("The winner is %d/n", s+1);
}



这个算法的时间复杂度为O(n),相对于模拟算法已经有了很大的提高。算n,m等于一百万,一千万的情况不是问题了。可见,适当地运用数学策略,不仅可以让编程变得简单,而且往往会成倍地提高算法执行效率。
以上转自:http://blog.csdn.net/zouqibo/article/details/5681466

接下来我们来看两道POJ的题:
1.The Joseph's problem is notoriously known. For those who are not familiar with the problem, 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 the message about the incident.

Although many good programmers have been saved since Joseph spread out this information, Joseph's cousin introduced a new variant of the malignant game. This insane character is known for its barbarian ideas and wishes to clean up the world from silly programmers. We had to infiltrate some the agents of the ACM in order to know the process in this new mortal game.

In order to save yourself from this evil practice, you must develop a tool capable of predicting which person will be saved.

The Destructive Process

The persons are eliminated in a very peculiar order; m is a dynamical variable, which each time takes a different value corresponding to the prime numbers' succession (2,3,5,7...). So in order to kill the ith person, Joseph's cousin counts up to the ith prime.

Input

It consists of separate lines containing n [1..3501], and finishes with a 0.

Output

The output will consist in separate lines containing the position of the person which life will be saved.

Sample Input
6
Sample Output
4
举个例子,6个人分别编号1 2 3 4 5 6,2号出队,剩下1 3 4 5 6,然后从3号开始循环往后数3下,5号出队,剩下1 3 4 6,然后从6号循环往后数5下,6号出队,剩下1 3 4,然后从1号开始循环往后数7下,1号出队,剩下3 4,然后从3号开始循环往后数11下,3号出队,最后剩下4.
我们可以模仿上面程序,只是m在动态变化,我们先用数组存储连续的素数。然后在循环中不断更换m值。代码如下:
#include <iostream>#include <cmath>using namespace std;bool is_prime(int x){    int i;    int y=sqrt(x);    for(i=2;i<=y;i++)    {        if(x%i==0)        {            return false;        }    }    return true;}int main(){    int i=3,j=2,n,prime[3501];    prime[1]=2;    for(i=3;j<3501;i+=2)    {        if(is_prime(i))        {            prime[j++]=i;        }    }    while((cin>>n)&&n)    {        int s=0;        j=n-1;        for(i=2;i<=n;i++)        {            s=(s+prime[j--])%i;        }        cout<<s+1<<endl;    }    return 0;}
2.

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

340
Sample Output
530
执行k次后,还剩下k人,这k个人编号分别为0,1,2...k-1,将他们转化到2k个人中的编号,编号必须在1到k之间。我们可以遍历m,找到一个符合要求的m值,其中m%2k>k或者m%2k==0.代码如下:
#include <iostream>using namespace std;int main(){    int k;    int a[15];    a[11]=459901;    a[12]=1358657;    a[13]=2504881;    a[14]=13482720;    while(cin>>k&&k)    {        if(k>=11)        {            cout<<a[k]<<endl;            continue;        }        int s,i,m,node,flag;        for(m=k+1;;m++)        {            flag=1;            if((m%(2*k)>k)||(m%(2*k)==0))            {                for(node=0;node<k;node++)                {                    s=node;                    for(i=k+1;i<=2*k;i++)                    {                        s=(s+m)%i;                    }                    if((s+1)>k)                    {                        flag=0;                        break;                    }                }            }            else            {                m+=k-1;                continue;            }            if(flag)            {                cout<<m<<endl;                break;            }        }    }    return 0;}
由于当k大于等于11时该程序会超时,所以只能先算出k=11,12,13,14时的m值...
0 0
原创粉丝点击