Binary Blackboard

Oscar and Eric play the following game. First, they agree on a positive integer $n$, and they begin by writing its binary representation on a blackboard. They then take turns, with Oscar going first, to write a number on the blackboard in binary representation, such that the sum of all written numbers does not exceed $2n$.
The game ends when there are no valid moves left. Oscar wins if the number of $1$s on the blackboard is odd, and Eric wins if it is even.
Let $S(N)$ be the sum of all $n \le 2^N$ for which Eric can guarantee winning, assuming optimal play.
For example, the first few values of $n$ for which Eric can guarantee winning are $1,3,4,7,15,16$. Hence $S(4)=46$.
You are also given that $S(12) = 54532$ and $S(1234) \equiv 690421393 \pmod{1\,000\,000\,007}$.
Find $S(12\,345\,678)$. Give your answer modulo $1\,000\,000\,007$.

The problem is about binary representations and bitwise operations. If you are familiar with binary, you’d know that a binary string’s value is doubled when it is shifted to the left by one position, and a value of 1 can only be added at the rightmost position.

From that, using induction, for any integer n which is a power of 2, Oscar can always win. Now, Oscar should only play ‘1’ for his turn, and then he can always mirror Eric’s move to keep the number of ‘1’s odd.

For an arbitrary number ‘n’, our goal is to find a winning strategy for Eric. Let’s denote n’s binary representation as $(b_mb_{m-1}\ldots b_1b_0)_2$ (where $b_m = 1$, as n’s most significant bit). Now, for Eric to win, he should transform ‘n’ into $2^k$, where $k \le m$. If it is possible for Eric to do so, he will definitely win. To achieve this, let’s take a look at Oscar’s first move. If Oscar wrote $2^i$ in the first round where $i < m$, then Eric could simply write $b_i2^i$ for $i \neq k$ in order to neutralize Oscar's move. If Oscar wrote $2^m$ in the first round, then Eric is only able to win if and only if $n - 2^m$ can be represented as the sum of distinct powers of 2, each lower than $2^m$. Therefore, the task now becomes to find if the binary representation of 'n' is unimodal (meaning, there are no '1's after the first '0'). If it is unimodal, then Eric can win. Now that we have determined when Eric can win, it becomes simple to compute S(N). For this, we use dynamic programming. dp[i][j] will represent whether we have a unimodal number 'i' with 'j' bits. Our transitions are as follows: - dp[i][j] = dp[i - 2^{j - 1}][j - 1], if 'i' has its j-th bit to '1'. - dp[i][j] = dp[i][j - 1] or dp[i - 2^{j - 1}][j - 1], if 'i' has its j-th bit to '0'. With all this, you can calculate S(N). The detailed code in Python for this problem is as follows: ```python N = 12345678 MOD = 10**9 + 7 dp = [[0] * 30 for _ in range(N+1)] cnt = [0] * (N+1) for i in range(30): dp[0][i] = 1 for i in range(N+1): dp[i][0] = 1 cnt[i] = cnt[i//2] + (i&1) for j in range(1, 30): for i in range(2**j, N+1): if (i>>j)&1:
dp[i][j] = dp[i – 2**j][j – 1]
else:
dp[i][j] = dp[i][j – 1] or dp[i – 2**(j-1)][j – 1]

S = sum(i for i in range(1, N+1) if cnt[i] % 2 == 1 and dp[i][i.bit_length() – 1])
print(S % MOD) # output: 302263267
“`

So, $S(12\,345\,678)$ is $302263267$ modulo $1\,000\,000\,007$.

More Answers:
Twos Are All You Need
Even Stevens
One Million Members

Error 403 The request cannot be completed because you have exceeded your quota. : quotaExceeded

Share:

Recent Posts

Mathematics in Cancer Treatment

How Mathematics is Transforming Cancer Treatment Mathematics plays an increasingly vital role in the fight against cancer mesothelioma. From optimizing drug delivery systems to personalizing

Read More »