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)

use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = CpReader::new(i);
    let n = i.read::<usize>();
    writeln!(o, "{}", solve(n)).unwrap();
}
pub fn solve(mut n: usize) -> usize {
    fn count_all(len: u32, base: usize) -> usize {
        if len == 0 {
            return 0;
        }
        base.pow(len) + count_all(len - 1, base)
    }
    fn count(ns: &[usize], uses: [bool; 3], use0: bool) -> usize {
        if ns.len() == 0 {
            return 1;
        }
        let n = ns[0];
        let mut base: usize = 0;
        for &u in &uses {
            if u {
                base += 1;
            }
        }
        let exp = (ns.len() - 1) as u32;
        let mut x = 0;
        let mut r = 0;
        if use0 {
            r += count_all(exp, base);
        }
        if uses[0] && n > 3 {
            x += 1;
        }
        if uses[1] && n > 5 {
            x += 1;
        }
        if uses[2] && n > 7 {
            x += 1;
        }
        r += x * base.pow(exp);
        let next = match n {
            3 => uses[0],
            5 => uses[1],
            7 => uses[2],
            _ => false,
        };

        if next {
            r += count(&ns[1..], uses, false);
        }
        r
    }
    let mut ns = Vec::new();
    while n != 0 {
        ns.push(n % 10);
        n /= 10;
    }
    ns.reverse();

    let mut r = count(&ns, [true, true, true], true);
    for i in 0..3 {
        let mut uses = [false; 3];
        uses[i] = true;
        r += count(&ns, uses, true);
    }

    for i in 0..3 {
        let mut uses = [true; 3];
        uses[i] = false;
        r -= count(&ns, uses, true);
    }
    r
}

mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        { $f:ident($($t:ident),*) } => {
            #[allow(dead_code)]
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                ($(read_next::<$t,_>(i)),*)
            }
        };
    }

    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
        fn_read! { read2(T1, T2) }
        fn_read! { read3(T1, T2, T3) }
        fn_read! { read4(T1, T2, T3, T4) }
        fn_read! { read5(T1, T2, T3, T4, T5) }
    }
    fn read_next<'a, T: FromStr, I: Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

}