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

#![allow(non_snake_case)]
#![allow(unused_imports)]
use ascii::AsciiChar;
use ascii::AsciiStr;
use itertools::Itertools as _;
use permutohedron::LexicalPermutation as _;
use petgraph::algo::dijkstra;
use petgraph::algo::min_spanning_tree;
use petgraph::data::FromElements;
use petgraph::graph::NodeIndex;
use petgraph::graph::UnGraph;
use petgraph::unionfind::UnionFind;
use proconio::fastout;
use proconio::input;
use proconio::marker::Chars;
use proconio::marker::Usize1;
use std::cmp::max;
use std::cmp::min;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use superslice::Ext as _;

#[fastout]
fn main() {
    input! {
        N: i64,
    }

    let mut q: Vec<i64> = Vec::new();
    q.push(357);
    q.push(375);
    q.push(537);
    q.push(573);
    q.push(735);
    q.push(753);

    for digit in 4..=9 {
        // 3^digit通り
        let mut stack: Vec<Vec<char>> = Vec::new();
        stack.push(vec!['3']);
        stack.push(vec!['5']);
        stack.push(vec!['7']);
        while !stack.is_empty() {
            let state = stack.pop().unwrap();

            if state.len() == digit {
                let mut contains_three = false;
                let mut contains_five = false;
                let mut contains_seven = false;

                for i in 0..digit {
                    if state[i] == '3' {
                        contains_three = true;
                    }
                    if state[i] == '5' {
                        contains_five = true;
                    }
                    if state[i] == '7' {
                        contains_seven = true;
                    }
                }

                if contains_three && contains_five && contains_seven {
                    // OK
                    let num_s: String = state.iter().collect();
                    let num: i64 = num_s.parse().unwrap();
                    q.push(num);
                }
            } else {
                let mut s3 = state.clone();
                s3.push('3');
                stack.push(s3);

                let mut s5 = state.clone();
                s5.push('5');
                stack.push(s5);

                let mut s7 = state.clone();
                s7.push('7');
                stack.push(s7);
            }
        }
    }

    q.sort();

    let mut ans = 0;
    for &i in q.iter() {
        if i < N {
            ans += 1;
        }
    }
    println!("{}", ans);
}