#P1384. Lucky Permutation Positions

    ID: 14671 Type: Default 1000ms 256MiB

Lucky Permutation Positions

Lucky Permutation Positions

A number is lucky if and only if it consists solely of the digits $4$ and $7$, for example, $47$, $744$, or $4747$.

Given all the permutations of the numbers from $1$ to $n$ arranged in lexicographical order, find the permutation that is the $k$-th smallest in this order. In this permutation, count how many positions $i$ (1-indexed) satisfy both conditions:

  • $i$ is a lucky number;
  • The number at position $i$ is also a lucky number.

You may assume that the input is valid, i.e. $1 \le n \le 12$ and $1 \le k \le n!$.

inputFormat

The input consists of two space-separated integers $n$ and $k$, where $n$ denotes the size of the permutation and $k$ denotes the lexicographical order position.

outputFormat

Output a single integer representing the count of positions $i$ in the $k$-th lexicographically smallest permutation such that $i$ is lucky and the number at that position is also lucky.

sample

3 1
0