Bringing Order to Disorder

Count the n-digit strings that come before the given string ordered by digit sum, then shifted-digit product, then numeric value.

Medium7Dynamic programmingCombinatoricsMathNo attempts yetTime limit1sMemory limit256 MB

Problem

A sequence of digits usually represents a number, but another interpretation is possible. This problem defines a new order relation \prec between digit sequences of the same length.

Let s=d1d2dns = d_1 d_2 \cdots d_n be a sequence of nn digits, where each did_i (1in)(1 \le i \le n) is one of 0,1,,90, 1, \ldots, 9. Define sum(s)\mathrm{sum}(s), prod(s)\mathrm{prod}(s), and int(s)\mathrm{int}(s) as follows.

  • sum(s)=d1+d2++dn\mathrm{sum}(s) = d_1 + d_2 + \cdots + d_n
  • prod(s)=(d1+1)×(d2+1)××(dn+1)\mathrm{prod}(s) = (d_1 + 1) \times (d_2 + 1) \times \cdots \times (d_n + 1)
  • int(s)=d1×10n1+d2×10n2++dn×100\mathrm{int}(s) = d_1 \times 10^{n-1} + d_2 \times 10^{n-2} + \cdots + d_n \times 10^0

int(s)\mathrm{int}(s) is the integer that the sequence ss represents under the usual decimal reading.

For two sequences s1s_1 and s2s_2 of the same length, s1s2s_1 \prec s_2 (s1s_1 is less than s2s_2) holds if and only if one of the following three conditions holds.

  1. sum(s1)<sum(s2)\mathrm{sum}(s_1) < \mathrm{sum}(s_2)
  2. sum(s1)=sum(s2)\mathrm{sum}(s_1) = \mathrm{sum}(s_2) and prod(s1)<prod(s2)\mathrm{prod}(s_1) < \mathrm{prod}(s_2)
  3. sum(s1)=sum(s2)\mathrm{sum}(s_1) = \mathrm{sum}(s_2), prod(s1)=prod(s2)\mathrm{prod}(s_1) = \mathrm{prod}(s_2), and int(s1)<int(s2)\mathrm{int}(s_1) < \mathrm{int}(s_2)

For sequences of length 22, the order runs like this.

0001100220110330122189989900 \prec 01 \prec 10 \prec 02 \prec 20 \prec 11 \prec 03 \prec 30 \prec 12 \prec 21 \prec \cdots \prec 89 \prec 98 \prec 99

Given a sequence ss of nn digits, count the sequences of nn digits that are less than ss under the order \prec defined above.

Input

The input is a single line.

d1d2...dn

nn is a positive integer at most 1414, and each of d1,d2,,dnd_1, d_2, \ldots, d_n is a digit from 00 to 99. A sequence whose first digit is 00 can be given.

Output

Print, on a single line, the number of digit sequences of length nn that are less than d1d2dnd_1 d_2 \ldots d_n under the order defined above.