Problem 45. Numbers Divisible by 5 and 7

This problem is from 2003 Indian Mathematical Olympiad.
How many 7-digit numbers are there whose every digit is either 5 or 7 and the number is divisible by both 5 and 7?
Challenging version: The same question, but with 13 digits: How many 13-digit numbers are there whose every digit is 5 or 7 and the number is divisible by both 5 and 7?
Link to the problem on Twitter: https://twitter.com/Riazi_Cafe/status/1709796691770323356
. The answer for the 7-digits case is equal to 9 and for the 13-digits case it is equal to 585. In general, for the \(6n+1\)-digits case the solution is equal to \(\frac{2^{6n}-1}{7}\).
First, we try to solve the problem for the 7-digits case. For a number to be divisible by 5, the rightmost digit must be either 0 or 5. Since we are only allowed to have digits \(5\) and \(7\) in the desired numbers, then their rightmost digit should be equal to \(5\). Therefore, all desired numbers can be written as \(abcdef5\). In order for the number to be divisible by 7, the remainder of \(abcdef0\) over 7 must be equal to 2. This quantity can be formulated as:
\[\begin{aligned} abcdef0 \overset{7}{\equiv} &f \cdot 10 + e \cdot 10^2 + d \cdot 10^3 + \\ &c \cdot 10^4 + b \cdot 10^5 + a \cdot 10^6 \end{aligned}\]
Notice that putting a digit as 7 is equivalent to putting that digit as 0 when we only care about its module over 7. Thus we can interpret the problem as choosing a subset \(s\) of \(X = \{ a, b, c, d, e, f\}\) whose assigned values are equal to 5 in a way that \(f \cdot 10 + e \cdot 10^2 + d \cdot 10^3 + c \cdot 10^4 + b \cdot 10^5 + a \cdot 10^6 \overset{7}{\equiv} 2\) when the values of digits \(X \setminus s\) are all zero . Moreover, \(5 \cdot 10^i\) module 7 is equal to 1, 3, 2, 6, 4, 5 for \(1 \leq i \leq 6\) respectively (notice that each number from 1 to 6 appears exactly once in the sequence). Therefore, the problem is equivalent to counting the subsets of \(\{1, 2, 3, 4, 5, 6\}\) whose sum module 7 is equal to 2.
A simple case analysis shows that there are 9 such subsets:
Such subsets of length 1: \(\{2\}\).
Such subsets of length 2: \(\{3,6\}\), \(\{4,5\}\).
Such subsets of length 3: \(\{1,2,6\}\), \(\{1,3,5\}\), \(\{2,3,4\}\).
Such subsets of length 4: \(\{1,4,5,6\}\), \(\{2,3,5,6\}\).
Such subsets of length 5: \(\{1,2,3,4,6\}\).
If we continue computing \(5 \cdot 10^i\) module 7 for \(8 \leq i \leq 13\) we obtain the same sequence of numbers (1, 3, 2, 6, 4, 5). That is, to find the answer for the 13-digits case, we need to find the number of multisubsets of a multiset that has two appearances of each digit between 1 and 6.
We prove below that (Lemma [lemma:module7]) if \(X_n\) is a multiset that contains each digit between \(1\) and \(6\) exactly \(n\) times, then the number of its multisubsets whose sum module 7 is equal to 2 is equal to \(\frac{2^{6n}-1}{7}\) which for the case of \(n=2\) (for the 13-digits case) is equal to 585.
[lemma:module7] Let \(X_n\) be a multiset that contains each digit between \(1\) and \(6\) exactly \(n\) times. The number of multisubsets of \(X_n\) whose sum of values module 7 is equal to \(i\) is equal to: \[\left\{ \begin{array}{lr} \frac{2^{6n}+6}{7} & \text{for } i=0\\ \frac{2^{6n}-1}{7} & \text{for } 1\leq i\leq 6 \end{array} \right\}\]
Since 7 is a prime number, each of numbers 1 to 6 has a unique multiplicative inverse in module 7. If we have a multisubset whose sum module 7 is equal to \(p\), by multiplying each of its members by \(qp^{-1}\) (module 7) the sum of the values of the resulting multisubset module 7 is equal to \(q\). Since 7 is prime, such a multiplication gives a 1-to-1 mapping and thus the resulting multisubset is a multisubset of the original multiset. This implies that the number of multisubsets of \(X_n\) whose sum module 7 is equal to \(i\) are exactly the same for each \(1 \leq i \leq 6\). Next, we use induction to finish the proof:
Base \((n=1)\): We already discussed that for \(X_1 = \{1,2,3,4,5,6\}\), the number of subsets whose sum module \(7\) is equal to 2 is 9. Due to what mentioned above the same holds for the number of substes whose sum module 7 is equal to \(1\), \(3\), \(5\), and \(6\). Therefore, the number of subsets whose sum is divisible by \(7\) is equal to \(2^6-9\cdot 6=10\) which is as desired.
Induction step \((n>1)\): To choose a multisubset of \(X_n\) whose sum is divisible by 7, we have 7 choices:
(option 1): Choose a multisubset of \(X_{n-1}\) whose sum is divisible by 7 and append it to a multisubset of \(X_1\) whose sum is divisible by 7.
(options 2-7 for \(1 \leq i \leq 6\)) Choose a multisubset of \(X_{n-1}\) whose sum module \(7\) is equal to \(i\) and append it to a multisubset of \(X_1\) whose sum module 7 is \(7-i\).
Therefore, the total number of multisubsets of \(X_n\) whose sum is divisible by 7 is: \[6 \cdot 9 \cdot \frac{2^{6n-6} - 1}{7} + 10 \cdot \frac{2^{6n-6}+6}{7} = \frac{54 \cdot 2^{6n-6} - 54 + 10 \cdot 2^{6n-6} + 60}{7} = \frac{2^{6n} + 6}{7}.\] It follows from our discussions above that for any \(1 \leq i \leq 6\), the number of multisubsets of \(X_n\) whose sum module \(7\) is equal to \(i\) is equal to \[\frac{2^{6n} - \frac{2^{6n}+6}{7}}{6} = \frac{2^{6n}-1}{7}.\]