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 Rust (1.15.1) (judge verdict: Accepted)

#[macro_use]
#[allow(dead_code)]
mod template {
    macro_rules! debug {
        ($($a:expr),*) => {{
            use ::std::io::Write;
            writeln!(&mut ::std::io::stderr(), concat!($(stringify!($a), " = {:?}, "),*), $($a),*).unwrap();
        }}
    }

    macro_rules! input {
        (source = $s:expr, $($r:tt)*) => {
            let mut scanner = ::template::BufScanner::new($s.as_bytes());
            input_inner!{scanner, $($r)*}
        };
        ($($r:tt)*) => {
            let stdin = ::std::io::stdin();
            let mut scanner = ::template::BufScanner::new(stdin.lock());
            input_inner!{scanner, $($r)*}
        };
    }

    macro_rules! input_inner {
        ($scanner:expr) => {};
        ($scanner:expr, ) => {};

        ($scanner:ident, $var:ident : $t:tt $($r:tt)*) => {
            let $var = read_value!($scanner, $t);
            input_inner!{$scanner $($r)*}
        };
        ($scanner:ident, mut $var:ident : $t:tt $($r:tt)*) => {
            let mut $var = read_value!($scanner, $t);
            input_inner!{$scanner $($r)*}
        };
    }

    macro_rules! read_value {
        ($scanner:ident, ( $($t:tt),* )) => {
            ( $(read_value!($scanner, $t)),* )
        };

        ($scanner:ident, [ $t:tt ; $len:expr ]) => {
            (0..$len).map(|_| read_value!($scanner, $t)).collect::<Vec<_>>()
        };

        ($scanner:ident, chars) => {
            read_value!($scanner, String).chars().collect::<Vec<char>>()
        };

        ($scanner:ident, usize1) => {
            read_value!($scanner, usize) - 1usize
        };

        ($scanner:ident, $t:ty) => {
            ($scanner).next::<$t>()
        };
    }

    pub struct BufScanner<R: ::std::io::BufRead> {
        br: R,
        buf: Vec<u8>,
        pos: usize,
    }

    impl<R: ::std::io::BufRead> BufScanner<R> {
        pub fn new(br: R) -> Self {
            BufScanner {
                br: br,
                buf: Vec::new(),
                pos: 0usize,
            }
        }
        fn fill_line(&mut self) {
            self.buf.clear();
            self.pos = 0;
            self.br
                .read_until(b'\n', &mut self.buf)
                .expect("Read a line");
            if let Some(&b'\n') = self.buf.last() {
                self.buf.pop();
                if let Some(&b'\r') = self.buf.last() {
                    self.buf.pop();
                }
            }
        }
        fn skip_space(&mut self) {
            loop {
                for i in self.pos..self.buf.len() {
                    if self.buf[i] != b' ' {
                        self.pos = i;
                        return;
                    }
                }
                self.fill_line();
            }
        }
        pub fn read_token(&mut self) -> &str {
            self.skip_space();
            let start = self.pos;
            while self.pos < self.buf.len() && self.buf[self.pos] != b' ' {
                self.pos += 1;
            }
            let ret = unsafe { ::std::str::from_utf8_unchecked(&self.buf[start..self.pos]) };
            self.pos += 1; // skip the last space (or len() + 1 which is fine)
            return ret;
        }
        pub fn next<T: ::std::str::FromStr>(&mut self) -> T {
            self.read_token().parse::<T>().ok().expect("Parse")
        }
    }
}

fn dfs(n: usize, now: usize, use3: bool, use5: bool, use7: bool) -> usize {
    if n < now {
        return 0;
    }
    let mut ret = if use3 && use5 && use7 { 1 } else { 0 };
    ret += dfs(n, now * 10 + 3, true, use5, use7);
    ret += dfs(n, now * 10 + 5, use3, true, use7);
    ret += dfs(n, now * 10 + 7, use3, use5, true);
    return ret;
}

fn main() {
    input! {
        n: usize,
    }
    println!("{}", dfs(n, 0, false, false, false));
}