POJ 1781 In Danger 约瑟夫环(log(m)模板)

73 篇文章 0 订阅
6 篇文章 0 订阅

Link:http://poj.org/problem?id=1781



In Danger
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3589 Accepted: 1873

Description

Flavius Josephus and 40 fellow rebels were trapped by the Romans. His companions preferred suicide to surrender, so they decided to form a circle and to kill every third person and to proceed around the circle until no one was left. Josephus was not excited by the idea of killing himself, so he calculated the position to be the last man standing (and then he did not commit suicide since nobody could watch). 

We will consider a variant of this "game" where every second person leaves. And of course there will be more than 41 persons, for we now have computers. You have to calculate the safe position. Be careful because we might apply your program to calculate the winner of this contest! 

Input

The input contains several test cases. Each specifies a number n, denoting the number of persons participating in the game. To make things more difficult, it always has the format "xyez" with the following semantics: when n is written down in decimal notation, its first digit is x, its second digit is y, and then follow z zeros. Whereas 0<=x,y<=9, the number of zeros is 0<=z<=6. You may assume that n>0. The last test case is followed by the string 00e0.

Output

For each test case generate a line containing the position of the person who survives. Assume that the participants have serial numbers from 1 to n and that the counting starts with person 1, i.e., the first person leaving is the one with number 2. For example, if there are 5 persons in the circle, counting proceeds as 2, 4, 1, 5 and person 3 is staying alive.

Sample Input

05e0
01e1
42e0
66e6
00e0

Sample Output

3
5
21
64891137

Source



#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;


//编号从0,1,2,···n-1
//n为人数,m为出圈步长,s为起使报数位置(起始位置不一定要出圈)
int Josephus ( int n, int m, int s )
{
    if ( m == 1 )
        return ( s + n - 1 ) % n;


    for ( int i = 2; i <= n; i++ )
    {
        s = ( s + m ) % i;
        //if ( i == n ) break;


        if ( s + m < i )
        {
            int x = (i-s) / (m-1); // s + m * x <= i + x;
            if ( i + x < n )
            {
                i = i + x;
                s = ( s + m * x ) % i;
            }
            else { s = (s + m * (n-i)) % n; break; }
        }
    }
    return s; //返回最后一人的位置
}


int Jose ( int n, int m, int s )
{
    for ( int i = 2; i <= n; i++ )
        s = ( s + m ) % i;
    return s;
}


int main()
{
    char str[10];
    while ( scanf("%s",str) )
    {
        int t0 = str[0] - '0';
        int t1 = str[1] - '0';
        int t3 = str[3] - '0';
        if ( t0 + t1 + t3 == 0 ) break;
        int num = 1;
        while ( t3-- ) num *= 10;
        num = t0 * num * 10 + t1 * num;
        //printf("num = %d\n",num);
        int res = Josephus ( num, 2, 0 );
        //int res1 = Jose ( num, 2, 0 );
        printf("%d\n",res+1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值