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.42.0) (judge verdict: Accepted)

// -*- coding:utf-8-unix -*-

#[macro_use]
extern crate lazy_static;

// use proconio::derive_readable;
use proconio::fastout;
use proconio::input;
// use std::convert::TryInto;
use libm::*;
use std::cmp::*;
use std::io::*;
use std::str::FromStr;
use superslice::*;

pub fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

//abc114-C
// #[fastout]
fn main() {
    input![n_input: usize];
    n_input;

    let mut num753_cand: Vec<usize> = Vec::new();

    go(0, 0, 3, &mut num753_cand, n_input);
    go(0, 0, 5, &mut num753_cand, n_input);
    go(0, 0, 7, &mut num753_cand, n_input);

    let mut ans = 0;
    for i in num753_cand.iter() {
        let i_str = i.to_string();

        let mut is_753 = true;
        let mut counts = vec![0; 3];
        for c in i_str.chars() {
            if c != '7' || c != '5' || c != '3' {}

            match c {
                '7' => counts[0] += 1,
                '5' => counts[1] += 1,
                '3' => counts[2] += 1,
                _ => {
                    is_753 = false;
                    break;
                }
            }
        }

        if !is_753 {
            continue;
        }

        if counts.iter().all(|c| *c > 0) {
            ans += 1;
        }
    }

    println!("{}", ans);
}

fn go(mid: usize, d: usize, use_num: usize, num753_cand: &mut Vec<usize>, n: usize) {
    if mid > n {
        return;
    }

    let mut fact = 1;
    for _ in 0..d {
        fact *= 10;
    }

    let plus = fact * use_num;
    let new_mid = mid + plus;

    if new_mid > n {
        return;
    }

    num753_cand.push(new_mid);

    go(new_mid, d + 1, 3, num753_cand, n);
    go(new_mid, d + 1, 5, num753_cand, n);
    go(new_mid, d + 1, 7, num753_cand, n);
}