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: Wrong Answer)

use std::cmp::*;
use std::collections::*;
use std::io::*;
use std::ops::*;
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();
}
fn solve(mut n: usize) -> usize {
    fn count_all(len: u32, base: usize) -> usize {
        if len == 0 {
            return 0;
        }
        base * base.pow(len - 1) + count_all(len - 1, base)
    }
    fn count(ns: &[usize], uses: [bool; 3], use0: bool) -> usize {
        if ns.len() == 0 {
            return 0;
        }
        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 r = 0;

        if uses[0] {
            if n == 3 {
                r += count(&ns[1..], uses, false);
            }
            if n > 3 {
                r += base.pow(exp);
            }
        }
        if uses[1] {
            if n == 5 {
                r += count(&ns[1..], uses, false);
            }
            if n > 5 {
                r += base.pow(exp);
            }
        }
        if uses[2] {
            if n == 7 {
                r += count(&ns[1..], uses, false);
            }
            if n > 7 {
                r += base.pow(exp);
            }
        }
        if use0 {
            r += count_all(exp, base);
        }
        r
    }
    let mut ns = Vec::new();
    while n != 0 {
        ns.push(n % 10);
        n /= 10;
    }
    ns.reverse();

    println!("ns = {:?}", ns);
    let mut r = count(&ns, [true, true, true], true);
    println!("all = {}", r);
    for i in 0..3 {
        let mut uses = [false; 3];
        uses[i] = true;
        println!("only {} = {} ", i, count(&ns, uses, true));
        r += count(&ns, uses, true);
    }

    for i in 0..3 {
        let mut uses = [true; 3];
        uses[i] = false;
        println!("except {} = {} ", i, count(&ns, uses, true));
        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) }

        pub fn read_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            self.read_line()
                .split(' ')
                .take(n)
                .map(|x| x.parse().ok().unwrap())
                .collect()
        }
        pub fn read_array<A: AsMut<[T]> + Default, T: FromStr>(&mut self) -> A {
            let mut a: A = Default::default();
            {
                let a: &mut [T] = a.as_mut();
                let i = &mut self.read_line().split(' ');
                for n in 0..a.len() {
                    a[n] = read_next(i);
                }
            }
            a
        }
    }
    fn read_next<'a, T: FromStr, I: Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

    #[derive(Clone, Copy)]
    pub struct Mod(pub usize);

    use std::fmt;
    impl fmt::Debug for Mod {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{:?}", self.0)
        }
    }

    const MOD_BASE: usize = 1000000007;
    const MOD_INV_POW: usize = 1000000005;

    impl Mod {
        pub fn new(n: usize) -> Self {
            Mod(n % MOD_BASE)
        }
        pub fn pow(self, mut exp: usize) -> Self {
            let mut b = self;
            let mut r = Mod(1);
            while exp != 0 {
                if exp % 2 == 1 {
                    r *= b;
                }
                exp /= 2;
                b = b * b;
            }
            r
        }
        pub fn inverse_element(self) -> Self {
            self.pow(MOD_INV_POW)
        }
    }

    macro_rules! mod_op {
        (($ot:ident, $f:ident), ($ot_a:ident, $f_a:ident), $($rhs:ty : $e:expr,)*) => {
            $(
            impl $ot<$rhs> for Mod {
                type Output = Self;
                fn $f(self, rhs: $rhs) -> Self {
                    ($e)(self, rhs)
                }
            }
            impl $ot_a<$rhs> for Mod {
                fn $f_a(&mut self, rhs: $rhs) {
                    *self = ($e)(*self, rhs)
                }
            }
            )*
        }
    }

    mod_op!(
        (Add, add),
        (AddAssign, add_assign),
        Mod : |l: Mod, r: Mod| Mod::new(l.0 + r.0),
        usize : |l: Mod, r: usize| l + Mod::new(r),
    );

    mod_op!(
        (Mul, mul),
        (MulAssign, mul_assign),
        Mod : |l: Mod, r: Mod| Mod::new(l.0 * r.0),
        usize : |l: Mod, r: usize| l * Mod::new(r),
    );

    mod_op!(
        (Sub, sub),
        (SubAssign, sub_assign),
        Mod : |l: Mod, r: Mod| Mod::new(MOD_BASE + l.0 - r.0),
        usize : |l: Mod, r: usize| l - Mod::new(r),
    );

    mod_op!(
        (Div, div),
        (DivAssign, div_assign),
        Mod : |l: Mod, r: Mod| l * r.inverse_element(),
        usize : |l: Mod, r: usize| l / Mod::new(r),
    );

    // std::cmp::Reverseと同じ
    #[derive(PartialEq, Eq)]
    pub struct RevCmp<T>(pub T);

    impl<T: PartialOrd> PartialOrd for RevCmp<T> {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            other.0.partial_cmp(&self.0)
        }
    }
    impl<T: Ord> Ord for RevCmp<T> {
        fn cmp(&self, other: &Self) -> Ordering {
            other.0.cmp(&self.0)
        }
    }

    pub struct PascalsTriangle<T> {
        buf: Vec<T>,
        n: usize,
    }

    impl<T: One> PascalsTriangle<T> {
        pub fn new(n: usize) -> Self {
            let mut s = PascalsTriangle {
                buf: vec![T::one()],
                n: 1,
            };
            while s.n <= n {
                let i = s.buf.len() - s.n;
                s.buf.push(T::one());
                for m in 1..s.n {
                    let v = s.buf[i + m - 1] + s.buf[i + m];
                    s.buf.push(v);
                }
                s.buf.push(T::one());
                s.n += 1;
            }
            s
        }
        pub fn get(&mut self, n: usize, m: usize) -> T {
            debug_assert!(n <= self.n);
            debug_assert!(m <= n);
            self.buf[n * (n + 1) / 2 + m]
        }
    }

    pub struct PascalsTriangleIter<T> {
        buf: Vec<T>,
    }
    impl<T: One> PascalsTriangleIter<T> {
        pub fn new(n: usize) -> Self {
            let mut s = PascalsTriangleIter {
                buf: Vec::with_capacity(n + 1),
            };
            for _ in 0..n + 1 {
                s.next();
            }
            s
        }
        pub fn next(&mut self) {
            for i in (1..self.buf.len()).rev() {
                self.buf[i] = self.buf[i] + self.buf[i - 1];
            }
            self.buf.push(T::one());
        }
        pub fn get(&self) -> &[T] {
            &self.buf
        }
    }

    pub struct ModFacts {
        f: Vec<Mod>,
        rf: Vec<Mod>,
    }
    impl ModFacts {
        pub fn new(n: usize) -> Self {
            let mut s = ModFacts {
                f: vec![Mod(0); n + 1],
                rf: vec![Mod(0); n + 1],
            };

            s.f[0] = Mod(1);
            for i in 1..n + 1 {
                s.f[i] = s.f[i - 1] * i;
            }
            s.rf[n] = s.f[n].inverse_element();
            for i in (1..n + 1).rev() {
                s.rf[i - 1] = s.rf[i] * i;
            }
            s
        }
        pub fn fact(&self, n: usize) -> Mod {
            self.f[n]
        }
        pub fn comb(&self, n: usize, c: usize) -> Mod {
            self.f[n] * self.rf[c] * self.rf[n - c]
        }
    }

    pub trait Zero: Add<Output = Self> + Sized + Copy {
        fn zero() -> Self;
    }
    pub trait One: Zero {
        fn one() -> Self;
    }

    macro_rules! impl_zero {
        ($t:ty, $e:expr) => {
            impl Zero for $t {
                fn zero() -> Self {
                    $e
                }
            }
        }
    }
    macro_rules! impl_one {
        ($t:ty, $e1:expr, $e0:expr) => {
            impl One for $t {
                fn one() -> Self {
                    $e1
                }
            }
            impl_zero!($t, $e0);
        };
    }

    impl_one!(usize, 1, 0);
    impl_one!(u32, 1, 0);
    impl_one!(i32, 1, 0);
    impl_one!(f64, 1.0, 0.0);
    impl_one!(Mod, Mod(1), Mod(0));

    pub trait BinarySearchExt<T> {
        fn lower_bound_by<F: Fn(&T) -> bool>(&self, f: F) -> usize;
        fn lower_bound(&self, x: &T) -> usize;
        fn upper_bound(&self, x: &T) -> usize;
    }
    impl<T: Ord> BinarySearchExt<T> for [T] {
        fn lower_bound_by<F: Fn(&T) -> bool>(&self, f: F) -> usize {
            let mut low = 0;
            let mut high = self.len();
            while low < high {
                let mid = (low + high) / 2;
                if f(&self[mid]) {
                    high = mid;
                } else {
                    low = mid + 1;
                }
            }
            low
        }
        fn lower_bound(&self, x: &T) -> usize {
            self.lower_bound_by(|y| x <= y)
        }
        fn upper_bound(&self, x: &T) -> usize {
            self.lower_bound_by(|y| x < y)
        }
    }

    pub trait AtSliceExt<T> {
        fn rotate(&mut self, mid: usize);
    }
    impl<T> AtSliceExt<T> for [T] {
        fn rotate(&mut self, mid: usize) {
            {
                let (l, r) = self.split_at_mut(mid);
                l.reverse();
                r.reverse();
            }
            self.reverse()
        }
    }

}