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)

class Pre753
  attr_reader :n
  def initialize(n,digits=nil)
    @n=n
    if digits
      @digits=digits
    else
      @digits=Array.new(10,0)
      until n==0
        d=n%10
        @digits[d]+=1
        n/=10
      end
    end
  end

  def is753?
    @digits[7]>0&&@digits[5]>0&&@digits[3]>0
  end

  def create_new_pre753(d)
    _dig=@digits.dup
    _dig[d]+=1
    Pre753.new(10*@n+d,_dig)
  end
end

n=gets.to_i

a=[]
digit_num_wise=[7,5,3].map{|d|Pre753.new(d)}
8.times{
  _dnw=[]
  digit_num_wise.each{|pre|
    [7,5,3].each{|d|
      _dnw << pre.create_new_pre753(d)
    }
  }
  _dnw.each{|pre|a << pre if pre.is753?}
  digit_num_wise=_dnw
}

a.sort_by!{|pre|pre.n}
a<< Pre753.new(10**9)
ans=a.bsearch_index{|v|n<v.n}

puts ans