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 Ruby (2.3.3) (judge verdict: Accepted)


DBG =  !true
DBG2 =  !true
DBGf =  !true
DBGg =  !true

def f(j, b, x)
  if b == 0
    return (x>=j ? 1 : 0)
  end
  msb = x / (10**b)
  rest = x % (10**b)
  rest0 = (10**b)-1
  if msb == 0
    return f(j, b-1, rest)
  end
  ret = f(j, b-1, rest0)  # msb 0
  #ret += (msb >= j ? 1 : 0) # msb j
  if msb < j
    ret += 0  # dmy
  elsif msb == j
    ret += ff(j,b-1,rest)
  elsif msb > j
    ret += 1
  end
  printf("f(%d,%d,%d) returning %d\n",
        j, b, x, ret) if DBGf
  return ret
end

def ff(j, b, x)  # assume i<j  zero not allowed
  if b == 0
    return (x>=j ? 1 : 0)
  end
  msb = x / (10**b)
  rest = x % (10**b)
  printf("   in-ff) x:%d b: %d msb:%d rest:%d\n",
         x, b, msb, rest) if DBGf
  ret = 0  # msb<i
  if msb < j
    ret += 0  # dmy
  elsif msb == j
    ret = ff(j,b-1,rest)  # msb i
  elsif msb > j
    ret = 1    # msb ij
  end
  printf("ff(%d,%d,%d) returning %d\n",
         j,b,x, ret) if DBGf
  return ret
end

def g(i, j, b, x)  # assume i<j  upper bits may be zero
  if b == 0
    return (x>=j ? 2 : x>=i ? 1 : 0)
  end
  msb = x / (10**b)
  rest = x % (10**b)
  return g(i,j,b-1,rest) if msb==0
  rest0 = (10**b)-1
  printf("in-g) x:%d b: %d msb:%d rest:%d\n",
         x, b, msb, rest) if DBGg
  ret = g(i,j,b-1,rest0)  # msb 0
  if msb < i
    ret += 0  # dmy
  elsif msb == i
    ret += gg(i,j,b-1,rest)   # msb i
  elsif msb < j
    ret += 2**b   # msb i
  elsif msb == j
    ret += 2**b + gg(i,j,b-1,rest)  # msb i,j
  elsif msb > j
    ret += (2**b)*2  # msb ij
  end
  printf("g(%d,%d,%d,%d): returning %d\n",
         i, j, b, x, ret) if DBGg
  return ret
end

def gg(i, j, b, x)  # assume i<j  zero not allowed
  if b == 0
    return (x>=j ? 2 : x>=i ? 1 : 0)
  end
  msb = x / (10**b)
  rest = x % (10**b)
  printf("   in-gg) x:%d b: %d msb:%d rest:%d\n",
         x, b, msb, rest) if DBGg
  ret = 0  # msb<i
  if msb < i
    ret += 0  # dmy
  elsif msb == i
    ret = gg(i,j,b-1,rest)  # msb i
  elsif msb < j
    ret = 2**b      # msb i
  elsif msb == j
    ret = 2**b + gg(i,j,b-1,rest)  # msb i,j
  elsif msb >j
    ret = (2**b) * 2    # msb ij
  end
  printf("gg(%d,%d,%d,%d) returning %d\n",
         i,j,b,x, ret) if DBGg
  return ret
end


def h(i, j, k, b, x)  # assume i<j<k  upper bits may be zero
  if b == 0
    return (x>=k ? 3 : x>=j ? 2 : x>=i ? 1 : 0)
  end
  msb = x / (10**b)
  rest = x % (10**b)
  return h(i,j,k,b-1,rest) if msb==0
  rest0 = (10**b)-1
  printf("in-h) x:%d b: %d msb:%d rest:%d\n",
         x, b, msb, rest) if DBG

  ret = h(i,j,k,b-1,rest0)  # msb 0
  if msb < i
    ret += 0  # dmy
  elsif msb == i
    ret += hh(i,j,k,b-1,rest)  # msb i
  elsif msb < j
    ret += 3**b  # msb i
  elsif msb == j
    ret += 3**b + hh(i,j,k,b-1,rest)  # msb i,j
  elsif msb < k
    ret += (3**b)*2  # msb ij
  elsif msb == k
    ret += (3**b)*2 + hh(i,j,k,b-1,rest)  # msb ij,k
  elsif msb > k
    ret += (3**b)*3  # msb ijk
  end
  printf("h(%d): returning %d\n", x, ret) if DBG
  return ret
end

def hh(i, j, k, b, x)  # assume i<j<k  zero not allowed
  if b == 0
    return (x>=k ? 3 : x>=j ? 2 : x>=i ? 1 : 0)
  end
  msb = x / (10**b)
  rest = x % (10**b)
  printf("   in-hh) x:%d b: %d msb:%d rest:%d\n",
         x, b, msb, rest) if DBG
  ret = 0  # if msb < i
  if msb < i
    ret += 0   # dmy
  elsif msb == i
    ret = hh(i,j,k,b-1,rest)   # msb i
  elsif msb < j
    ret = 3**b  # msb i
  elsif msb == j
    ret = 3**b + hh(i,j,k,b-1,rest)  # msb i,j
  elsif msb < k
    ret = (3**b)*2   # msb ij
  elsif msb == k
    ret = (3**b)*2 + hh(i,j,k,b-1,rest)  # msb ij,k
  elsif msb > k
    ret = (3**b)*3  # msb ijk
  end
  printf("hh(%d) returning %d\n", x, ret) if DBG
  return ret
end

s = gets.chomp!

n = s.to_i
a = []
for i in 0...s.size
  a << s[i].to_i
end

if DBG2
  printf("h %d g35 %d g37 %d g57 %d f3 %d f5 %d f7 %d",
      h(3,5,7,9,n) , g(3,5,9,n) ,g(3,7,9,n) , g(5,7,9,n) ,
                   f(3,9,n) , f(5,9,n) , f(7,9,n))
  puts ""
end

print h(3,5,7,9,n) - g(3,5,9,n) - g(3,7,9,n) - g(5,7,9,n) +
                   f(3,9,n) + f(5,9,n) + f(7,9,n)