Write code in C++ to solve the problem below specified in an efficient manner. Use mathematical principles to reduce the computational complexity of the problem but do not investigate methods that only approximate the solution. Use std::algorithm and other features that simplify the code as much as possible. Do not use "using namespace std" in the code. Write the code to be as efficient as possible and easy for a human to read and understand. Use comments as necessary to explain complex functionality. Use the library <primesieve.hpp> as needed to generate primes and Boost multiprecision library for numbers exceeding 64 bits. Write solution to stdout with an std::endl at the end. Do not encapsulate the code in a namespace. Do not reference these instructions in the comments. Do not use placeholders, incomplete logic or print out a hardcoded value. Do not add includes that are unnecessary. Do not use non-ASCII characters in the code or comments.
To solve the problem efficiently, we recognize that it is equivalent to computing Grundy numbers for each word and checking the XOR (nimber) sum of strategies to determine the winner — making this a combinatorial game theory (Sprague-Grundy Theorem) problem.
Key Observations:
Each word of length n is reduced independently by players.
Moves are asymmetric: Left truncates from the left, Right from the right.
The game ends when all words are 1-letter long, at which point we count how many Ls and how many Rs there are and see who has the majority (odd number of words means no tie).
Strategy:
Each word can be modeled as a separate game. The Sprague-Grundy theorem tells us that we can associate a Grundy number (nimber) with each position. The overall position has a Grundy number equal to the XOR of the Grundy numbers of each individual word.
Right has a winning strategy (assuming Left plays first) if the Grundy XOR of all words is 0.
So we have two goals:
Compute the Grundy number for any word of length n.
Since the rules are uniform, and there are only two possible moves available (trimming from the side), we can memoize Grundy numbers for each possible word.
Count how many k-tuples of words of length n (with replacement, order matters) have XOR of Grundy numbers = 0, and among the resulting 1-letter words, R appears more than L.
Approach Summary:
Enumerate all possible words of length n over {'L', 'R'}.
For each word, compute:
Its Grundy number.
Whether its single-letter forms are L or R (trivial here: the last letter after moves).
Keep a frequency map: (grundy number, final_char) -> count.
DP: For k = number of words.
Iterate all k-tuples (multi-set with order) and compute XOR of Grundy numbers.
If the total final R count is more than L, and XOR == 0 → Right wins.
Count such configurations.
Use modulo arithmetic.
Use Boost multiprecision due to large values.
✔️ Efficient Implementation with Memoization and DP:
cpp
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <boost/multiprecision/cpp_int.hpp>
constexpr int MOD = 1001001011;
constexpr int N = 20;
constexpr int K = 7;
using BigInt = boost::multiprecision::uint128_t;
// Memoization for Grundy numbers
std::unordered_map<std::string, int> grundy_cache;
// Calculate the minimal excludant (mex)
int mex(const std::vector<int>& s) {
std::vector<bool> found(s.size() + 5, false);
for (int g : s) {
if (g < found.size()) found[g] = true;
}
for (std::size_t i = 0; i < found.size(); ++i) {
if (!found[i]) return static_cast<int>(i);
}
return 0;
}
// Compute Grundy number recursively
int grundy(const std::string& word) {
auto it = grundy_cache.find(word);
if (it != grundy_cache.end()) return it->second;
if (word.size() == 1) return 0; // Terminal word has Grundy number 0
std::vector<int> next;
// Left moves: remove 1 to len-1 letters from the left, but not all
for (std::size_t i = 1; i < word.size(); ++i) {
std::string next_word = word.substr(i);
next.push_back(grundy(next_word));
}
// Right moves: remove 1 to len-1 letters from the right, but not all
for (std::size_t i = 1; i < word.size(); ++i) {
std::string next_word = word.substr(0, word.size() - i);
next.push_back(grundy(next_word));
}
int g = mex(next);
grundy_cache[word] = g;
return g;
}
int main() {
std::map<std::pair<int, char>, BigInt> freq;
// Generate all 2^N words of length N over {'L','R'}
int total_words = 1 << N;
for (int mask = 0; mask < total_words; ++mask) {
std::string word;
for (int i = N - 1; i >= 0; --i) {
if (mask & (1 << i)) {
word += 'R';
} else {
word += 'L';
}
}
int g = grundy(word);
char last_char = word[word.size() - 1]; // The only char left at the end
freq[{g, last_char}] += 1;
}
// Dynamic programming table
// dp[cnt][xor][r_count] = number of combinations
std::vector<std::vector<std::vector<BigInt>>> dp(
K + 1, std::vector<std::vector<BigInt>>(64, std::vector<BigInt>(K + 1, 0)));
// Base case: zero words
dp[0][0][0] = 1;
for (int cnt = 0; cnt < K; ++cnt) {
for (int x = 0; x < 64; ++x) {
for (int rc = 0; rc <= cnt; ++rc) {
BigInt current = dp[cnt][x][rc];
if (current == 0) continue;
for (const auto& [key, f] : freq) {
int g2 = key.first;
char c = key.second;
int rc2 = rc + (c == 'R' ? 1 : 0);
if (rc2 > K) continue;
dp[cnt + 1][x ^ g2][rc2] = (dp[cnt + 1][x ^ g2][rc2] + current * f) % MOD;
}
}
}
}
BigInt result = 0;
for (int rcount = (K + 1) / 2; rcount <= K; ++rcount) {
result = (result + dp[K][0][rcount]) % MOD;
}
std::cout << result << std::endl;
return 0;
}
Explanation:
Generates all 2^20 possible words of length 20.
Computes Grundy numbers for each word via DFS + memoization.
Counts how many words have the same (Grundy, last char).
Uses a triple DP table to track:
Number of words considered so far.
Combined XOR of Grundy numbers.
How many resulting single-letter words are 'R'.
Final step: sum up all combinations where:
Overall XOR is 0.
More 'R's than 'L's (since there are 7 words, that means at least 4 'R's).
Modulo operation is done throughout.
✅ Final Result
Once compiled and run, this program will output the result for:
code
G(20, 7) mod 1001001011
On most systems, it might take a few minutes due to 2^20 words — but this algorithm is optimal given the constraints.