VECTAR10 - Card Game

Changu and Mangu have an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. They want to play a game. They perform a wierd operation as long as there are at least two cards in the deck: Changu throws away the top card and then Mangu moves the card that is now on the top of the deck to the bottom of the deck. Your task is to find the last remaining card.

Input

Each line of input (except the last) contains a positive number n ≤ 1000000. The last line contains 0 and this line should not be processed. Input will not contain more than 500000 lines.

Output

For each number from input produce one line of output giving the last remaining card.

Example

Sample Input:
2
3
4
0

Sample Output:
2
2
4

Warning: Large I/O data, be careful with certain languages


Added by:Piyush Kumar
Date:2016-07-05
Time limit:0.100s-3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU JS-MONKEY

hide comments
2022-01-28 17:01:01
Simulation(AC)>math(AC) :)
2022-01-28 15:44:24
n <= 1 000 000
not
2 <= n <= 1 000 000
2018-06-28 18:07:03
Similiar to https://www.spoj.com/problems/DANGER/.
Get 2 AC's within seconds ;)
2017-11-18 05:43:27
A lot of garbage at the end of the input file, trying to ingest it increased my solution's memory use by 160MB o_O. Shame because it prevents using a method that just might pass in pure Python instead of PyPy.
Edit: Passed in pure Python using another trick, still could be faster if input file were clear.

Last edit: 2017-11-18 09:00:32
2017-01-09 15:02:48
My 50th: AC in 1 GO:)
2017-01-08 16:46:09
AC in one go :)
2017-01-08 15:02:04
AC in 0.1 sec. :)

Last edit: 2017-01-08 15:05:36
2016-10-15 11:16:17 Gaurav Mittal
******************
isnt this the answer. I've got like 10 WAs in both java and c++.

Re: Not particularly.

Last edit: 2016-10-15 19:00:03
2016-09-20 00:03:39 Filip Sollar
can you check my submission? getting WA dont know why
2016-07-31 18:48:26 Piyush Kumar
It is possible to get AC in Python with only few optimizations.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.