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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            String n = in.next();
            int digitLen = n.length();

            char[] n753 = {'7', '5', '3', '0'};
            int[][][][][][] memo = new int[digitLen + 1][2][2][2][2][2];

            memo[0][0][0][0][0][0] = 1;
            for (int i = 0; i < digitLen; i++) {
                for (int j = 0; j < 2; j++) {
                    for (int k = 0; k < 2; k++) {
                        for (int n3 = 0; n3 < 2; n3++) {
                            for (int n5 = 0; n5 < 2; n5++) {
                                for (int n7 = 0; n7 < 2; n7++) {
                                    for (char cc : n753) {
                                        if ((i == digitLen - 1 || k == 1) && cc == '0') continue;
                                        if (j == 0 && cc > n.charAt(i)) continue;
                                        int less = (j == 1 || cc < n.charAt(i)) ? 1 : 0;
                                        int zero = (k == 1 || cc != '0') ? 1 : 0;
                                        int p3 = (n3 == 1 || cc == '3') ? 1 : 0;
                                        int p5 = (n5 == 1 || cc == '5') ? 1 : 0;
                                        int p7 = (n7 == 1 || cc == '7') ? 1 : 0;
                                        memo[i + 1][less][zero][p3][p5][p7] += memo[i][j][k][n3][n5][n7];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            int ans = memo[digitLen][1][1][1][1][1] + memo[digitLen][0][1][1][1][1];
            out.println(ans);
        }

    }

    static class FastScanner {
        private InputStream in;
        private byte[] buffer;
        private int bufPointer;
        private int bufLength;

        public FastScanner(InputStream in) {
            this.in = in;
            buffer = new byte[1024];
        }

        private boolean hasNextByte() {
            if (bufPointer < bufLength) return true;
            bufPointer = 0;
            try {
                bufLength = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (bufLength > 0);
        }

        private int readByte() {
            return hasNextByte() ? buffer[bufPointer++] : -1;
        }

        private static boolean isPrintableChar(int c) {
            return (c >= 33 && c <= 126);
        }

        public boolean hasNext() {
            while (hasNextByte() && !isPrintableChar(buffer[bufPointer]))
                bufPointer++;
            return hasNextByte();
        }

        public String next() {
            if (!hasNext())
                throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

    }
}