I write immediately the idea of a normal solution (an idea, because I think that the tasks from the thymus should be driven by ourselves). Normal, this means immediately in complete restrictions and not trimmed versions (which, by the way, should lead to this decision).
- We don’t need the first digit at all.
- By and large, we don’t care what figure, the main thing is 0 or not.
The solution to this problem is to generate a mask of 0 / not 0. (2 ^ 16 variants in the worst case). Further for each mask you need to add 9 ^ to the answer (the number 1 is not a mask). Do not forget that the first element in the mask is always 1.
- Suppose we have placed i digits, to place i + 1, we need to know only the value of i digits, the rest of them do not interfere with us.
The solution of a more complicated problem is the dynamics F [i] [d] - the number of ways to make i first digits so that the last digit is d (we remember that only 0 is important to us or not). The recalculation is obvious (from 0 only to 1 and k-1 mode, from 1 to 1 also k-1 and from 1 to 0 1 method). Base F [1] [1] = K-1. O (N) complexity.
Then you can pack all this into a formula, it is not difficult to deduce it, the formula will be reduced to a rapid exponentiation, and the complexity will drop to O (log N).