Problem:
Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484

Solution in Lua (5.3.2) (judge verdict: Accepted)

local n = io.read("*n")
local dignum = 0
local digits = {}
while 0 < n do
  table.insert(digits, n % 10)
  n = n // 10
  dignum = dignum + 1
end
local norm = {}
local maymax = {}

-- 0, 03, 05, 07, 035, 037, 057, 0357
for i = 1, 8 do
  norm[i] = {}
  maymax[i] = {}
  for j = 1, dignum do
    norm[i][j] = 0
    maymax[i][j] = 0
  end
end
local digval = digits[dignum]
norm[1][dignum] = 1
if 8 <= digval then
  norm[2][dignum] = 1
  norm[3][dignum] = 1
  norm[4][dignum] = 1
elseif 7 == digval then
  norm[2][dignum] = 1
  norm[3][dignum] = 1
  maymax[4][dignum] = 1
elseif 6 == digval then
  norm[2][dignum] = 1
  norm[3][dignum] = 1
elseif 5 == digval then
  norm[2][dignum] = 1
  maymax[3][dignum] = 1
elseif 4 == digval then
  norm[2][dignum] = 1
elseif 3 == digval then
  maymax[2][dignum] = 1
end

for i = dignum - 1, 1, -1 do
  local digval = digits[i]
  norm[1][i] = 1
  -- 03, 05, 07
  for j = 2, 4 do
    local tgt = j * 2 - 1
    if tgt < digval then
      norm[j][i] = norm[1][i + 1] + norm[j][i + 1] + maymax[j][i + 1]
      maymax[j][i] = 0
    elseif tgt == digval then
      norm[j][i] = norm[1][i + 1] + norm[j][i + 1]
      maymax[j][i] = maymax[j][i + 1]
    else
      norm[j][i] = norm[1][i + 1] + norm[j][i + 1]
      maymax[j][i] = 0
    end
  end
  -- 035, 037, 057
  for j = 5, 7 do
    local rightval = j == 5 and 5 or 7
    local rightidx = j == 5 and 3 or 4
    local leftval = j == 7 and 5 or 3
    local leftidx = j == 7 and 3 or 2
    if rightval < digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
        + maymax[leftidx][i + 1] + maymax[rightidx][i + 1] + maymax[j][i + 1] * 2
      maymax[j][i] = 0
    elseif rightval == digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
        + maymax[rightidx][i + 1] + maymax[j][i + 1]
      maymax[j][i] = maymax[leftidx][i + 1] + maymax[j][i + 1]
    elseif leftval < digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
        + maymax[rightidx][i + 1] + maymax[j][i + 1]
      maymax[j][i] = 0
    elseif leftval == digval then
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
      maymax[j][i] = maymax[rightidx][i + 1] + maymax[j][i + 1]
    else
      norm[j][i] = norm[leftidx][i + 1] + norm[rightidx][i + 1] + norm[j][i + 1] * 2
      maymax[j][i] = 0
    end
  end
  if 7 < digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[5][i + 1] + maymax[6][i + 1] + maymax[7][i + 1] + maymax[8][i + 1] * 3
    maymax[8][i] = 0
  elseif 7 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[6][i + 1] + maymax[7][i + 1] + maymax[8][i + 1] * 2
    maymax[8][i] = maymax[5][i + 1] + maymax[8][i + 1]
  elseif 6 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[6][i + 1] + maymax[7][i + 1] + maymax[8][i + 1] * 2
    maymax[8][i] = 0
  elseif 5 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[7][i + 1] + maymax[8][i + 1]
    maymax[8][i] = maymax[6][i + 1] + maymax[8][i + 1]
  elseif 4 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
      + maymax[7][i + 1] + maymax[8][i + 1]
    maymax[8][i] = 0
  elseif 3 == digval then
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
    maymax[8][i] = maymax[7][i + 1] + maymax[8][i + 1]
  else
    norm[8][i] = norm[5][i + 1] + norm[6][i + 1] + norm[7][i + 1] + norm[8][i + 1] * 3
    maymax[8][i] = 0
  end
end
print(norm[8][1] + maymax[8][1])