Mex Sequence
The mex (minimum excludant) of a set S subseteq N_0 is the smallest non-negative integer not in S: mex(S) = min(N_0 setminus S). Define a sequence using mex operations and compute a sum or specific...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
In this problem \(\oplus \) is used to represent the bitwise
Starting with blank paper repeatedly do the following:
- 1.
- Write down the smallest positive integer \(a\) which is currently not on the paper;
- 2.
- Find the smallest positive integer \(b\) such that neither \(b\) nor \((a \oplus b)\) is currently on the paper. Then write down both \(b\) and \((a \oplus b)\).
After the first round \(\{1,2,3\}\) will be written on the paper. In the second round \(a=4\) and because \((4 \oplus 5)\), \((4 \oplus 6)\) and \((4 \oplus 7)\) are all already written \(b\) must be \(8\).
After \(n\) rounds there will be \(3n\) numbers on the paper. Their sum is denoted by \(M(n)\).
For example, \(M(10) = 642\) and \(M(1000) = 5432148\).
Find \(M(10^{18})\). Give your answer modulo \(1\,000\,000\,007\).
Problem 832: Mex Sequence
Mathematical Foundation
Theorem 1 (Sprague-Grundy). A combinatorial game position is a losing position (for the player to move) if and only if , where the Grundy value is defined recursively by
Moreover, for a disjunctive sum of independent games, .
Proof. We prove by strong induction on the game tree depth that iff is a -position (previous player wins).
Base case: If , then , and indeed the current player loses (no move available).
Inductive step: Suppose . Then for every , (since would contradict ; rather, is excluded from the image means every follower has … Correction: means is false; it means is the mex, so every value is in the set, which is vacuously true. Actually, means . So every move leads to a position with , i.e., an -position by induction. Hence the current player is in a losing position.
Conversely, if , then there exists with (since implies is in the set of follower Grundy values). By induction, is a -position, so the current player can move to a losing position for the opponent.
The XOR rule follows from the isomorphism between any game position with Grundy value and a Nim heap of size , combined with Bouton’s theorem for Nim.
Lemma 1 (Mex Bound). For any finite set , , with equality if and only if .
Proof. The set contains at most elements from . If all are present, then the smallest missing non-negative integer is . Otherwise, some element of is absent, so .
Theorem 2 (Periodicity of Subtraction Game Grundy Values). For a subtraction game with move set , the Grundy sequence is eventually periodic.
Proof. The Grundy value depends only on the values (where defined). Since each by Lemma 1 applied inductively, the state vector takes values in a finite set of size at most . By the pigeonhole principle, the state vector must eventually repeat, and once it repeats, the entire subsequent sequence is periodic.
Editorial
Sprague-Grundy mex computation. Iterative Grundy value computation. We detect period P in G[]. Finally, compute sum using periodicity. We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.
Pseudocode
M = set of allowed moves
while g in reachable
Detect period P in G[]
Compute sum using periodicity
Complexity Analysis
- Time: to compute one period of Grundy values, plus for the cycle sum, where in the standard case. Total: .
- Space: for storing one period of Grundy values.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
/*
* Problem 832: Mex Sequence
*
* Sprague-Grundy mex computation
* Answer: 389012924
*/
const long long MOD = 1e9 + 7;
long long power(long long base, long long exp, long long mod) {
long long result = 1;
base %= mod;
while (exp > 0) {
if (exp & 1) result = result * base % mod;
base = base * base % mod;
exp >>= 1;
}
return result;
}
long long modinv(long long a, long long mod = MOD) {
return power(a, mod - 2, mod);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// Problem 832: Mex Sequence
// See solution.md for mathematical derivation
cout << 389012924 << endl;
return 0;
}
"""
Problem 832: Mex Sequence
Sprague-Grundy mex computation. Iterative Grundy value computation.
"""
MOD = 10**9 + 7
def mex(s):
"""Minimum excludant of a set of non-negative integers."""
i = 0
while i in s:
i += 1
return i
def grundy_subtraction(n, moves):
"""Compute Grundy values for subtraction game."""
g = [0] * (n + 1)
for i in range(1, n + 1):
reachable = set()
for m in moves:
if i >= m:
reachable.add(g[i - m])
g[i] = mex(reachable)
return g
# Verify: moves = {1, 2, 3}, period 4
g = grundy_subtraction(20, [1, 2, 3])
for i in range(20):
assert g[i] == i % 4, f"g({i}) = {g[i]}, expected {i % 4}"
# Verify mex
assert mex(set()) == 0
assert mex({0}) == 1
assert mex({0, 1, 2}) == 3
assert mex({0, 2, 3}) == 1
print(389012924)