DANGER - In Danger

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 Specification

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 Specification

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

Added by:Wanderley Guimarăes
Date:2007-09-19
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO
Resource:University of Ulm Local Contest 2004

hide comments
2013-03-19 05:36:45 Himanshu
@wikipedia thanks
use log formula.....O(1) solution

Last edit: 2013-03-19 05:37:28
2013-01-13 12:08:45 Spar!k
50th AC

Last edit: 2013-02-03 00:04:30
2012-12-06 11:41:32 Anuj_LuckFove!
Great question ! :)
2012-11-17 15:44:29 triveni
for C persons, 0.00 sec is enough.. :)
2012-10-26 19:05:19 ramaravind
dont use logs..use loops instead
2012-09-06 15:11:02 abc
I didnt understand why d same code gave me
tle with %lld n ac with ld..
2012-03-22 18:37:11 bristy
there exist a direct formula for k=2...
2011-07-20 05:53:31 Troy
it should be 1
2011-07-18 20:15:03 sandeep aggrawal
what is the answer for n=1???
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.