We need to solve the problem: For given n and k, find the lexicographically k-th permutation of numbers from 1 to n. However, n and k can be up to 1e9, so we can't generate the permutation directly. But we are only asked to count indices i such that i is a lucky number (contains only digits 4 and 7) and a_i (the element at position i) is also a lucky number.
We need to compute this count for the k-th permutation, or output -1 if the permutation does not exist (i.e., if k > n!). For n up to 1e9, n! is huge and k ≤ 1e9, so permutation exists only for relatively small n because n! grows quickly. Actually, n! exceeds 1e9 for n >= 13? Let's compute: 1! =1, 2! =2, 3! =6, 4! =24, 5! =120, 6! =720, 7! =5040, 8! =40320, 9! =362880, 10! =3,628,800 > 1e6, 11! =39,916,800 > 3e7, 12! =479,001,600 ~ 4.79e8, 13! =6,227,020,800 > 6e9 > 1e9. So for n >= 13, n! > 1e9, but k <= 1e9, so for n >= 13, the k-th permutation always exists? Wait: if n! > k, then there is a k-th permutation. But for n=13, n! ≈ 6.2e9 > 1e9, so k (max 1e9) is less than n!, so permutation exists. But for n=20, n! is astronomically huge, but k <= 1e9 is still less than n! so permutation exists. However, is there any constraint that we cannot list permutation? We need to compute a_i only for lucky positions i. Since n can be large (up to 1e9), but the number of lucky positions i is limited: lucky numbers up to 1e9 (since i ≤ n) are all numbers consisting of digits 4 and 7 only. How many such numbers ≤ 1e9? The count of lucky numbers of length L (digits) is 2^L. Up to 1e9 (which is 10^9, max 9 digits? Actually 1e9 is 1,000,000,000, which has 10 digits? Wait: 10^9 = 1000000000, which is 10 digits? Actually 10^9 = 1,000,000,000 has 10 digits (1 followed by nine zeros). But we consider numbers up to n ≤ 1e9. So the max lucky number could be up to 1e9, but the largest lucky number with digits only 4 and 7 that is ≤ 1e9 is 777,777,777 (9 digits) or maybe 4,444,444,444? That's > 1e9 (10 digits). So maximum length is 9 digits. Count of lucky numbers with at most 9 digits: sum_{L=1}^{9} 2^L = 2^{10} - 2 = 1024-2=1022? Actually sum_{i=1}^{9} 2^i = 2^{10} - 2 = 1022. But wait: L=1..9: 2^1=2, 2^2=4,... 2^9=512, total = 1022. So there are at most 1022 lucky numbers ≤ 1e9. That's manageable.
Thus we can enumerate all lucky numbers i <= n. For each such index i, we need to determine a_i in the k-th permutation of [1..n]. Since n can be large (up to 1e9) but k is up to 1e9, the permutation's structure for large n is almost the identity for most initial positions? Actually, lexicographic order of permutations: The first permutation is 1,2,3,...,n. Then we incrementally change at the end. The k-th permutation can be obtained by factorial number system representation. For n large (>= something), the first elements may remain 1,2,... until some point where the permutation deviates. Specifically, if n > some threshold such that n! is huge, the first many elements will be 1,2,3,... because the number of permutations with first element = 1 is (n-1)! which is huge for large n, so for k <= 1e9, we likely have k <= (n-1)!? That depends on n. For n >= 13, (n-1)! is huge (12! = 479M, 13!? Wait (n-1)! for n=13 is 12! = 479,001,600 < 1e9? Actually 12! = 479001600 ≈ 4.79e8 < 1e9, (13-1)! = 12! = 479001600 < 1e9. For n=14, (13)! = 6227020800 > 1e9, so (n-1)! for n=14 is 13! = 6.2e9 > 1e9. So for n=14, (n-1)! > 1e9, so the k-th permutation will have first element = 1 if k <= (n-1)!, else maybe 2 etc. Since k <= 1e9, for n=14, (n-1)! > 1e9, so first element remains 1. Similarly, for n=13, (n-1)! = 12! = 479001600 < 1e9, so it's possible that k > (n-1)!, so first element could be >1. But we need to compute the permutation for indices up to n, but only for a small set of indices (lucky numbers i). Since i can be up to n, which can be large, but the permutation values a_i for i that are lucky may be affected by the reordering only in the tail part? Actually, the lexicographic permutation changes the later elements first. In the factorial number system, the permutation can be represented as: we have a list of remaining numbers. For position pos from 1 to n, we determine the index (starting from 0) of the number to pick: index = (k-1) / (n-pos)! (or something). Then we remove that number from the set. So to compute a_i for a particular i, we could simulate this process, but n can be up to 1e9, we cannot iterate over all positions. However, we only need to compute for at most 1022 positions. The challenge is that the set of numbers is huge (size up to 1e9). We cannot maintain the entire set.
But we can use the observation that for large n, the permutation will be close to identity for most initial positions because (n-1)! quickly becomes enormous. In fact, for positions i where (n-i)! > k (or something), the element at position i will simply be the smallest remaining number, which will often be i? Actually, let's reason carefully.
We need to compute the k-th permutation of numbers 1..n, lexicographically. Represent k-1 in factorial number system with digits d_1, d_2, ..., d_n, where d_i is in [0, n-i]. Then the permutation is constructed by: start with available numbers [1..n] sorted. For i from 1 to n, pick the (d_i)-th (0-indexed) smallest remaining number; that is a_i.
But d_i = floor((k-1) / (n-i)!) mod (n-i+1)? Actually, careful: Usually, to get the k-th permutation (k starting from 1), we compute: for i from 1 to n:
fact = (n-i)!
index = (k-1) // fact (integer division)
then a_i = the (index)-th smallest element among remaining.
k = k - index * fact.
But note: after picking, the set size reduces. The index is between 0 and (n-i). That works.
Now, if (n-i)! > (k-1) (or large relative to k), then index will be 0, meaning we pick the smallest remaining number. For positions i where (n-i)! > k-1, index=0. Since k-1 <= 1e9-1, we need to find for which i does (n-i)! exceed 1e9. Let's compute factorials:
n! for n=1..:
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
10!=3,628,800
11!=39,916,800
12!=479,001,600
13!=6,227,020,800
So (n-i)! will be considered for i from 1 upwards, and n can be large. For a given n, (n-i)! is the factorial of (n-i). When (n-i) >= 13, (n-i)! >= 13! = 6.227e9 > 1e9, so (n-i)! > 1e9. Since k-1 < 1e9, index will be 0 for all i such that n-i >= 13, i.e., i <= n-13. So for all positions from 1 to n-13, index=0. That means that at each of these positions, we pick the smallest remaining number.
What does "smallest remaining number" become after several picks? If we always pick the smallest remaining number at each step, then the permutation would be 1,2,3,..., up to some point. Let's simulate: Initially available numbers = {1,2,...,n}. At position 1, index=0 -> pick 1. Remaining: {2,3,...,n}. At position 2, index=0 -> pick smallest = 2. So indeed, as long as we always pick index=0, we will pick the numbers in increasing order: 1,2,3,... So for positions i <= n-13, we will have a_i = i. Because at step i, after having picked 1..i-1, the smallest remaining is i. So a_i = i for i <= n-13. However, careful: This holds if index=0 for all those positions. But after we might have a position where index != 0 later, that may affect the set of remaining numbers for earlier positions? But earlier positions already fixed. So for i from 1 to n-13, we have a_i = i (since the process picks in order). So for any i ≤ n-13, the value at position i is i.
But what about i such that i > n-13? That is the last 13 positions. For those, (n-i)! might be less than or equal to k, so index may be non-zero. So the permutation differs from identity only in the last (up to 13) positions. This is crucial.
But this reasoning assumes n is at least 13. For n < 13, (n-i)! never exceeds 1e9? Actually, if n < 13, then n-i could be less than 13, and (n-i)! could be less than or equal to 1e9. For n=12, (n-1)! = 11! = 39,916,800 < 1e9, (n-2)! = 10! = 3,628,800, etc. So it's possible that index could be non-zero for many positions, even early ones if k is large. But n is at most 12 in that case. For n ≤ 12, we can directly compute the k-th permutation (since 12! = 479M, but that's still manageable? Actually we can compute using next_permutation? But k up to 1e9, and n ≤ 12, we can compute permutation by factorial representation without iterating all positions? We can simulate the factorial representation directly because n is small. That's fine.
For n ≥ 13, the above property holds: the first n-13 positions are unchanged (a_i = i). Since lucky positions i are at most 1022, many of them may be in the region i ≤ n-13 if n is large. For those, a_i = i, so condition (i is lucky and a_i is lucky) reduces to i is lucky (since a_i = i). So any lucky i in that range contributes 1.
But careful: if n is large, say n=1e9, then n-13 ≈ 1e9 - 13, which is huge. All lucky numbers i ≤ n are obviously ≤ n, but many of them will be ≤ n-13? Since lucky numbers up to 1e9 are at most 1022, and the largest lucky number ≤ n could be close to n. But n-13 is also huge, so all lucky numbers are ≤ n-13 except possibly those very close to n, i.e., the largest lucky numbers that might be greater than n-13. But n-13 is huge, but the lucky numbers are spaced out. For n large, all lucky numbers are ≤ n-13 because lucky numbers are at most around 777,777,777 (9 digits) while n can be up to 1e9, but n-13 is about 1e9 -13 ≈ 1,000,000,000 -13 = 999,999,987, which is > 777,777,777? Actually 1e9 is 1,000,000,000, so n-13 can be as low as 1,000,000,000 -13 = 999,999,987, which is still > 777,777,777. So if n is close to 1e9, then lucky numbers (max 777,777,777) are all less than n-13 because n-13 is larger than the maximum lucky number? Let's check: The maximum lucky number <= 1e9 is 777,777,777? Actually 777,777,777 is about 7.78e8. Next would be 4,444,444,444 which is > 1e9, so max is 777,777,777. So for any n >= 777,777,777 + 13? Let's compute: 777,777,777+13 = 777,777,790. So if n >= 777,777,790, then n-13 >= 777,777,777, meaning all lucky numbers i ≤ n are ≤ n-13? Because if n-13 >= 777,777,777, then any lucky number i ≤ n will also be ≤ n, but could be > n-13? Possibly if i is between n-13+1 and n. But if n-13 is already >= max lucky number (777,777,777), then there cannot be any lucky number > n-13 because the max lucky is <= n-13. So in that case, all lucky i satisfy i ≤ n-13, so a_i = i, and the answer is simply the total count of lucky numbers <= n. Because condition i lucky and a_i lucky becomes i lucky and i lucky -> count of lucky i. That's one scenario.
If n is less than 777,777,777+13, then some lucky numbers might be in the tail region (the last 13 positions) where a_i may differ from i. But note: even if n is not that large, say n = 100, then n-13 = 87, all lucky numbers up to 100 are: 4,7,44,47,74,77? Actually 44,47,74,77 are all less than 87, so all are in the identity region, so again a_i = i. But what about n=15? n-13=2, lucky numbers <=15: 4,7, (44>15) so i=4,7 > 2, so they are in the tail region (positions 4 and 7, which are within the last 13 positions). For those, a_i may not equal i. So we need to compute a_i for lucky i > n-13, if any. Since there are at most 1022 lucky numbers, and the tail size is at most 13, we only need to consider those lucky i that lie in the set {max(1, n-12) ... n}. Because positions i from n-12 to n (13 positions) are potentially non-identity. Actually, if n >=13, the first n-13 positions are identity, so the last 13 positions are the ones where we need to compute the permutation exactly. That is, for i > n-13, we need to compute a_i. However, what if n <13? Then we cannot say first n-13 positions (negative). So we treat n<13 as a special case where we compute the full permutation (since n is small, we can compute all a_i). For n >=13, we need to compute the permutation only for the last L = min(13, n) positions? Actually, n-13 could be 0 if n=13, so then first 0 positions identity, last 13 positions? Wait n=13, n-13=0, so positions 1..0 none, positions 1..13 are all "tail" (since n-i = 12 for i=1, (12)! = 479M which < 1e9, so index might be non-zero for early positions). But earlier we said for positions i <= n-13 we have index=0. For n=13, n-13=0, so no position automatically has index=0. So for n=13, it's possible that some of the first positions may have index=0 depending on k? Actually, the condition for index=0 is (n-i)! > k-1. For i=1, (12)! = 479001600. If k-1 < 479001600, then index=0; if k-1 >= that, index may be >0. Since k can be up to 1e9, it's possible that k > 12!+1, so index at i=1 could be >0. So for n=13, we cannot guarantee that any position has index=0 just because (n-i)! > 1e9? But (12)! is less than 1e9, so it's not > 1e9. So the condition (n-i)! > 1e9 does not hold for n=13, i=1? (n-i)! = 12! ≈ 4.79e8 < 1e9, so it's not > 1e9. So indeed, the property that (n-i)! > k-1 may or may not hold depending on k. Our earlier deduction that for n-i >= 13, (n-i)! > 1e9 is based on (13)! ≈ 6.2e9 > 1e9. So if n-i >= 13, then (n-i)! > 1e9, which is > k-1 (since k-1 < 1e9). So only when n-i >= 13, i.e., i <= n-13, we guarantee (n-i)! > k-1, hence index=0. For n=13, n-i >=13 means i <=0, which is impossible. So for n=13, there is no such guarantee; all positions could have potential non-zero index. Therefore, our earlier statement that for n >= 13 the first n-13 positions are identity is only valid if n-13 >= 1? Actually, we need n-i >=13 for i <= n-13. For i <= n-13, n-i >=13, so (n-i)! > 1e9, which is > k-1, so index=0. That holds for any n such that n-13 >= 1, i.e., n >= 14. Because if n=14, then n-13=1, so positions i <= 1? Actually n-13 = 1, so positions i <= 1 (i=1) satisfy i <= n-13? i=1 <=1, yes. For i=1, n-i=13, 13! = 6.2e9 > 1e9, so index=0. For i=2, n-i=12, 12! = 4.79e8, not necessarily > k-1, because k-1 could be up to 1e9-1, which is larger. So for n=14, we only guarantee that position 1 is identity. For position 2, (n-i)!=12! < 1e9, so index may be non-zero. So the identity region is only positions i <= n-13. So for n=14, identity positions are i <= 1 (only first 1). For n=15, identity positions i <= 2. For n=20, identity positions i <= 7? Wait n-13 = 7, so i <=7 identity. For n=100, identity positions i <= 87. So the identity region size = max(0, n-13). So for n >= 14, identity region exists. For n=13, identity region size = 0. For n <13, we treat specially.
Therefore, for n >= 14, we know that a_i = i for i = 1..(n-13). For the last 13 positions (i = n-12 to n), we need to compute the permutation of the remaining numbers after picking the first n-13 numbers (which were 1..n-13). The remaining set is {n-12, n-11, ..., n}? Actually, careful: After picking the first (n-13) numbers as 1,2,...,n-13, the set of remaining numbers is {n-12, n-11, ..., n} (since the smallest numbers 1..n-13 have been taken). So the tail is a permutation of these 13 numbers. But note: n can be large, e.g., n=1e9, then the tail numbers are huge (around 1e9). But we don't need their actual values? We need to know whether a_i (which is some number from this set) is lucky. Also i (position) is a lucky number. Since i is in the tail region (i between n-12 and n), i can be huge (like ~1e9), but we only have at most 1022 lucky positions overall; we can check each lucky i if it falls in the tail region. Since the tail region has only 13 positions, the number of lucky i that fall into this region is at most 13. So we can compute the permutation of the tail explicitly. But we must be careful: The tail size might be less than 13 if n < 13? But we are in n>=14, tail size is exactly 13? Actually, if n >= 14, n-13 >=1, identity region exists, and the tail is the last 13 positions: i = n-12, n-11, ..., n. That's 13 positions. For n = 14, tail positions: 2 to 14? Wait n-12 = 2, n=14 => tail positions: 2,3,4,...,14. That's 13 positions? 14-2+1 =13, correct. For n=15, tail: 3 to 15 (13 positions). So yes, for n>=14, tail size = 13. For n=13, we have no identity region; but then the whole permutation is of size 13. But we treat n<=13 separately (or we could treat n=13 as a special case of "tail" of size 13). Since 13 is small, we can compute the whole permutation for n<=13 directly using factorial representation.
Similarly, for n between 1 and 13, we can compute the full permutation explicitly: we have at most 13 numbers, so we can generate the k-th permutation by factorial number system. That is feasible.
Now, for n >= 14, we only need to compute the tail permutation of 13 numbers. How do we compute the tail permutation? We know the first (n-13) picks were forced to be 1,2,...,n-13. So the remaining set S = {n-12, n-11, ..., n} (13 numbers). The order of picks for positions i = n-12,...,n is determined by the remainder of k after accounting for the first (n-13) picks? Let's examine the process.
The original process for the full permutation: we have initial k (1-indexed). For i=1..n, we compute index_i = (k-1) // (n-i)! (integer division), then set a_i = the (index_i)-th smallest in remaining set, and update k = (k-1) % (n-i)! + 1? Actually after picking, we set k = k - index_i * (n-i)!? Or better: let k' = k-1. For i from 1 to n: fact = (n-i)!; index = k' // fact; pick element; then k' = k' % fact; then continue. That's standard.
Now for i=1..(n-13), we deduced that index_i = 0 because (n-i)! > k'? But we need to be careful: our deduction was based on that (n-i)! > (k-1) initially. However, after picking some elements, k' reduces? Actually, note that if index_i = 0 at step i, then k' remains unchanged (since k' = k' % fact = k' because k' < fact). But if at the first step (n-1)! > k-1, then index_1 = 0, and k' remains k-1. For step 2, (n-2)! could be still > k'? It depends: k' hasn't changed, and (n-2)! could be > k' as well, leading to index_2=0, etc. So indeed if initially (n-1)! > k-1, then all subsequent (n-i)! for i=2,... will also be > k'? Not necessarily: (n-2)! is smaller than (n-1)!. It could still be > k-1 if k-1 is small enough. But we need to ensure that for all i <= n-13, (n-i)! > k-1. Since (n-i)! decreases as i increases? Actually (n-i)! for i larger (closer to n) is factorial of a smaller number. For i <= n-13, we have n-i >= 13, so (n-i)! >= 13! = 6.2e9. Since k-1 < 1e9, indeed (n-i)! > k-1 for all i with n-i >= 13. So as long as n-i >= 13, the factorial is > k-1, so index will be 0. And after each step with index 0, k' stays the same (k-1). So the condition persists. Therefore, for i from 1 to n-13, index_i = 0, and k' unchanged. So after processing these steps, we have picked numbers 1,2,...,n-13 in order. Then we have remaining set = {n-12, ..., n}. The remaining k' is still the original k-1 (since each step didn't change k'). So for the tail positions (i = n-12,...,n), we have a smaller problem: we need to find the (k')-th permutation (0-indexed) of the remaining 13 numbers, where k' = (original k - 1) (since no reduction). Actually after n-13 steps, the current k' is still original k-1. However, we need to be careful: At step i = n-13, (n-i)! = (13)!? Let's compute: i = n-13 => n-i = 13, fact = 13! = 6.2e9. Since k-1 < 1e9, index = 0, k' unchanged. So yes, after i=n-13 steps, we have k' = k-1. Then the remaining number of elements is 13. So we need to compute the permutation of these 13 numbers according to k' (0-indexed). This is straightforward because 13! is about 6.2e9, but k' is at most 1e9-1, so it's valid (since 13! > k').
Thus algorithm:
If n <= 13: compute full permutation of numbers 1..n using factorial number system to get array a[1..n]. Then count lucky indices i where i and a[i] are lucky.
If n >= 14:
- Determine the identity cutoff: first = n-13 (so positions 1..first are identity: a_i = i).
- For positions i = first+1 to n (i.e., n-12 .. n), we need to compute permutation of the set S = {first+1, first+2, ..., n} (which are 13 consecutive integers). Compute the (k-1)-th permutation (0-indexed) of S. Use factorial representation with base factorials up to 12! (since we have 13 numbers). Then assign a_i accordingly.
- Then for each lucky index i (enumerate all lucky numbers ≤ n), if i ≤ first, then if i is lucky (and a_i = i) so count if i lucky. Actually we need both i and a_i lucky. Since a_i = i, it's equivalent to i lucky. So we can add 1 if i is lucky.
- If i > first (i.e., in tail), we check if a_i is lucky (we already have a_i computed for those positions). Since i is lucky by enumeration, we only need to check a_i.
But wait: we need to compute a_i only for tail positions that are lucky. Since there are only up to 13 tail positions, we can compute a for all tail positions and then check each lucky i in that range. However, it's easier: after computing the tail permutation (list of 13 values in order for positions from first+1 to n), we can iterate over those positions and if the position i is lucky, check if the assigned value is lucky.
But we also need to consider the possibility that n may be less than 13? Already handled.
Now, we must also check if the k-th permutation exists: that is, if k > n!. Since n can be up to 1e9, n! is huge for n>=14, so it's guaranteed to exist because n! > 1e9. But for n <= 13, we must check: compute n! and compare with k. If k > n!, output -1.
Now, we need to generate lucky numbers up to n. Since n can be up to 1e9, we can generate all numbers composed of digits 4 and 7 up to 1e9. We'll recursively generate them or iterative with BFS. There are only 1022 such numbers, as computed. We'll store them in a list sorted.
Then we can count lucky indices i ≤ n that are lucky. For identity region, answer contribution is simply the number of lucky i ≤ first (since a_i = i). For tail region, we need to compute a_i for those i that are lucky and > first.
But careful: The identity region includes positions 1..first. For i in identity, a_i = i, so condition holds if i is lucky. That's correct.
One nuance: The identity region condition relies on the fact that index_i = 0 for i <= first. That's true as argued only if n >= 14? Actually for n=14, first = 1, yes it's identity. For n=13, first = 0, no identity. For n=12, we treat separately.
But we must also consider n > 13? Actually for n=13, identity region size = n-13 = 0, so no identity. But our plan for n>=14 works. So yes.
Now, we must handle n up to 1e9, but generating lucky numbers up to n is fine. However, we need to compute factorial numbers for the tail permutation. Since tail size is at most 13, we can precompute factorials up to 12! (since 13! may be needed for existence check but not for permutation generation? Actually, when generating permutation of m numbers (m=13), we need factorials from (m-1)! down to 0! to compute digits. We'll need factorials up to 12! which is 479001600 (fits 32-bit). We'll compute them.
Edge Cases:
- n = 0? Not allowed (n >=1).
- k might be > n! for n <=13. Output -1.
- n might be exactly 13: we treat as small case, compute full permutation of 13 numbers. That's feasible.
- n might be 14: first = 1, tail positions 2..14 (13 positions). Compute permutation of S = {2,3,...,14}. But careful: S size = 13? Actually numbers from first+1 = 2 to n = 14 inclusive: that's 13 numbers (2..14). But these numbers are not necessarily the original numbers after picking 1? Yes, after picking 1, remaining set = {2,3,...,14}. So that's correct. But note that the numbers are 2..14. In the permutation generation, we will compute the (k-1)-th permutation of these 13 numbers. So a_i for i=2..14 is assigned accordingly. Then for lucky positions: e.g., i=4 (lucky) and i=7 (lucky) are in tail, we need to check a_4 and a_7. Good.
Now, what about n very large, say n=10^9, first = n-13 ≈ 10^9 -13. The tail positions are 10^9 -12 .. 10^9. Lucky numbers up to 10^9 are at most 777,777,777, which is less than 10^9-12? Actually 10^9 - 12 = 999,999,988. The maximum lucky number is 777,777,777, which is less than 999,999,988, so all lucky numbers i are <= first? Indeed, 777,777,777 < 999,999,988, so all lucky i are in identity region. So answer = count of lucky numbers <= n. But note: we need to confirm that for n=10^9, first = n-13 = 999,999,987, and 777,777,777 < 999,999,987, so yes all lucky i are identity. So answer is just the number of lucky numbers <= n. However, careful: If n is less than the maximum lucky number + something, some lucky numbers might be in tail. But that's fine.
Now, need to compute count of lucky numbers <= n. That can be done by enumerating all lucky numbers up to 1e9 and counting those <= n.
Now, we need to ensure that the tail permutation computation for n >= 14 uses the correct set of numbers. The set S = {first+1, first+2, ..., n}. Since first = n-13, S = {n-12, n-11, ..., n}. These are 13 consecutive integers. They are not necessarily 1..13; they are large numbers. Their lucky status must be checked. So we need to compute whether each a_i (which is an element from S) is lucky. Since S contains large numbers up to 1e9, but checking if a number is lucky is easy: just check digits (only 4 and 7). So we can do that.
Now, we also need to consider the case where n may be less than 14 but we treat separately. For n <= 13, we compute full permutation using factorial representation. Since n is at most 13, we can compute a[1..n] easily. Then for each lucky index i (i <= n), we check a[i] lucky. However, we must also consider that i might be > n? No, lucky numbers enumerated up to n. So just loop through lucky numbers list and if i <= n, check a[i].
Implementation steps:
-
Precompute factorials up to 12 (or up to 13). Actually we might need up to 12! for tail (since we have 13 elements, we need factorials from 12! down). Also for n <=13, we may need factorials up to n! for existence check and permutation generation. So compute factorials up to 13 (13! = 6227020800 > 2^31 but we can store as long long). Use 64-bit.
-
Generate list of lucky numbers up to 1e9 (or up to n, but easier up to 1e9) using recursion: start with 0, then add digits 4 and 7: generate numbers: 4,7,44,47,74,77,444,... up to > 1e9. Store in vector.
-
Read n, k.
-
If n <= 13:
- Compute fact_n = n! (using precomputed factorial array). If k > fact_n, output -1.
- Else, generate k-th permutation of numbers 1..n.
- To generate: Let numbers = list [1..n]. Let k0 = k-1 (0-indexed). For i from 0 to n-1: fact = (n-1-i)!; idx = k0 / fact; a[i] = numbers[idx]; erase numbers[idx]; k0 %= fact.
- Then count: For each lucky number L in lucky list where L <= n: if a[L-1] (since indices 1-indexed) is lucky (check digits) then ans++.
- Output ans.
-
Else (n >= 14):
- Check existence: Since n>=14, n! > 1e9 (actually 14! = 87178291200 > 1e9) so definitely k <= n!. So permutation exists.
- Compute first = n - 13. (Note: if n is exactly 14, first = 1; n=15, first=2; ...)
- Count lucky indices in identity region: For each lucky number L in lucky list, if L <= first, increment ans.
- Now compute tail permutation for positions first+1 to n (13 numbers). The set S = list of numbers from first+1 to n inclusive (13 numbers). We need to compute the (k-1)-th permutation (0-indexed) of S.
- Since k-1 < 1e9, we can use factorial representation for m=13.
- However, we must be careful: The permutation of the tail is determined by the remaining k0 = k-1 (as argued, since no reduction from earlier steps). But is that always correct? Let's double-check: After picking the first 'first' elements with index=0, k0 remains k-1. The number of remaining elements is 13. So the permutation of the remaining set S corresponds to the (k0)-th permutation (0-indexed) of S in lex order. Yes.
- So generate permutation of S: let vec = S (sorted ascending). Let k0 = k-1. For i from 0 to 12: fact = factorial[12-i] (since for 13 elements, first choice uses 12!; then 11! etc). idx = k0 / fact; a_tail[i] = vec[idx]; remove that element; k0 %= fact.
- Now we have a mapping from position offset: position i corresponds to actual index = first + 1 + i (since positions start at first+1). So for each offset i, let pos = first+1 + i; value = a_tail[i].
- For each lucky number L in lucky list, if L > first and L <= n (i.e., L in tail positions), then we need to check if value at that position is lucky. Since tail positions are only 13, we can simply iterate over the tail positions and check if pos is lucky and if value is lucky.
But we must verify that the tail permutation generation works for any k-1 between 0 and 13!-1 (but 13! is >1e9, so k-1 is within range). However, for n >= 14, what if n is exactly 14? Then first = 1, tail set S = {2,3,...,14}. That's 13 numbers. Generation is fine.
Now, we need to ensure that the identity region argument holds for n >= 14. But we must double-check for n = 14: (n-1)! = 13! ≈ 6.2e9 > 1e9, so index_1 = 0 for any k <= 1e9. Yes. For i=2, (n-2)! = 12! ≈ 4.79e8, which might be less than k-1 depending on k. So identity region only i=1. That's consistent with first = n-13 = 1. So good.
For n = 100: first = 87, identity positions 1..87. For i=88, (n-88)! = (12)!? Actually n-i = 100-88 =12, so factorial 12! = 4.79e8. k-1 could be up to 1e9-1, so index may be non-zero. So tail starts at 88 (n-12 = 88). So identity region size = n-13 = 87. So correct.
Now, what about n = 13? In that case, identity region would be n-13 =0, tail would be all 13 positions. But our plan treats n<=13 separately, so fine.
What about n = 0? Not possible.
Now, need to consider if n is extremely large, say 10^9, first = n-13 ~ 10^9-13, but then we need to generate tail set S = {first+1,...,n} which are 13 consecutive integers, each up to 10^9. That's fine. And we need to generate the permutation of these 13 numbers. That is easy.
Now, potential pitfalls:
-
The enumeration of lucky numbers up to n: If n is huge (like 10^9), we generate all lucky numbers up to 10^9. That's fine.
-
The answer for identity region is simply count of lucky numbers <= first. But careful: We must not double-count lucky indices in tail region that are also counted in identity region? No, identity region is positions <= first, tail region is > first, they are disjoint.
-
For n >= 14, we didn't check existence because n! > 1e9 ensures k <= n!. But is it always true that for n >= 14, n! > 1e9? Let's check: 14! = 87178291200 > 1e9, yes. So for any n>=14, n! >= 14! > 1e9 >= k. So existence is guaranteed.
-
For n <= 13, we must also check existence (k <= n!). If not, output -1.
Now, we need to confirm that the process for n >= 14 yields correct permutation even if k is huge? Since k <= 1e9, and tail permutation uses k-1 (which is < 1e9) while 13! > 6e9, so k-1 < 13!, so it's a valid index for permutations of 13 elements. So no problem.
Now, we should test with examples.
Example 1: n=7, k=4.
n <= 13, so compute full permutation.
7! = 5040, k=4 <=5040.
Compute 4th permutation of 1..7.
List permutations in lex order:
1: 1 2 3 4 5 6 7
2: 1 2 3 4 5 7 6
3: 1 2 3 4 6 5 7
4: 1 2 3 4 6 7 5 (as given)
So a = [1,2,3,4,6,7,5].
Lucky indices <=7: 4,7? Actually lucky numbers <=7: 4,7? And also maybe 4? 4 is lucky, 7 is lucky.
- i=4: a[4]=4 -> lucky, count++.
- i=7: a[7]=5 -> not lucky.
Also check i=?? 4 and 7 only. So ans=1. matches.
Example 2: n=4, k=7.
4! = 24, k=7 <=24.
Compute 7th permutation of 1..4.
List:
1:1 2 3 4
2:1 2 4 3
3:1 3 2 4
4:1 3 4 2
5:1 4 2 3
6:1 4 3 2
7:2 1 3 4
So a = [2,1,3,4].
Lucky indices <=4: 4? (4 is lucky) also maybe? 4 only.
i=4: a[4]=4 -> lucky, count=1. matches.
Now, test a case where n is larger, say n=14, k=1. Then permutation is identity: 1,2,3,...,14.
Lucky indices <=14: 4,7? 4 and 7 are lucky. Also? 44>14, 47>14. So 4 and 7. Both positions have values 4 and 7, both lucky. So answer=2.
Our algorithm: n>=14, first = n-13 =1. identity region: i=1 only. Lucky numbers <=1: none, so ans=0.
Tail: positions 2..14, compute permutation of S = {2..14} with k-1=0 -> identity order: 2,3,4,...,14.
Now check lucky positions in tail: 4 and 7 are in tail (since >1). At position 4, value = 4? Wait: tail positions mapping: offset 0->pos2 value2, offset1->pos3 value3, offset2->pos4 value4? Actually tail list: positions: first+1 = 2 is index0, 3 index1, 4 index2, ... So for pos=4, offset = 4-2 =2, a_tail[2] = S[2] = 4. So yes lucky. pos=7, offset=5, value = 7? S[5] = 7? Let's list S: 2,3,4,5,6,7,8,9,10,11,12,13,14. Index0:2, idx1:3, idx2:4, idx3:5, idx4:6, idx5:7, idx6:8, idx7:9, idx8:10, idx9:11, idx10:12, idx11:13, idx12:14. So pos=7 -> offset=5, value=7 -> lucky. So ans=2. Good.
Now test n=14, k=some other value, say k= 13! + 1? But k max 1e9, and 13! ≈ 6.2e9 > 1e9, so k can't exceed 13! for n=14? Actually for n=14, total permutations = 14! > 1e9, so k <= 1e9 < 14!, so it's fine. But k might be > 12!? For instance, k= 500,000,000. Then tail permutation may be non-identity. Let's manually test small case: n=14, k such that first element remains 1 (since 13! > k). So first element = 1, identity region i=1. Then tail permutation of numbers 2..14 with k0 = k-1 (since first step index=0). So tail permutation is the (k-1)-th permutation of 13 numbers. That's plausible.
Now, we need to confirm that the identity region is exactly positions 1..n-13 for n>=14. Let's prove: For i=1 to n-13, we have n-i >= 13, so (n-i)! >= 13! > 1e9 > k-1, so (n-i)! > k-1, so index_i = (k-1) // (n-i)! = 0. After picking, k-1 remains unchanged because modulo by (n-i)! yields same. So by induction, index_i=0 for all i <= n-13. So a_i = i.
Thus identity region is correct.
Now, we must also consider n = 13. For n=13, we treat separately, but we must check that our approach would also work with identity region of size 0 and tail size 13, but we will compute full permutation with n=13 directly. That's fine.
Now, for n between 1 and 13 inclusive, we can directly compute the permutation.
One more nuance: The problem statement: "If the k-th permutation of numbers from 1 to n does not exist, print the single number '-1'." Since n and k can be up to 1e9, but for n>=14, n! > 1e9, so k <= 1e9 < n! always? Actually n! for n=14 is 87178291200 > 1e9. So for any n>=14, n! > 1e9, and k <= 1e9, so k <= n! always. However, what about n=0? Not applicable. So only for n<=13 we need to check if k > n!.
Now, we must also consider n=0? No.
Now, generate lucky numbers up to n (or up to 1e9). Since n may be up to 1e9, we generate all numbers with digits 4 and 7 up to 1e9. We'll do DFS: start with 0, then for each step add digit 4 or 7. But be careful to avoid leading zeros. We'll generate by building numbers from 1 digit up to 9 digits (since 10-digit numbers exceed 1e9). Actually, max 9 digits because 10-digit smallest lucky number is 4444444444 which is > 1e9? 4,444,444,444 > 1,000,000,000, yes. So we only need up to 9 digits. So generate all numbers composed of digits 4 and 7 of length 1 to 9. That's 2^1+...+2^9 = 1022 numbers. We'll store them in a vector.
Now, for counting lucky indices in identity region, we can simply iterate over the lucky numbers list and count those <= first. Since first could be up to 1e9, but list length is small.
For tail region, we compute a_tail for the 13 positions. Then for each lucky L in the list, if L > first and L <= n, we compute value = a_tail[L - (first+1)] (since tail positions start at first+1). But note: we need to be careful about 1-indexing. Positions are 1..n. So tail positions indices: first+1, first+2, ..., n. So offset = (L - (first+1)). This offset will be between 0 and 12 inclusive. We then check if the value at that offset is lucky. Since we already have a_tail array (size 13) storing values in order for those positions.
Alternatively, we can just iterate over tail positions i from first+1 to n, check if i is lucky (by maybe using a set of lucky numbers or binary search in the sorted lucky list) and if a_tail[i - (first+1)] is lucky, then