1781 In Danger 约瑟夫问题 枚举超时,需找规律

本文介绍了一个基于约瑟夫环问题的算法实现,通过特定规则淘汰参与者直至最后一名幸存者,适用于计算特定场景下幸存者的编号,并提供了一种高效算法来解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In Danger
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 2639 Accepted: 1401

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
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
    int n,m,z;
    while(scanf("%de%d",&n,&z)!=-1&&(n||z))
    {
        while(z--) n*=10;
        m=2;
        /*int s=0;
        for(int i=2;i<=n;i++) s=(s+m)%i;
        printf("%d/n",s+1);//超时*/
        //找规律
        /*
        n:      1   2 3 4 5 6 7 8 9 10
        J(N) 1   1 3   1 3 5 7   1 3 5
        发现J(N)按照2的权值分组,在每一组里面按照2增加,如果我们把n写成2^m+l,则J(N)=2*l+1;
        所以:J(2^m+l)=2*l+1;m>=0 && 0 <= l < 2 ^m;*/
        int k=0,l,_n=n,cnt=1;//n=2^k+l;
        while(_n) _n/=2,k++,cnt*=2;k--,cnt/=2;
        l=n-cnt;
        printf("%d/n",2*l+1);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值