VECTAR10 - Card Game

no tags 

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


hide comments
adityad1998: 2018-06-28 18:07:03

Similiar to https://www.spoj.com/problems/DANGER/.
Get 2 AC's within seconds ;)

nadstratosfer: 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
shreyanshid: 2017-01-09 15:02:48

My 50th: AC in 1 GO:)

cs_abhi2000: 2017-01-08 16:46:09

AC in one go :)

shubham_cs_iet: 2017-01-08 15:02:04

AC in 0.1 sec. :)

Last edit: 2017-01-08 15:05:36
Gaurav Mittal: 2016-10-15 11:16:17

******************
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
Filip Sollar: 2016-09-20 00:03:39

can you check my submission? getting WA dont know why

Piyush Kumar: 2016-07-31 18:48:26

It is possible to get AC in Python with only few optimizations.

Rahadian Koesdijarto Putra: 2016-07-31 13:11:27

just like @kapoor_rakshit, using the very same algorithm yields TLE in Py 3, but got AC in C/C++. Costed me a TLE :/

pt97: 2016-07-17 19:36:45

just open ur eyes and check other test cases....!!!!


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