P1384 Lucky Numbers and Permutations

Description

A number is a lucky number if and only if it consists only of the digits $4$ and $7$, for example $47$, $744$, $4747$. Among all permutations of $1$ to $n$ in lexicographical order, consider the $k$-th smallest permutation. Count how many indices $i$ such that both the position $i$ and the element at position $i$ are lucky numbers.

Input Format

One line containing two integers $n$, $k$.

Output Format

Output a single integer representing the answer. If the $k$-th smallest permutation does not exist, output $-1$.

Explanation/Hint

Sample 1 Explanation: The permutation is 1, 2, 3, 4, 6, 7, 5. Sample 2 Explanation: The permutation is: 2, 1, 3, 4. Constraints: - For $30\%$ of the testdata, it is guaranteed that $n \le 8$. - For $60\%$ of the testdata, it is guaranteed that $n \le 100$. - For $100\%$ of the testdata, it is guaranteed that $1 \leq n, k \le 10^9$. Translated by ChatGPT 5