Binary Palindrome Sums
A binary palindrome is a positive integer whose binary representation (with no leading zeros) is a palindrome. Find the sum of all binary palindromes less than 2^30.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
de Bruijn has a digital combination lock with \(k\) buttons numbered \(0\) to \(k-1\) where \(k \le 10\).
The lock opens when the last \(n\) buttons pressed match the preset combination.
Unfortunately he has forgotten the combination. He creates a sequence of these digits which contains every possible combination of length \(n\). Then by pressing the buttons in this order he is sure to open the lock.
Consider all sequences of shortest possible length that contains every possible combination of the digits.
Denote by \(C(k, n)\) the lexicographically smallest of these.
For example, \(C(3, 2) = \) 0010211220.
Define the sequence \(a_n\) by \(a_0=0\) and
\[a_n=(920461 a_{n-1}+800217387569)\bmod 10^{12} \text { for }\ n > 0\] Interpret each \(a_n\) as a \(12\)-digit combination, adding leading zeros for any \(a_n\) with less than \(12\) digits.
Given a positive integer \(N\), we are interested in the order the combinations \(a_1,\dots ,a_N\) appear in \(C(10,12)\).
Denote by \(p_n\) the
For example, the combination \(a_1=800217387569\) is entered before \(a_2=696996536878\). Therefore: \[F(2)=1\cdot 800217387569 + 2\cdot 696996536878 = 2194210461325\] You are also given \(F(10)=32698850376317\).
Find \(F(10^7)\). Give your answer modulo \(1234567891\).
Problem 941: Binary Palindrome Sums
Mathematical Foundation
Definition 1. A positive integer is a binary palindrome if its binary representation satisfies for all , where (no leading zeros).
Theorem 1. The number of -bit binary palindromes is for .
Proof. A -bit binary palindrome is determined by its first bits. The leading bit is fixed. The remaining bits are free, each independently 0 or 1. The second half is determined by symmetry: . Hence there are exactly palindromes of bit-length .
Lemma 1. Every -bit binary palindrome with satisfies , and every with has bit-length at most 30.
Proof. A -bit number satisfies . For , we have . Conversely, implies has at most 30 bits.
Theorem 2 (Sum Formula). Let denote the number of free bits for -bit palindromes. The sum of all -bit binary palindromes is:
for even , and
for odd (with ).
Proof. Each palindrome has value (for even , ). The leading and trailing bits contribute to each palindrome. Each free bit at position also appears at the mirror position , contributing when . Over all palindromes, each free bit is 1 in exactly half, giving factor . For odd , the middle bit at position is its own mirror.
Editorial
Compute the sum of all binary palindromes less than 2^30. A binary palindrome is a positive integer whose binary representation reads the same forwards and backwards (no leading zeros). Generation strategy: For each bit length k (1..30), generate palindromes by choosing the first ceil(k/2) bits (with MSB = 1), then mirroring to produce the full k-bit palindrome. Results:.
Pseudocode
total = 0
For k from 1 to max_bits:
For half from 2^(ceil(k/2) - 1) to 2^ceil(k/2) - 1:
palindrome = BuildPalindrome(half, k)
total += palindrome
Return total
bits = binary_digits(half, ceil(k/2))
full = bits + reverse(bits[0 : floor(k/2)]) // mirror first half
Return binary_to_int(full)
Complexity Analysis
- Time: . With per palindrome for construction, total is .
- Space: auxiliary (palindromes generated on the fly).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main(){
long long total=0;
int cnt=0;
for(int k=1;k<=30;k++){
int half=(k+1)/2;
for(int fh=(1<<(half-1));fh<(1<<half);fh++){
long long val=0;
vector<int> bits;
int t=fh;
for(int i=0;i<half;i++){bits.push_back(t&1);t>>=1;}
reverse(bits.begin(),bits.end());
vector<int> full=bits;
if(k%2==1){
for(int i=half-2;i>=0;i--) full.push_back(bits[i]);
} else {
for(int i=half-1;i>=0;i--) full.push_back(bits[i]);
}
if((int)full.size()!=k) continue;
for(int b:full) val=val*2+b;
if(val>0&&val<(1LL<<30)){total+=val;cnt++;}
}
}
cout<<total<<endl;
return 0;
}
"""
Problem 941: Binary Palindrome Sums
Compute the sum of all binary palindromes less than 2^30.
A binary palindrome is a positive integer whose binary representation reads
the same forwards and backwards (no leading zeros).
Generation strategy:
For each bit length k (1..30), generate palindromes by choosing the
first ceil(k/2) bits (with MSB = 1), then mirroring to produce the
full k-bit palindrome.
Results:
- 1-bit: {1}
- 2-bit: {3} (11)
- 3-bit: {5, 7} (101, 111)
- Count of binary palindromes < 2^30: computed below
Methods:
1. gen_binary_palindromes -- generate all binary palindromes up to max_bits
2. count_by_bitlength -- count palindromes grouped by bit length
3. sum_by_bitlength -- partial sums by bit length
4. is_binary_palindrome -- direct check (verification)
"""
def gen_binary_palindromes(max_bits=30):
"""Generate all binary palindromes with at most max_bits bits, in sorted order."""
pals = []
for k in range(1, max_bits + 1):
if k == 1:
pals.append(1)
continue
half = (k + 1) // 2
for first_half in range(1 << (half - 1), 1 << half):
bits = bin(first_half)[2:]
if k % 2 == 1:
full = bits + bits[-2::-1]
else:
full = bits + bits[::-1]
val = int(full, 2)
if val < (1 << max_bits):
pals.append(val)
return sorted(pals)
def count_by_bitlength(pals):
"""Return dict mapping bit_length -> count."""
counts = {}
for p in pals:
bl = p.bit_length()
counts[bl] = counts.get(bl, 0) + 1
return counts
def sum_by_bitlength(pals):
"""Return dict mapping bit_length -> sum of palindromes."""
sums = {}
for p in pals:
bl = p.bit_length()
sums[bl] = sums.get(bl, 0) + p
return sums
def is_binary_palindrome(n):
"""Check if n is a binary palindrome."""
b = bin(n)[2:]
return b == b[::-1]
# Verification
assert is_binary_palindrome(1) # 1
assert is_binary_palindrome(3) # 11
assert is_binary_palindrome(5) # 101
assert is_binary_palindrome(7) # 111
assert not is_binary_palindrome(2) # 10
assert not is_binary_palindrome(6) # 110
# Brute-force check for small range
brute_small = [n for n in range(1, 1024) if is_binary_palindrome(n)]
gen_small = [p for p in gen_binary_palindromes(10) if p < 1024]
assert brute_small == gen_small, "Generator mismatch with brute force"
# Computation
pals = gen_binary_palindromes(30)
answer = sum(pals)
print(answer)