Arena of Greed
Contest: 2020 ICPC, COMPFEST 12, Indonesia Multi-Provincial Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)
Difficulty rating: 1400
Tags: games, greedy

Lately, Mr. Chanek frequently plays the game Arena of Greed. As the name implies, the game's goal is to find the greediest of them all, who will then be crowned king of Compfestnesia.

The game is played by two people taking turns, where Mr. Chanek takes the first turn. Initially, there is a treasure chest containing $$$N$$$ gold coins. The game ends if there are no more gold coins in the chest. In each turn, the players can make one of the following moves:

- Take one gold coin from the chest.
- Take half of the gold coins on the chest. This move is only available if the number of coins in the chest is even.

Both players will try to maximize the number of coins they have. Mr. Chanek asks your help to find the maximum number of coins he can get at the end of the game if both he and the opponent plays optimally.

Input format:
The first line contains a single integer $$$T$$$ $$$(1 \le T \le 10^5)$$$ denotes the number of test cases.

The next $$$T$$$ lines each contain a single integer $$$N$$$ $$$(1 \le N \le 10^{18})$$$.

Output format:
$$$T$$$ lines, each line is the answer requested by Mr. Chanek.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
For the first case, the game is as follows:

1. Mr. Chanek takes one coin.
2. The opponent takes two coins.
3. Mr. Chanek takes one coin.
4. The opponent takes one coin.

For the second case, the game is as follows:

1. Mr. Chanek takes three coins.
2. The opponent takes one coin.
3. Mr. Chanek takes one coin.
4. The opponent takes one coin.

Example input:
2
5
6

Example output:
2
4

Editorial:
We can solve this problem greedily. The tricky case is if the current number of coins is a multiple of $$$4$$$ greater than $$$8$$$, it is optimal to take 1 coin instead of taking $$$\frac{N}{2}$$$ coins. The proof:
Lets say the number of of coins is $$$4k$$$ for some $$$k>0$$$. If we take $$$2k$$$ coins:
1. Mr. Chanek takes $$$2k$$$ coins. (remaining: $$$2k$$$)
2. Opponent takes $$$k$$$ coins. (remaining: $$$k$$$)
The number of coins Mr. Chanek gets is $$$2k$$$ coins.
Lets say we take $$$1$$$ coin:
1. Mr. Chanek takes $$$1$$$ coin. (remaining: $$$4k-1$$$)
2. Opponent takes $$$1$$$ coin. (remaining: $$$4k-2$$$)
3. Mr. Chanek takes $$$2k -1$$$ coins. (remaining: $$$2k-1$$$).
4. Opponent takes $$$1$$$ coin. (remaining: $$$2k-2$$$).
In both cases, Mr. Chanek ends up with $$$2k$$$ coins. However, there are $$$2k-2$$$ coins remaining in the second example, while only $$$k$$$ coins remain in the first example. So, if $$$2k-2 > k$$$, we take the second case. It turns out, $$$2k - 2 \le k$$$ is only true if $$$k = 1$$$. So if the number of coins is $$$4$$$, we take $$$2$$$ coins, else we take $$$1$$$ coin.
Alternatively, you can also look at the number of coins the opponent gained. On the first case, the opponent gains $$$k$$$ coins, while on the second case, the opponent gains $$$2$$$ coins. In both cases, we gain $$$2k$$$ coins. So, it's optimal to choose the option that maximizes the difference of coins gained ($$$k$$$ or $$$2$$$).
For other possible number of coins:
• if the number of coins is even not multiple of $$$4$$$, we take half coins.
• If the number of coins is odd, we take $$$1$$$ coin.
Time complexity: $$$O(T log N)$$$