Question
A more efficient algorithm in finding the number of distinct decompositions a number has using only repdigits
This is a problem from a previous semester that I am trying to upsolve. I am trying to solve a problem involving the total number of ways of decomposing a number using only repdigits. A repdigit is a positive integer consisting of only a single digit, e.g. numbers 1-9, 11, 22, 3333, and so on.
To illustrate, say we have n = 3:
1+1+1
1+2
2+1
3
which totals 4. Note that 1+2 and 2+1 are considered distinct here.
Now I have a solution in python that uses dynamic programming. I thought that it would be efficient enough but apparently it still exceeds the time limit (we use an online judge created by the university to automatically check code). The limits of the problem is up to n == 90000 with only a 5 second limit. We can only use python. For reference, here is my code:
def count_repdigit_decompositions(n: int) -> int:
rep: list[int] = []
for digit in range(1, 10):
repdigit = digit
while repdigit <= n:
rep.append(repdigit)
repdigit = repdigit * 10 + digit
rep.sort()
dp = [0] * (n + 1)
dp[0] = 1
for i in range(1, n + 1):
for repdigit in rep:
dp[i] += dp[i - repdigit]
return dp[n]
I have tried looking into oeis (https://oeis.org/search?q=1%2C+1%2C+2%2C+4%2C+8%2C+16%2C+32%2C+64%2C+128%2C+256%2C+511%2C+1022&go=Search) for the pattern to this but it only shows for the palindromic decomposition which makes sense since the first few palindromes match exactly of that of the repdigits. How do I make this more efficient?