## Given any positive integer $n$, we can construct a new integer by inserting plus signs between some of the digits of the base $B$ representation of $n$, and then carrying out the additions.

For example, from $n=123_{10}$ ($n$ in base $10$) we can construct the four base $10$ integers $123_{10}$, $1+23=24_{10}$, $12+3=15_{10}$ and $1+2+3=6_{10}$.

Let $f(n,B)$ be the smallest number of steps needed to arrive at a single-digit number in base $B$. For example, $f(7,10)=0$ and $f(123,10)=1$.

Let $g(n,B_1,B_2)$ be the sum of the positive integers $i$ not exceeding $n$ such that $f(i,B_1)=f(i,B_2)$.

You are given $g(100,10,3)=3302$.

Find $g(10^7,10,3)$.

### This problem can be solved using a combination of basic math knowledge, pattern recognition, and programming.

First, let’s understand what the problem is asking for.

– $f(n,B)$ represents the minimum number of steps it takes to reduce a number to a single digit by inserting plus signs between digits in a base $B$ representation of $n$.

– $g(n,B_1,B_2)$ is the sum of all positive integers $i$ up to $n$ such that $f(i,B_1) = f(i,B_2)$.

Given $g(100,10,3)=3302$, we need to compute $g(10^7,10,3)$.

Solving this problem directly for $n = 10^7$ would be computationally prohibitive. However, we can solve this problem more efficiently with dynamic programming by recognizing the patterns in the problem.

First, realize that $f(n,B)$ only depends on the sum of digits of $n$ in base $B$, not $n$ itself. Define $F(S,B)$ as the minimum number of steps required to reduce a number with digit sum $S$ in base $B$ to a single digit. The recurrence relation for $F(S,B)$ can be written as follows:

– $F(S,B) = 0$ if $S < B$
- $F(S,B) = 1 + F(S \mod B + S / B, B)$ otherwise
Second, we calculate $F(S,10)$ and $F(S,3)$ for all $0 ≤ S ≤ 10^7$ employing this recurrence relation in a bottom-up manner, i.e., by first calculating $F(S,B)$ for small $S$ and iterating to larger values.
Then, we convert $g(n,B_1,B_2)$ into a format that facilitates computation. Let the base $B_1$ representation of $n$ be $d_SD_{S-1}...d_1d_0{[B_1]}$ with $d_S$ being the highest non-zero digit. Then we can write $g(n,B_1,B_2)$ as below, leveraging the fact that $g$ counts all numbers i < n with the same $f$ values in both bases:
- $g(n,B_1,B_2) = g(d_SD_{S-1}...d_1d_0{[B_1]}, B_1, B_2)$
- $+ \sum_{i=0}^{d_S-1}g(iD_{S-1}...d_1d_0{[B_1]}, B_1, B_2)$ whenever $d_S > 0$

Finally, construct the table of values for $g(X,B_1,B_2)$ to speed up the computation, and use the formula above to incrementally build the result.

The exact computation will require an implementation in a programming language, most likely Python or C++ due to the computational nature and size of the problem.

Please note that the above explanation assumes familiarity with dynamic programming and modulo arithmetic, which are usually covered in introductory computer science and discrete mathematics courses. If you need further explanation on these topics, please let me know!

##### More Answers:

Square Prime Factors IINumbers of the Form $a^2b^3$

Subset Sums