Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
bool lucky(long long int a) {
while (a) {
if (a % 10 != 4 && a % 10 != 7) return false;
a /= 10;
}
return true;
}
vector<long long int> gen() {
vector<long long int> res;
for (int k = 1; k < 11; k++) {
for (int i = 0; i < (1 << k); i++) {
long long int a = 0;
for (int j = 0; j < k; j++) {
a *= 10;
if (i & (1 << j))
a += 4;
else
a += 7;
}
res.push_back(a);
}
}
sort(res.begin(), res.end());
return res;
}
vector<int> gen(long long int n, long long int k) {
long long int c = 1;
k--;
for (int i = 0; i < n; i++) c *= (i + 1);
vector<int> res;
if (c <= k) return res;
res.resize(n);
c /= n;
for (int i = 0; i < n - 1; i++) {
res[i] = k / c;
k %= c;
c /= (n - i - 1);
}
res[n - 1] = 0;
vector<bool> d(n, false);
for (int i = 0; i < n; i++) {
int c = 0;
while (1) {
if (!d[c]) {
if (res[i] == 0) {
d[c] = true;
res[i] = c;
break;
} else
res[i]--;
}
c++;
}
}
for (int i = 0; i < n; i++) res[i]++;
return res;
}
int main() {
long long int n, k;
cin >> n >> k;
vector<int> v = gen(min((int)n, 13), k);
if (v.empty()) {
cout << -1;
return 0;
}
int res = 0;
for (int i = 0; i < v.size(); i++) {
v[i] += (n - v.size());
if (lucky(i + n - v.size() + 1) && lucky(v[i])) res++;
}
if (n > 13) {
vector<long long int> v2(gen());
res -= v2.begin() - lower_bound(v2.begin(), v2.end(), n - 13 + 1);
}
cout << res;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | // practice with rainboy
import java.io.*;
import java.util.*;
public class CF121C extends PrintWriter {
CF121C() { super(System.out, true); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF121C o = new CF121C(); o.main(); o.flush();
}
long lucky(int b) {
return b == 1 ? 0 : lucky(b / 2) * 10 + (b % 2 == 0 ? 4 : 7);
}
boolean is_lucky(int y) {
return y == 0 ? true : is_lucky(y / 10) && (y % 10 == 4 || y % 10 == 7);
}
static final int N = 13;
boolean[] used = new boolean[N];
int[] ff = new int[N];
void init() {
int f = 1;
for (int i = 0; i < N; i++) {
ff[i] = f;
f *= i + 1;
}
}
int get(int n, int k, int i) {
Arrays.fill(used, false);
for (int h = 0; h < n; h++) {
int f = ff[n - 1 - h];
int s = k / f;
for (int j = 0; j < n; j++)
if (!used[j] && s-- == 0) {
if (h == i)
return j;
used[j] = true;
break;
}
k %= f;
}
return -1;
}
void main() {
init();
int n = sc.nextInt();
int k = sc.nextInt();
long f = 1;
int n_ = 1;
while (f < k)
f *= ++n_;
if (n_ > n) {
println(-1);
return;
}
int ans = 0;
for (int b = 2; ; b++) {
long x = lucky(b);
if (x > n)
break;
if (x <= n - n_) {
ans++;
continue;
}
int y = n - n_ + get(n_, k - 1, (int) (x - (n - n_) - 1)) + 1;
if (is_lucky(y))
ans++;
}
println(ans);
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.io.*;
import java.util.*;
import java.lang.reflect.Array;
import java.math.*;
public class lucksum {
public static long go(long m)
{
long res=0;
Queue<Long> l=new LinkedList<Long>();
l.add(new Long(0));
do{
Long t=l.remove();
if(t.compareTo(new Long(m))<=0)
{
res++;
l.add(t*10+4);
l.add(t*10+7);
}
else
break;
}while(true);
return res-1;
}
public static boolean isluck(long m)
{
boolean res=true;
while(m!=0)
{
long t=m%10;
if((t!=4)&&(t!=7))
return false;
m/=10;
}
return res;
}
public static void main(String[] args)
{
//try{
Scanner s=new Scanner(System.in);
long n=s.nextLong();
long k=s.nextLong();
long []p=new long[21];
Arrays.fill(p,Long.MAX_VALUE);
long start=1;
long mul=2;
p[0]=1;
while(start<=k)
{
p[(int)mul-1]=start;
start*=mul;
mul++;
}
if((n<20)&&(p[(int)n]<k))
{
System.out.println(-1);
return;
}
int index=0;
for(;p[index]<k;index++);
// index++;
long res=go(n-index);
List<Long> left=new LinkedList<Long>();
for(int i=index-1;i>=0;i--)
left.add(n-i);
while(index>0)
{
Long[] mm=(Long[])left.toArray(new Long[left.size()]);
long t=(k-1)/p[index-1];
long h=mm[(int)t];
if(isluck(h)&&isluck(n-index+1))
res++;
left.remove((int)t);
k-=t*p[index-1];
index--;
}
System.out.println(res);
// }catch (Exception e1){
//}
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<long long> V;
vector<long long> F;
vector<long long> out;
bool used[30];
long long per[20];
void dfs(int depth, string S) {
if (depth > 11) return;
if (S.length() != 0) {
long long fuck;
istringstream st(S);
st >> fuck;
V.push_back(fuck);
}
dfs(depth + 1, S + '4');
dfs(depth + 1, S + '7');
}
void go(long long cur, int depth) {
if (depth == 0) return;
long long permu = per[depth - 1];
long long i, j, k;
if (cur == 0) {
for (i = 0; i < F.size(); i++) {
if (used[i] == 0) {
out.push_back(F[i]);
return;
}
}
}
for (i = 1;; i++) {
if (permu * i >= cur) {
int count = i;
for (j = 0; j < F.size(); j++) {
if (used[j] == 0) {
count--;
if (count == 0) {
used[j] = 1;
out.push_back(F[j]);
go(cur - permu * (i - 1), depth - 1);
break;
}
}
}
break;
}
}
}
bool CANT(long long N, long long K) {
if (N < 15 && per[N] < K) return 1;
return 0;
}
bool ok(long long t) {
while (t > 0) {
if (!(t % 10 == 4 || t % 10 == 7)) return 0;
t /= 10;
}
return 1;
}
int main() {
int i, j, k;
int M;
long long N, K;
long long ans = 0;
per[1] = per[0] = 1;
for (long long i = 2; i <= 18; i++) per[i] = per[i - 1] * i;
dfs(0, string());
sort(V.begin(), V.end());
cin >> N >> K;
if (CANT(N, K)) {
cout << -1 << endl;
return 0;
}
string S;
long long p;
for (p = 1;; p++) {
if (per[p] >= K) {
break;
}
}
for (i = 0; i < V.size(); i++) {
if (V[i] > N - p) break;
ans++;
}
for (long long temp = N - p + 1; temp <= N; temp++) F.push_back(temp);
go(K, p);
for (long long i = 0; i < out.size(); i++) {
if (ok(i + (N - p + 1)) && ok(out[i])) ans++;
}
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyStore.Builder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class LuckyPermutation {
static StringTokenizer st = null;
static BufferedReader reader = null;
static int perm[] = null;
static boolean used[] = null;
static long fact[] = null;
static void build(int pos, long k) throws Exception {
if (pos == perm.length)
return;
for (int i = 0; i < perm.length; ++i) {
if (!used[i]) {
if (k >= fact[perm.length - 1- pos]) {
k -= fact[perm.length - 1- pos];
} else {
perm[pos] = i;
used[i] = true;
build(pos + 1, k);
return;
}
}
}
throw new Exception();
}
public static void main(String[] args) throws Exception {
reader = new BufferedReader(new InputStreamReader(System.in));
//reader = new BufferedReader(new FileReader("input.txt"));
int n = NextInt();
long k = (long)NextInt();
int length = Math.min(13, n);
fact = new long [20];
fact[0] = 1;
for (int i = 1; i < 20; ++i)
fact[i] = (long)(i) * fact[i - 1];
k -= 1;
if (k >= fact[length]) {
System.out.println("-1");
return;
}
perm = new int[length];
used = new boolean[length];
System.err.println(Arrays.toString(fact));
build(0, k);
System.err.println(Arrays.toString(perm));
long a[] = new long [3000];
int p = 0;
for (int len = 1; len <= 10; ++len) {
for (int mask = 0; mask < (1 << len); ++mask) {
long num = 0;
for (int i = 0; i < len; ++i)
num = num * 10 + 4 + 3 * ((mask & (1 << i)) == 0 ? 1 : 0);
a[p++] = num;
}
}
a = Arrays.copyOf(a, p);
Arrays.sort(a);
int res = 0;
boolean good[] = new boolean[length];
for (int i = 0; i < p; ++i) {
if (a[i] <= n - length)
res += 1;
else
break;
}
for (int i = 0; i < p; ++i) {
if (a[i] >= n - length + 1 && a[i] <= n) {
good[(int) (a[i] - n + length - 1)] = true;
}
}
for (int i = 0; i < length; ++i) {
if (good[i] && good[perm[i]])
++res;
}
System.out.println(res);
}
static String NextToken() throws IOException {
while(st == null || !st.hasMoreTokens())
st = new StringTokenizer(reader.readLine());
return st.nextToken();
}
static int NextInt() throws NumberFormatException, IOException {
return Integer.parseInt(NextToken());
}
} | JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long dx[] = {4LL, 7LL};
vector<int> lucky;
vector<long long> P;
int main() {
P.push_back(1);
for (long long i = 1; P.back() <= 1000000000; i++) P.push_back(P[i - 1] * i);
int n, k;
cin >> n >> k;
if (n < 13 && P[n] < k) {
cout << -1;
return (0);
}
int tmp;
list<int> L;
for (int i = 1, x = n; i < P.size(); i++, x--) {
L.push_front(x);
if (P[i] >= k) {
tmp = x;
break;
}
}
vector<int> perm;
for (int i = L.size() - 1; i >= 0; i--) {
list<int>::iterator it = L.begin();
while (k > P[i]) {
k -= P[i];
it++;
}
perm.push_back(*it);
L.erase(it);
}
{
queue<int> Q;
Q.push(0);
while (!Q.empty()) {
int x = Q.front();
for (int i = 0; i < 2; i++) {
long long x1 = x * 10LL + dx[i];
if (x1 <= 1000000000) {
lucky.push_back(x1);
Q.push(x1);
}
}
Q.pop();
}
}
int res = 0;
for (int i = 0; i < lucky.size() && lucky[i] < tmp; i++) res++;
for (int i = 0, p = tmp; i < perm.size(); i++, p++) {
bool f1 = false;
bool f2 = false;
for (int j = 0; j < lucky.size(); j++) {
if (perm[i] == lucky[j]) f1 = true;
if (p == lucky[j]) f2 = true;
}
if (f1 && f2) res++;
}
cout << res;
exit:
return (0);
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 |
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Comparator;
public class CF121C {
public static void main(String[] args) throws Exception {
boolean local = System.getProperty("ONLINE_JUDGE") == null;
boolean async = false;
Charset charset = Charset.forName("ascii");
FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset);
Task task = new Task(io, new Debug(local));
if (async) {
Thread t = new Thread(null, task, "dalt", 1 << 27);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
t.join();
} else {
task.run();
}
if (local) {
io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M");
}
io.flush();
}
public static class Task implements Runnable {
final FastIO io;
final Debug debug;
int inf = (int) 1e8;
int mod = (int) 1e9 + 7;
public int mod(int val) {
val %= mod;
if (val < 0) {
val += mod;
}
return val;
}
public int mod(long val) {
val %= mod;
if (val < 0) {
val += mod;
}
return (int) val;
}
int bitAt(int x, int i) {
return (x >> i) & 1;
}
int bitAt(long x, int i) {
return (int) ((x >> i) & 1);
}
public Task(FastIO io, Debug debug) {
this.io = io;
this.debug = debug;
}
@Override
public void run() {
solve();
}
Permutation p;
int n;
int total = 0;
public void solve() {
n = io.readInt();
int k = io.readInt();
p = Permutation.newPermutation(n, k);
if (p == null) {
io.cache.append(-1);
return;
}
for (int i = 1; i <= 9; i++) {
count(i, 0);
}
io.cache.append(total);
}
public void count(int step, int val) {
if (step == 0) {
if (val > n) {
return;
}
int e = p.elementAt(val);
if (isValid(e)) {
total++;
}
return;
}
count(step - 1, val * 10 + 4);
count(step - 1, val * 10 + 7);
}
public boolean isValid(int val) {
if (val % 10 != 4 && val % 10 != 7) {
return false;
}
val /= 10;
while (val > 0) {
if (val % 10 != 4 && val % 10 != 7) {
return false;
}
val /= 10;
}
return true;
}
}
public static class Permutation {
int n;
int[] tailPerm;
public static Permutation newPermutation(int n, int k) {
long p = 1;
int t = 0;
for (int i = 1; i <= n && p < k; i++) {
p = p * i;
t = i;
}
if (p < k) {
return null;
}
int[] tailPerm = new int[t];
for (int i = 0; i < t; i++) {
tailPerm[i] = n - i;
}
perm(tailPerm, 0, k, p);
Permutation permutation = new Permutation();
permutation.n = n;
permutation.tailPerm = tailPerm;
return permutation;
}
public int elementAt(int i) {
if (i > n - tailPerm.length) {
return tailPerm[i - (n - tailPerm.length) - 1];
}
return i;
}
public static void perm(int[] tailPerm, int offset, int k, long p) {
if (offset == tailPerm.length) {
return;
}
Arrays.sort(tailPerm, offset, tailPerm.length);
int remain = tailPerm.length - offset;
p /= remain;
for (int i = offset + 1; p < k; i++) {
Memory.swap(tailPerm, offset, i);
k -= p;
}
perm(tailPerm, offset + 1, k, p);
}
}
public static class Memory {
public static <T> void swap(T[] data, int i, int j) {
T tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
public static void swap(char[] data, int i, int j) {
char tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
public static void swap(int[] data, int i, int j) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
public static void swap(long[] data, int i, int j) {
long tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
public static <T> int min(T[] data, int from, int to, Comparator<T> cmp) {
int m = from;
for (int i = from + 1; i < to; i++) {
if (cmp.compare(data[m], data[i]) > 0) {
m = i;
}
}
return m;
}
public static <T> void move(T[] data, int from, int to, int step) {
int len = to - from;
step = len - (step % len + len) % len;
Object[] buf = new Object[len];
for (int i = 0; i < len; i++) {
buf[i] = data[(i + step) % len + from];
}
System.arraycopy(buf, 0, data, from, len);
}
public static <T> void reverse(T[] data, int f, int t) {
int l = f, r = t - 1;
while (l < r) {
swap(data, l, r);
l++;
r--;
}
}
public static void reverse(int[] data, int f, int t) {
int l = f, r = t - 1;
while (l < r) {
swap(data, l, r);
l++;
r--;
}
}
public static void copy(Object[] src, Object[] dst, int srcf, int dstf, int len) {
if (len < 8) {
for (int i = 0; i < len; i++) {
dst[dstf + i] = src[srcf + i];
}
} else {
System.arraycopy(src, srcf, dst, dstf, len);
}
}
public static void fill(int[][] x, int val) {
for (int[] v : x) {
Arrays.fill(v, val);
}
}
public static void fill(int[][][] x, int val) {
for (int[][] v : x) {
fill(v, val);
}
}
}
public static class FastIO {
public final StringBuilder cache = new StringBuilder();
private final InputStream is;
private final OutputStream os;
private final Charset charset;
private StringBuilder defaultStringBuf = new StringBuilder(1 << 8);
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastIO(InputStream is, OutputStream os, Charset charset) {
this.is = is;
this.os = os;
this.charset = charset;
}
public FastIO(InputStream is, OutputStream os) {
this(is, os, Charset.forName("ascii"));
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public long readLong() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
long val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public double readDouble() {
boolean sign = true;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+';
next = read();
}
long val = 0;
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
if (next != '.') {
return sign ? val : -val;
}
next = read();
long radix = 1;
long point = 0;
while (next >= '0' && next <= '9') {
point = point * 10 + next - '0';
radix = radix * 10;
next = read();
}
double result = val + (double) point / radix;
return sign ? result : -result;
}
public String readString(StringBuilder builder) {
skipBlank();
while (next > 32) {
builder.append((char) next);
next = read();
}
return builder.toString();
}
public String readString() {
defaultStringBuf.setLength(0);
return readString(defaultStringBuf);
}
public int readLine(char[] data, int offset) {
int originalOffset = offset;
while (next != -1 && next != '\n') {
data[offset++] = (char) next;
next = read();
}
return offset - originalOffset;
}
public int readString(char[] data, int offset) {
skipBlank();
int originalOffset = offset;
while (next > 32) {
data[offset++] = (char) next;
next = read();
}
return offset - originalOffset;
}
public int readString(byte[] data, int offset) {
skipBlank();
int originalOffset = offset;
while (next > 32) {
data[offset++] = (byte) next;
next = read();
}
return offset - originalOffset;
}
public char readChar() {
skipBlank();
char c = (char) next;
next = read();
return c;
}
public void flush() {
try {
os.write(cache.toString().getBytes(charset));
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public boolean hasMore() {
skipBlank();
return next != -1;
}
}
public static class Debug {
private boolean allowDebug;
public Debug(boolean allowDebug) {
this.allowDebug = allowDebug;
}
public void assertTrue(boolean flag) {
if (!allowDebug) {
return;
}
if (!flag) {
fail();
}
}
public void fail() {
throw new RuntimeException();
}
public void assertFalse(boolean flag) {
if (!allowDebug) {
return;
}
if (flag) {
fail();
}
}
private void outputName(String name) {
System.out.print(name + " = ");
}
public void debug(String name, int x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, long x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, double x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, int[] x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.toString(x));
}
public void debug(String name, long[] x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.toString(x));
}
public void debug(String name, double[] x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.toString(x));
}
public void debug(String name, Object x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, Object... x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.deepToString(x));
}
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, k, l, a[2000], mk[15], s[15];
long long f[15];
map<int, bool> lc;
void F(long long x) {
if (x > 1e9) return;
if (x > 0) {
a[++l] = x;
lc[x] = 1;
}
F(x * 10LL + 4LL);
F(x * 10LL + 7LL);
}
int main() {
f[0] = 1LL;
for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * 1LL * i;
scanf("%d%d", &n, &k);
if (n <= 12 && k > f[n]) {
printf("-1\n");
return 0;
}
int t = 1;
while (f[t] < k) t++;
F(0);
sort(a + 1, a + l + 1);
int sol = 0;
for (int i = 1; i <= l; i++) {
if (a[i] > n - t) break;
sol++;
}
for (int i = 1; i <= t; i++) {
for (int j = 1; j <= t; j++) {
if (mk[j]) continue;
if (k > f[t - i]) {
k -= f[t - i];
continue;
}
s[i] = j + n - t;
mk[j] = 1;
break;
}
}
for (int i = 1; i <= t; i++)
if (lc.count(i + n - t) && lc.count(s[i])) sol++;
printf("%d\n", sol);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class C{
public static void main(String[] args) throws Exception{
new C().run();
}
static final long M = 4444444444L;
ArrayList<Long> ls = new ArrayList<Long>();
void dfs(long cur){
if(cur > M)return;
if(cur!=0)ls.add(cur);
dfs(cur*10+4);
dfs(cur*10+7);
}
long[] fact = new long[14];
void run() throws Exception{
Scanner sc = new Scanner(System.in);
//BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
// only sc.readLine() is available
dfs(0);
Collections.sort(ls);
//System.out.printf("ls size = %d\n", ls.size());
fact[0] = fact[1] = 1;
for(int i = 2; i < fact.length; i++)fact[i] = fact[i-1]*i;
int n = sc.nextInt();
long k = sc.nextInt()-1;
if(n <= 13){
if(k >= fact[n]){
System.out.println(-1);
return ;
}
}
int ans = 0;
int tail = min(n, 13);
for(Long v : ls)if(v <= n-tail)ans++;
ArrayList<Long> rest = new ArrayList<Long>();
for(int i = tail-1; i >= 0; i--) rest.add((long)(n-i));
int index = n - tail + 1;
for(int i = 0; i < tail; i++){
long val = 0;
if(k >= fact[tail-i-1]){
int w = (int)(k / fact[tail-i-1]);
k %= fact[tail-i-1];
val = rest.get(w);
rest.remove((Long)val);
if(Collections.binarySearch(ls, val) >= 0 && Collections.binarySearch(ls, 1L*index) >= 0)ans++;
}else{
val = rest.get(0);
rest.remove((Long)val);
if(Collections.binarySearch(ls, val) >= 0 && Collections.binarySearch(ls, 1L*index) >= 0)ans++;
}
//System.out.printf("%d is %d\n", index, val);
index++;
}
System.out.println(ans);
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int M = 10 + 10;
long long int lucky[10000];
long long int fac(long long int n) {
long long int ans = 1;
for (long long int i = 1; i <= n; i++) {
ans = ans * i;
}
return ans;
}
bool islucky(long long int x) {
bool ok = true;
while (x) {
if (x % 10 != 4 && x % 10 != 7) {
ok = false;
break;
}
x /= 10;
}
return ok;
}
int main() {
long long int n, k;
cin >> n >> k;
long long int num = 1;
long long int id = 1;
while (n - id + 1 >= 1) {
num = num * id;
if (num >= k) {
break;
}
id++;
}
if (num < k) {
cout << "-1" << endl;
} else {
long long int idl = n - id + 1;
long long int idr = n;
int len = idr - idl + 1;
int a[M];
set<int> ilist;
for (int i = 0; i < len; i++) {
a[i] = idl + i;
ilist.insert(i);
}
int idx[M];
int res = len - 1;
long long int kk = k - 1;
for (int i = 0; i < len; i++) {
long long int f = fac(res);
set<int>::iterator it = ilist.begin();
for (long long int j = 1; j <= kk / f; j++) {
it++;
}
idx[i] = (*it);
ilist.erase(idx[i]);
kk = kk - kk / f * f;
res--;
}
int ans = 0;
for (int i = 0; i < len; i++) {
if (islucky(idx[i] + idl) && islucky(a[i])) {
ans++;
}
}
int num = 0;
for (int d = 1; d <= 9; d++) {
for (int i = 0; i < (1 << d); i++) {
int val = 0;
for (int k = 0; k < d; k++) {
if ((i >> k) & 1) {
val = val * 10 + 4;
} else {
val = val * 10 + 7;
}
}
lucky[num++] = val;
}
}
for (int i = 0; i < num; i++) {
if (lucky[i] < idl) {
ans++;
}
}
cout << ans << endl;
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Main {
static long[] fact;
public static void main(String[] args) {
// ArrayList<String> all = new ArrayList<String>();
// char[] a = "1234567".toCharArray();
//
// go(0, a, new char[7], all, new boolean[7]);
//
// Collections.sort(all);
//
// for(String s : all)
// System.out.println(s);
ArrayList<Long> all = new ArrayList<Long>();
go("0", all);
Collections.sort(all);
fact = new long[16];
fact[0] = 1;
for(int i = 1; i < 16; i++)
fact[i] = i * fact[i-1];
Scanner r = new Scanner(System.in);
int n = r.nextInt();
int k = r.nextInt();
if(n < 15 && k > fact[n]){
System.out.println(-1);
return;
}
k--;
int start = Math.max(0, n-15);
int sum = 0;
int length = n - start;
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i = 0; i < length; i++)
a.add(i + start + 1);
boolean[] v = new boolean[length];
int[] p = new int[length];
generate(0, length, a, p, v, k);
// System.out.println(Arrays.toString(p));
for(int i = 0; i < length; i++){
long index = i + start + 1;
long val = p[i];
if(all.contains(index) && all.contains(val))sum++;
}
for(long i: all){
long index = i-1;
if(index >= start)break;
sum++;
}
System.out.println(sum);
}
private static int[] generate(int i, int length, ArrayList<Integer> a, int[] p, boolean[] v, long k) {
if(length == 0){
return p;
}else{
int index = (int) (k / fact[length - 1]);
p[i] = a.get(index);
a.remove(index);
return generate(i+1, length-1, a, p, v, k % fact[length-1]);
}
}
private static void go(int i, char[] a, char[] p, ArrayList<String> all, boolean[] v) {
if(i == 7){
all.add(new String(p));
}else{
for(int j = 0; j < 7; j++)if(!v[j]){
v[j] = true;
p[i] = a[j];
go(i+1, a, p, all, v);
v[j] = false;
}
}
}
private static void go(String s, ArrayList<Long> all) {
if(s.length() <= 10){
if(!s.equals("0"))
all.add(Long.parseLong(s));
if(s.length() == 10)return;
}
go(s+"4", all);
go(s+"7", all);
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long numlucky(long long i, long long bound) {
if (i > bound)
return 0;
else
return 1 + numlucky(i * 10 + 4, bound) + numlucky(i * 10 + 7, bound);
}
bool check(long long i) {
if (i <= 0) return false;
while (i > 0) {
int a = i % 10;
if (a != 4 && a != 7) return false;
i = i / 10;
}
return true;
}
int main() {
std::ios_base::sync_with_stdio(false);
long long n, k;
cin >> n >> k;
long long fact[14];
fact[1] = 1;
for (long long i = 2; i <= 13; i++) fact[i] = i * fact[i - 1];
if (n < 14 && fact[n] < k) {
cout << -1 << endl;
return 0;
}
long long num = numlucky(0, max((long long)0, n - 13)) - 1;
int t = 1;
k -= 1;
vector<int> perm;
for (int i = n - 12; i <= n; i++) perm.push_back(i);
for (int i = 13; i >= 2; i--) {
int index = k / fact[i - 1];
if (check(perm[index]) && check(n - i + 1)) num++;
perm.erase(perm.begin() + index);
k = k % fact[i - 1];
}
if (check(perm[0]) && check(n)) num++;
cout << num << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.io.*;
import java.math.*;
import java.util.*;
/*public class TaskC {
}
*/
public class TaskC implements Runnable {
// based on Petr's template
private void solve() throws IOException {
ArrayList< Integer > luckies = new ArrayList< Integer >();
for ( int i = 1; i <= 9; i ++ )
for ( int mask = 0; mask < ( 1 << i ); mask ++ ){
int num = 0;
for ( int j = 0; j < i; j ++ ){
num = num * 10;
if ( ( mask & ( 1 << j ) ) != 0 )
num += 7;
else
num += 4;
}
luckies.add( num );
}
long[] all = new long[ luckies.size() ];
for ( int i = 0; i < luckies.size(); i ++ )
all[i] = luckies.get( i );
Arrays.sort( all );
int res = 0;
int[] fac = new int[13];
fac[0] = 1;
for ( int i = 1; i < 13; i ++ )
fac[i] = fac[ i - 1 ] * i;
int n = this.nextInt();
int start = Math.max( n - 12, 1 );
int K = this.nextInt() - 1;
for ( int i = 0; i < all.length; i ++ )
if ( all[i] < start )
res ++;
if ( n < 13 && K >= fac[n] ){
System.out.println( -1 );
return;
}
if ( n < 4 ){
System.out.println( 0 );
return;
}
int F = Math.min( n - 1, 12 );
int[] vst = new int[13];
for ( int i = start; i <= n; i ++ ){
int next = 0;
for ( int j = 0; j < 13; j ++ )
if ( vst[j] == 0 )
if ( K >= fac[F] )
K -= fac[F];
else{
next = start + j;
break;
}
// System.out.println( F + " " + i + " " + next );
vst[ next - start ] = 1;
if ( lucky( next ) && lucky( i ) )
res ++;
F --;
}
System.out.println( res );
}
private boolean lucky(int v) {
// TODO Auto-generated method stub
while( v > 0 ){
if ( v % 10 == 4 || v % 10 == 7 )
v /= 10;
else
return false;
}
return true;
}
public static void main(String[] args) {
new TaskC().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = null;
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
boolean hasNext() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String nextLine = reader.readLine();
if (nextLine == null)
return false;
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
return reader.readLine();
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
BigInteger nextBigInteger() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return new BigInteger(tokenizer.nextToken());
}
} | JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<long long> luckies;
long long x;
void gen_luck(int k, int pos, long long cur) {
if (k == pos) {
x = cur;
if (cur <= 1e10) luckies.push_back(cur);
} else {
gen_luck(k, pos + 1, cur * 10 + 4);
gen_luck(k, pos + 1, cur * 10 + 7);
}
}
void Init() {
for (int i = 1;; ++i) {
gen_luck(i, 0, 0);
if (x > 1e10) break;
}
}
vector<int> perm;
vector<bool> u;
long long fact(long long n) {
long long res = 1;
for (long long i = 2; i <= n; ++i) res *= i;
return res;
}
void gen(long long l, long long pos, long long num) {
if (pos == l) return;
long long sz = l - pos;
int cur = 1;
while ((cur - 1) * fact(sz - 1) < num) ++cur;
--cur;
int cnt = 0, j = 0;
while (cnt < cur) {
if (!u[j]) ++cnt;
++j;
}
--j;
u[j] = true;
perm[pos] = j;
gen(l, pos + 1, num - (cur - 1) * fact(sz - 1));
}
bool lucky(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7) return false;
n /= 10;
}
return true;
}
int low(long long n) {
int l = 0, r = luckies.size();
while (l < r - 1) {
int m = (l + r) / 2;
if (luckies[m] < n)
l = m;
else
r = m;
}
if (n < 4)
return 0;
else
return (luckies[r] <= n ? r + 1 : l + 1);
}
int main() {
Init();
long long n, k;
cin >> n >> k;
if (n <= 13 && fact(n) < k) {
cout << -1;
return 0;
}
int len = 1;
while (fact(len) < k && len < 13) ++len;
u.assign(len, false);
perm.resize(len);
gen(len, 0, k);
vector<long long> res(len);
long long start = n - len + 1;
long long ans = low(n - len);
for (int i = 0; i < len; ++i) res[i] = perm[i] + start;
for (int i = 0; i < len; ++i) {
if (lucky(n - len + i + 1) && lucky(res[i])) ++ans;
}
cout << ans << endl;
cin >> n;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int table[20];
long long tot = 0;
int nns[20];
bool isluck(int k) {
while (k) {
if (k % 10 != 4 && k % 10 != 7) return false;
k /= 10;
}
return true;
}
int luckns[2000];
int tol = 0;
void maketable(int i, int np) {
if (i == 9) return;
int np1 = np * 10 + 4, np2 = np * 10 + 7;
luckns[tol++] = np1;
luckns[tol++] = np2;
maketable(i + 1, np1);
maketable(i + 1, np2);
}
int main() {
maketable(0, 0);
table[1] = 1;
for (tot = 2; table[tot - 1] * tot < 1000000000; ++tot)
table[tot] = table[tot - 1] * tot;
tot--;
int n, k;
scanf("%d%d", &n, &k);
if (n <= tot && table[n] < k)
puts("-1");
else {
int pi;
for (pi = tot; table[pi] >= k; --pi)
;
int summ = 0;
for (int i = 0; i < tol; ++i)
if (luckns[i] >= 1 && luckns[i] < n - pi) summ++;
for (int i = n - pi; i <= n; ++i) nns[i - n + pi + 1] = i;
int top = pi;
int tns = n - pi;
k--;
while (top) {
int p1 = k / table[top] + 1;
for (int i = 1; i <= pi + 1; ++i)
if (nns[i] != -1) {
p1--;
if (p1 == 0) {
if (isluck(nns[i]) && isluck(tns)) summ++;
nns[i] = -1;
break;
}
}
k %= table[top];
tns++;
top--;
}
for (int i = 1; i <= pi + 1; ++i)
if (nns[i] != -1) {
if (isluck(nns[i]) && isluck(n)) summ++;
break;
}
printf("%d\n", summ);
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long jc[15];
int da[15];
void sjr(int a, long long b, long long c) {
sort(da + a, da + a + c);
if (c == 1) return;
if (b <= jc[c - 1]) sjr(a + 1, b, c - 1);
for (long long i = 1; i <= c; i++) {
if (b <= i * jc[c - 1]) {
swap(da[a], da[a + i - 1]);
b -= (i - 1) * jc[c - 1];
sjr(a + 1, b, c - 1);
break;
}
}
}
bool ju(int a) {
char cc[100];
sprintf(cc, "%d", a);
for (int i = 0; i < strlen(cc); i++)
if (cc[i] != '4' && cc[i] != '7') return false;
return true;
}
int main() {
vector<unsigned long long> vd;
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < (1 << i); j++) {
unsigned long long ta = 0;
for (int k = 0; k < i; k++) {
if (j & (1 << k))
ta += pow(10LL, k * 1LL) * 7LL;
else
ta += pow(10LL, k * 1LL) * 4LL;
}
vd.push_back(ta);
}
}
jc[0] = 1;
jc[1] = 1;
for (long long i = 2; i <= 14; i++) jc[i] = jc[i - 1] * i;
long long n, k;
cin >> n >> k;
if (n <= 14 && k > jc[n]) {
printf("-1\n");
return 0;
}
int j = 13;
int bh = n;
int fz = 0;
for (int i = 0; i < 13 && n - i > 0; i++) {
da[--j] = n - i;
fz++;
bh--;
}
sjr(j, k, fz);
int jx = 0;
for (int i = 0; i < vd.size(); i++) {
if (vd[i] <= bh) jx++;
}
for (bh++; bh <= n; bh++) {
if (ju(da[j]) && ju(bh)) jx++;
j++;
}
cout << jx << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int luck[5000], top;
int a[20], fa[20] = {1};
bool used[20];
void make(long long x) {
if (x > 1e9) return;
luck[top++] = x;
make(x * 10 + 4);
make(x * 10 + 7);
}
bool is(int x) { return x == 0 || (x % 10 == 4 || x % 10 == 7) && is(x / 10); }
void inttoarray(int k, int n) {
int i, j, tmp;
for (i = 1; i <= n; i++) {
tmp = k / fa[n - i];
for (j = 1; j <= n; j++)
if (!used[j]) {
if (tmp == 0) break;
tmp--;
}
a[i] = j;
used[j] = 1;
k %= fa[n - i];
}
}
int main() {
int n, k, i, j, as = 0;
for (i = 1; i < 20; i++) fa[i] = fa[i - 1] * i;
scanf("%d%d", &n, &k);
k--;
if (n <= 13) {
if (k >= fa[n]) return puts("-1");
inttoarray(k, n);
if (a[4] == 4 || a[4] == 7) as++;
if (a[7] == 4 || a[7] == 7) as++;
printf("%d\n", as);
return 0;
}
make(0);
sort(luck + 1, luck + top);
inttoarray(k, 13);
for (i = 1; i <= 13; i++)
if (is(i + n - 13) && is(a[i] + n - 13)) as++;
for (i = 1; i < top; i++)
if (luck[i] > n - 13) break;
as += i - 1;
printf("%d\n", as);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
long long b[32] = {0};
bool f(int x) {
if (x <= 0) {
return false;
}
while (x) {
if (x % 10 != 4 && x % 10 != 7) {
return false;
}
x /= 10;
}
return true;
}
int get(int x) {
int w = 0, s = 0, t = 0, i = 0, xx = 0, ans = 0;
long long now = 0;
xx = x;
while (xx) {
t++;
xx /= 10;
}
for (w = 1; w <= t; w++) {
for (s = 0; s < (1 << w); s++) {
now = 0;
for (i = 0; i < w; i++) {
if (s & (1 << i)) {
now = now * 10 + 4;
} else {
now = now * 10 + 7;
}
}
if (now <= x) {
ans++;
}
}
}
return ans;
}
void tt(int m, int k, int a[]) {
int i = 0, j = 0;
bool u[32] = {0};
for (i = 0; i < m; i++) {
for (j = 0; j < m; j++) {
if (!u[j]) {
if (k > b[m - i - 1]) {
k -= b[m - i - 1];
} else {
a[i] = j;
u[j] = true;
break;
}
}
}
}
}
void init() {
b[0] = 1;
int i = 0;
for (i = 1; i <= 20; i++) {
b[i] = b[i - 1] * i;
}
}
int main() {
int n = 0, k = 0, m = 0, i = 0, ans = 0, a[32];
init();
while (scanf("%d%d", &n, &k) == 2) {
for (m = 1;; m++) {
if (b[m] >= k) {
break;
}
}
if (n < m) {
printf("-1\n");
continue;
}
ans = get(n - m);
tt(m, k, a);
for (i = 0; i < m; i++) {
if (f(a[i] + n - m + 1) && f(i + n - m + 1)) {
ans++;
}
}
printf("%d\n", ans);
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.util.ArrayList;
import java.util.Scanner;
public class e {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), k = in.nextInt();
if(k > count(n)) {
System.out.println(-1);
return;
}
int ans = 0;
int mi = Math.max(1, n-13);
find("");
for(long l : list) {
if(l < mi)
ans++;
}
ArrayList<Integer> unused = new ArrayList<>();
for(int i=mi;i<=n;i++) {
unused.add(i);
}
ans += solve(mi, unused, k);
System.out.println(ans);
}
static int count(int n) {
if(n == 0)
return 1;
if(n > 12)
return 2000000000;
return count(n-1) * n;
}
static boolean isLucky(int v) {
String s = v + "";
boolean ans = true;
for(int i=0;i<s.length();i++) {
ans &= s.charAt(i) == '4' || s.charAt(i) == '7';
}
return ans;
}
static int solve(int idx, ArrayList<Integer> unused, int k) {
if(unused.size() == 0)
return 0;
int ncnt = count(unused.size()-1);
for(int i=0;i<unused.size();i++) {
if(k <= ncnt) {
// System.out.print(unused.get(i) + "[" + idx + "]" + " ");
boolean lucky = isLucky(idx) && isLucky(unused.get(i));
unused.remove(i);
return solve(idx+1, unused, k) + (lucky ? 1 : 0);
}
k -= ncnt;
}
return 0;
}
static ArrayList<Long> list = new ArrayList<>();
static void find(String s) {
if(s.length() >= 11)
return;
if(s.length() > 0) {
long v = Long.parseLong(s);
list.add(v);
}
find(s + "4");
find(s + "7");
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.util.*;
import java.io.*;
public class C121 {
public static long[] fact = new long[15];
public static void main(String[] args) throws IOException {
List<Long> list = new ArrayList<Long>();
for (int len = 1; len <= 10; len++) {
for (int which = 0; which < (1<<len); which++) {
String str = "";
long lucky = 0;
long mult = 1;
for (int bit = 0; bit < len; bit++) {
if ((which&(1<<bit))!=0) lucky += mult * 7;
else lucky += mult * 4;
mult *= 10;
}
list.add(lucky);
if (len==10) break;
}
}
fact[0] = 1;
for (int i = 1; i < fact.length; i++) {
fact[i] = ((long)i)*fact[i-1];
}
Scanner s = new Scanner(System.in);
PrintStream out = System.out;
long n = s.nextInt(), k = s.nextInt();
if (n < fact.length && k > fact[(int)n]) {
System.out.println("-1");
return;
}
int digits = (int)Math.min(14, n);
long[] lastDigits = new long[digits];
long realIndex = n - digits + 1;
for (int i = 0; i < digits; i++) {
lastDigits[i] = realIndex + i;
}
lastDigits = applyPerm(lastDigits, (int)digits, (int)(k-1));
long ct = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) <= n-digits) ct++;
else break;
}
// ct += number of lucky in [1,n-digits]
for (int i = 0; i < digits; i++) {
if (lucky(realIndex+i) && lucky(lastDigits[i])) {
ct++;
}
}
//System.out.println(Arrays.toString(lastDigits));
System.out.println(ct);
}
public static boolean lucky(long n) {
String s = ""+n;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i)!='4' && s.charAt(i)!='7') return false;
}
return true;
}
public static long[] applyPerm(long[] array, int n, int k) {
List<Integer> remain = new ArrayList<Integer>();
int[] perm = new int[n];
for (int i = 0; i < n; i++) remain.add(i);
for (int at = 0; at < n; at++) {
int i = n - 1 - at;
int index = (int)(k/fact[i]);
perm[at] = remain.get(index);
k %= fact[i];
remain.remove(index);
}
long[] newArray = new long[n];
for (int i = 0; i < n; i++) {
newArray[i] = array[perm[i]];
}
return newArray;
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long fac[51];
map<long long, bool> lc;
int32_t main() {
vector<long long> all;
for (long long i = 1; i <= 10; ++i) {
for (long long j = 0; j <= i; ++j) {
vector<long long> v;
for (long long k = 0; k < j; ++k) v.push_back(4);
for (long long k = 0; k < i - j; ++k) v.push_back(7);
do {
long long t = 0, c = 1;
for (long long k : v) t += c * k, c *= 10;
all.push_back(t);
} while (next_permutation(v.begin(), v.end()));
}
}
sort(all.begin(), all.end());
fac[0] = 1;
for (long long i = 1; i <= 50; ++i) fac[i] = fac[i - 1] * i;
vector<long long> v;
long long n, k;
cin >> n >> k;
if (n <= 20 && fac[n] < k) return cout << "-1" << endl, 0;
--k;
for (long long i : all)
if (i <= n) v.push_back(i);
long long z = -1;
for (long long i = n; i >= 1; --i) {
long long c = n - i;
if (c * fac[c] >= k) {
z = i;
break;
}
}
if (z == -1) z = 1;
long long ans = 0;
for (long long i : v)
if (i < z) ++ans;
for (long long i : v) lc[i] = true;
long long cur = 0;
vector<long long> ch;
for (long long i = z; i <= n; ++i) ch.push_back(i);
for (long long i = z; i <= n; ++i) {
long long d = 31, p = 0;
while (d--) {
if (p + (1 << d) < ch.size()) {
long long u = ch[p + (1 << d)];
long long c = p + (1 << d);
if (cur + c * fac[n - i] <= k) p += (1 << d);
}
}
cur += p * fac[n - i];
if (lc[i] && lc[ch[p]]) ++ans;
vector<long long> nch;
for (long long j : ch)
if (j != ch[p]) nch.push_back(j);
ch = nch;
}
cout << ans << endl;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.awt.*;
import java.io.*;
import java.math.*;
import java.util.*;
import static java.lang.Math.*;
public class BetaRound91_Div1_C implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
public static void main(String[] args) {
new Thread(null, new BetaRound91_Div1_C(), "", 256 * (1L << 20)).start();
}
@Override
public void run() {
try {
long startTime = System.currentTimeMillis();
if (System.getProperty("ONLINE_JUDGE") != null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
Locale.setDefault(Locale.US);
tok = new StringTokenizer("");
solve();
in.close();
out.close();
long endTime = System.currentTimeMillis();
System.err.println("Time = " + (endTime - startTime));
long freeMemory = Runtime.getRuntime().freeMemory();
long totalMemory = Runtime.getRuntime().totalMemory();
System.err.println("Memory = " + ((totalMemory - freeMemory) >> 10));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
/** http://pastebin.com/j0xdUjDn */
static class Utils {
private Utils() {}
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static final int MAGIC_VALUE = 50;
private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergeSort(a, leftIndex, middleIndex);
mergeSort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
int[] leftArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
}
// solution
long[] fact = new long[21];
ArrayList<Long> list = new ArrayList<Long>();
void gen() {
for (int len = 1; len <= 10; len++) {
for (int mask = 0; mask < (1 << len); mask++) {
long res = 0;
for (int i = 0; i < len; i++) {
if (((1 << i) & mask) != 0) {
res = 10*res + 7;
} else {
res = 10*res + 4;
}
}
list.add(res);
}
}
Collections.sort(list);
fact[0] = 1;
for (int i = 1; i <= 20; i++) {
fact[i] = fact[i - 1] * i;
}
}
void solve() throws IOException {
gen();
long n = readLong();
long k = readLong();
if (n <= 3) {
if (k <= fact[(int)n]) {
out.println(0);
} else {
out.println(-1);
}
return;
}
if (k == 1) {
long ans = Collections.binarySearch(list, n);
if (ans < 0) {
ans = -ans - 1;
} else {
ans++;
}
out.println(ans);
return;
}
int ki = Arrays.binarySearch(fact, k);
if (ki < 0) {
ki = -ki - 1;
}
long stay = n - ki;
if (stay < 0) {
out.println(-1);
return;
}
long ans = Collections.binarySearch(list, stay);
if (ans < 0) {
ans = -ans - 1;
} else {
ans++;
}
p = new long[ki];
generatePermutation(ki, k);
for (int i = 0; i < ki; i++) {
if (isLucky(stay + i + 1) && isLucky(stay + p[i])) {
ans++;
}
}
out.println(ans);
}
boolean isLucky(long x) {
if (x == 0) return false;
while (x > 0) {
if (x % 10 != 4 && x % 10 != 7) {
return false;
}
x /= 10;
}
return true;
}
long[] p;
void generatePermutation(int n, long k) {
boolean[] used = new boolean[n+1];
k--;
int nn = n;
for (int pos = 0; pos < nn; pos++) {
long d = k / fact[n-1];
int i = 0;
int count = 0;
while (i < nn) {
i++;
if (used[i]) continue;
if (count == d) {
used[i] = true;
p[pos] = i;
break;
}
count++;
}
k -= d * fact[n-1];
n--;
}
}
} | JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | digits = [4, 7]
nums = [4, 7]
LIM = 10**10
while nums[-1] < LIM:
nums = list(set(nums + [num*10 + digit for digit in digits for num in nums]))
lucky_nums = list(sorted(nums))
def e(n, k):
fact = [1, 1]
x = 2
while True:
new = fact[x-1] * x
if new > LIM:
break
fact.append(new)
x += 1
if n < len(fact) and fact[n] < k:
return -1
i = 1
while k > fact[i]:
i += 1
ii = i
nums = []
k -= 1
start = n - i + 1
used = [False] * 30
while ii > 0:
f = fact[ii-1]
digit = k / f
digit_2 = digit
for j in range(len(used)):
if not used[j]:
if digit_2 == 0:
used[j] = True
nums.append(j + start)
digit_2 -= 1
k %= f
ii -= 1
answer_count = 0
for num in lucky_nums:
if num <= n:
if num < start:
answer_count += 1
elif nums[num - start] in lucky_nums:
answer_count += 1
return answer_count
'''
print e(2, 5)
print '--'
print e(7, 3)
print '--'
print e(7, 4)
print '--'
print e(4, 1)
print '--'
print e(4, 6)
print '--'
print e(4, 7)
print '--'
'''
n, k = [int(_) for _ in raw_input().split(" ")]
print e(n, k)
| PYTHON |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.io.*;
import java.util.*;
import java.math.*;
public class Main implements Runnable
{
BufferedReader reader;
StringTokenizer strtok;
HashSet<Long> happyNums = new HashSet<Long>();
int[] digits = new int[20];
void fillList(int count, int position)
{
digits[position] = 4;
if (position == count - 1)
{
long result = 0;
long curpow = 1;
for (int i = position; i >= 0; i--)
{
result += digits[i] * curpow;
curpow *= 10;
}
happyNums.add(result);
}
else
{
fillList(count, position + 1);
}
digits[position] = 7;
if (position == count - 1)
{
long result = 0;
long curpow = 1;
for (int i = position; i >= 0; i--)
{
result += digits[i] * curpow;
curpow *= 10;
}
happyNums.add(result);
}
else
{
fillList(count, position + 1);
}
}
void solve() throws Exception
{
int n = nextInt(), k = nextInt();
for (int i = 1; i <= 10; i++)
{
fillList(i, 0);
}
long fact = 1;
for (int i = 1; i <= n; i++)
{
fact *= i;
if (fact >= k)
{
break;
}
}
if (fact < k)
{
System.out.println(-1);
return;
}
int[] arr = new int[Math.min(n, 13)];
for (int i = 0; i < arr.length; i++)
{
arr[i] = n - arr.length + i + 1;
}
for (int i = 0; i < arr.length; i++)
{
long fact2 = 1;
for (int j = 1; j < arr.length - i; j++)
{
fact2 *= j;
}
long position = (k - 1) / fact2;
int el = arr[(int)position + i];
for (int j = (int)position; j > 0; j--)
{
arr[j + i] = arr[j + i - 1];
}
arr[i] = el;
k -= position * fact2;
if (k <= 1)
break;
}
int answer = 0;
for (long hn : happyNums)
{
if (hn < n - arr.length + 1)
{
answer++;
}
}
for (int i = 0; i < arr.length; i++)
{
long x = n - arr.length + i + 1;
if (happyNums.contains((long)(n - arr.length + i + 1)) && happyNums.contains((long)arr[i]))
{
answer++;
}
}
System.out.println(answer);
/*
while (k > 1)
{
long fact2 = 1;
int last = 1;
while (fact2 * (last + 1) < k)
{
fact2 *= last + 1;
last++;
}
int lastnum = arr[arr.length - 1];
for (int i = 0; i < last; i++)
{
arr[arr.length - i - 1] = arr[arr.length - i - 2];
}
arr[arr.length - last - 1] = lastnum;
k -= fact2;
}*/
}
public static void main(String[] args)
{
new Thread(null, new Main(), "Solution", 16 * 1024 * 1024).start();
}
@Override
public void run()
{
try
{
if (System.getProperty("ONLINE_JUDGE") == null)
{
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream("output.txt"));
}
reader = new BufferedReader(new InputStreamReader(System.in));
solve();
}
catch (Exception e)
{
e.printStackTrace();
}
}
String nextToken() throws IOException
{
if (strtok == null || !strtok.hasMoreTokens())
{
strtok = new StringTokenizer(reader.readLine());
}
return strtok.nextToken();
}
int nextInt() throws IOException
{
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException
{
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException
{
return Double.parseDouble(nextToken());
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 |
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
public class E {
static int[] Fac;
static int[] P;
static int n;
static HashSet<Integer> H = new HashSet<Integer>();
static int count = 0;
static int start;
static ArrayList<Long> Q = new ArrayList<Long>();
static int inf = (int) 1e9;
public static boolean isLucky(int x) {
while (x > 0) {
if (x % 10 != 4 && x % 10 != 7)
return false;
x /= 10;
}
return true;
}
public static void generate(int pos, int k) {
if (pos > n)
return;
for (int i = start; i <= n; i++) {
if (H.contains(i))
continue;
int left = n - start - H.size();
if (Fac[left] < k) {
k -= Fac[left];
} else {
H.add(i);
if (isLucky(pos) && isLucky(i))
count++;
generate(pos + 1, k);
}
}
}
public static void generate(long x) {
Q.add(10 * x + 4);
Q.add(10 * x + 7);
if (x < inf) {
generate(10 * x + 4);
generate(10 * x + 7);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
int k = in.nextInt();
Fac = new int[13];
Fac[0] = 1;
for (int i = 1; i < 13; i++)
Fac[i] = Fac[i - 1] * i;
if (n < 13 && k > Fac[n]) {
System.out.println(-1);
return;
}
start = 12;
while (start >= 0 && Fac[start] >= k)
start--;
int pos = start = n - start;
generate(pos, k);
generate(0);
for (long x : Q)
if (x < start)
count++;
System.out.println(count);
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<long long> v, ext;
long long f[20];
bool vis[20];
bool chck(long long j) {
while (j) {
if (j % 10 != 4 && j % 10 != 7) break;
j /= 10;
}
return j == 0;
}
int main() {
long long i, n, k, st, sum, j, pre, sz, ans;
f[0] = 1;
for (i = 1; i <= 13; i++) f[i] = f[i - 1] * i;
cin >> n >> k;
if (n <= 13 && k > f[n]) {
cout << -1 << endl;
return 0;
}
for (i = 1; f[i] < k; i++)
;
st = n - i + 1;
sum = k;
for (j = 1; j <= i; j++) {
for (k = st; k <= n; k++) {
if (!vis[k - st]) {
if (sum - f[i - j] > 0) {
sum -= f[i - j];
} else {
ext.push_back(k);
vis[k - st] = 1;
break;
}
}
}
}
queue<long long> q;
q.push(4);
q.push(7);
v.push_back(4);
v.push_back(7);
while (!q.empty()) {
pre = q.front();
q.pop();
if (pre * 10 + 4 <= 5000000000LL) {
q.push(pre * 10 + 4);
q.push(pre * 10 + 7);
v.push_back(pre * 10 + 4);
v.push_back(pre * 10 + 7);
}
}
sz = v.size();
ans = 0;
for (i = 0; i < sz && v[i] < st; i++) ans++;
for (i = st; i <= n; i++) {
if (chck(i)) {
if (chck(ext[i - st])) ans++;
}
}
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long l[5010];
int tot;
long long ans, n, k;
void prepare() {
tot = 2;
l[0] = 4, l[1] = 7;
for (int i = 0; i < tot; i++)
if (l[i] > 2e9)
break;
else
l[tot++] = l[i] * 10 + 4, l[tot++] = l[i] * 10 + 7;
}
bool inside(long long t) {
for (int i = 0; i < tot; i++)
if (l[i] == t) return 1;
return 0;
}
long long deal(int b, int n) {
long long ret = 0, temp = 1;
for (int i = 1; i <= n; i++) temp *= i;
bool v[20];
memset(v, 0, sizeof v);
if (temp < k) return -0x7ffffff;
k--;
for (int i = n; i >= 1; i--) {
temp /= i;
int t = k / temp + 1, j;
k -= (t - 1) * temp;
for (j = 1; j <= n; j++) {
if (!v[j]) t--;
if (!t) break;
}
v[j] = 1;
if (inside(b + j) && inside(b + n - i + 1)) ret++;
}
return ret;
}
int main() {
prepare();
cin >> n >> k;
if (n <= 15)
ans = deal(0, n);
else {
for (int i = 0; l[i] <= n - 15; i++) ans++;
ans += deal(n - 15, 15);
}
if (ans < 0)
puts("-1");
else
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
void run();
int main() {
ios::sync_with_stdio(0);
run();
}
long long fac[20] = {1};
bool lucky(long long i) {
return i == 0 or ((i % 10 == 4) or (i % 10 == 7)) and lucky(i / 10);
}
void run() {
long long n, m;
cin >> n >> m;
--m;
for (long long i = 1; i < 20; i++) fac[i] = fac[i - 1] * i;
if (n <= 16 and fac[n] <= m) {
cout << "-1" << endl;
return;
}
long long red = max(0ll, n - 14ll);
long long offset = red + 1;
long long res = 0;
vector<long long> luck[20];
luck[0].push_back(0);
--res;
for (long long len = 1, p = 1; not luck[len - 1].empty(); ++len, p *= 10ll) {
for (auto j : luck[len - 1]) {
if (j + 4 * p <= red) luck[len].push_back(j + 4 * p);
if (j + 7 * p <= red) luck[len].push_back(j + 7 * p);
++res;
}
}
vector<long long> consider;
for (long long i = offset; i <= n; i++) consider.push_back(i);
while (not consider.empty()) {
long long f = fac[consider.size() - 1];
long long i = m / f;
m -= i * f;
if (lucky(n - consider.size() + 1) and lucky(consider[i])) ++res;
consider.erase(consider.begin() + i);
}
cout << res << endl;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long int fact[14];
int n, k;
int cs, s;
vector<int> v;
vector<long long int> lnum;
void Set() {
fact[0] = 1;
for (long long int i = 1; i < 14; i++) fact[i] = i * fact[i - 1];
}
void create_lucky(int i) {
for (int i = cs; i < s; i++)
lnum.push_back(lnum[i] * 10 + 4), lnum.push_back(lnum[i] * 10 + 7);
}
bool lucky(int num) {
while (num) {
if (num % 10 != 4 && num % 10 != 7) return false;
num /= 10;
}
return true;
}
int main() {
lnum.push_back(4);
lnum.push_back(7);
cs = 0;
s = 2;
for (int i = 2; i <= 9; i++) {
create_lucky(i);
cs = s;
s = lnum.size();
}
lnum.push_back(4444444444);
Set();
cin >> n >> k;
if (n <= 13 && fact[n] < k) {
cout << -1 << endl;
return 0;
}
int dig = 1;
for (int i = 1; fact[i] < k; i++) dig++;
for (int i = n - dig + 1; i <= n; i++) {
v.push_back(i);
}
int cp = n - dig, cs = v.size(), pi = 1, cnt = 0;
while (1) {
int cn = 0;
while (k > cn * fact[cs - 1]) cn++;
k = k - (cn - 1) * fact[cs - 1];
int cnum = v[cn - 1];
int pos = cp + pi;
pi++;
v.erase(v.begin() + cn - 1);
if (lucky(pos) && lucky(cnum)) cnt++;
if (k == 1) {
for (int i = 0; i < cs; i++)
if (lucky(pos + i + 1) && lucky(v[i])) cnt++;
break;
}
cs--;
}
for (int i = 0; lnum[i] <= cp; i++) cnt++;
cout << cnt << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.fill;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.sort;
public class Main {
public static void main(String[] args) throws IOException {
new Thread(null, new Runnable() {
public void run() {
try {
try {
if (new File("input.txt").exists())
System.setIn(new FileInputStream("input.txt"));
} catch (SecurityException e) {}
long prevTime = System.currentTimeMillis();
new Main().run();
System.err.println("Total time: " + (System.currentTimeMillis() - prevTime) + " ms");
System.err.println("Memory status: " + memoryStatus());
} catch (IOException e) {
e.printStackTrace();
}
}
}, "1", 1L << 24).start();
}
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
in.close();
}
/***************************************************************
* Solution
**************************************************************/
final long INF = Long.MAX_VALUE / 7L;
int MAXL = 10000;
long[] ln = new long [MAXL];
int lsz = 0;
Set<Long> lucky = new HashSet<Long>();
int MAXN = 50;
int n;
int k;
int[] perm;
void solve() throws IOException {
gen(0, 0L);
ln = Arrays.copyOf(ln, lsz);
sort(ln);
for (long x : ln)
lucky.add(x);
int N = nextInt();
n = min(MAXN, N);
k = nextInt();
long[] fac = new long [n + 1];
fac[0] = 1L;
for (int i = 1; i <= n; i++) {
if (fac[i - 1] == INF) {
fac[i] = INF;
} else {
fac[i] = fac[i - 1] * i;
if (fac[i] > k)
fac[i] = INF;
}
}
if (fac[n] < k) {
out.println(-1);
return;
}
int R = N - n;
int ans = solveSimple(R);
TreeSet<Integer> set = new TreeSet<Integer>();
int add = N - n;
for (int i = 1; i <= n; i++)
set.add(add + i);
k--;
perm = new int [n];
for (int i = 0; i < n; i++) {
long f = fac[n - i - 1];
int cx = set.first();
int g = 0;
if (f != INF) {
for (int x : set) {
if (f * g > k)
break;
g++;
cx = x;
}
g--;
}
k -= g * f;
set.remove(cx);
perm[i] = cx;
}
for (int i = 0; i < n; i++) {
int index = N - n + i + 1;
int number = perm[i];
if (lucky(index) && lucky(number))
ans++;
}
out.println(ans);
}
void gen(int dn, long sum) {
if (dn == 10) return;
if (sum != 0L)
ln[lsz++] = sum;
gen(dn + 1, sum * 10L + 4L);
gen(dn + 1, sum * 10L + 7L);
}
boolean lucky(long x) {
return lucky.contains(x);
}
int solveSimple(long max) {
int ret = 0;
for (long x : ln)
if (x <= max)
ret++;
return ret;
}
/***************************************************************
* Input
**************************************************************/
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
int[] nextIntArray(int size) throws IOException {
int[] ret = new int [size];
for (int i = 0; i < size; i++)
ret[i] = nextInt();
return ret;
}
long[] nextLongArray(int size) throws IOException {
long[] ret = new long [size];
for (int i = 0; i < size; i++)
ret[i] = nextLong();
return ret;
}
double[] nextDoubleArray(int size) throws IOException {
double[] ret = new double [size];
for (int i = 0; i < size; i++)
ret[i] = nextDouble();
return ret;
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
/***************************************************************
* Output
**************************************************************/
void printRepeat(String s, int count) {
for (int i = 0; i < count; i++)
out.print(s);
}
void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i > 0) out.print(' ');
out.print(array[i]);
}
out.println();
}
void printArray(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i > 0) out.print(' ');
out.print(array[i]);
}
out.println();
}
void printArray(double[] array) {
for (int i = 0; i < array.length; i++) {
if (i > 0) out.print(' ');
out.print(array[i]);
}
out.println();
}
void printArray(double[] array, String spec) {
for (int i = 0; i < array.length; i++) {
if (i > 0) out.print(' ');
out.printf(Locale.US, spec, array[i]);
}
out.println();
}
void printArray(Object[] array) {
boolean blank = false;
for (Object x : array) {
if (blank) out.print(' '); else blank = true;
out.print(x);
}
out.println();
}
@SuppressWarnings("rawtypes")
void printCollection(Collection collection) {
boolean blank = false;
for (Object x : collection) {
if (blank) out.print(' '); else blank = true;
out.print(x);
}
out.println();
}
/***************************************************************
* Utility
**************************************************************/
static String memoryStatus() {
return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() >> 20) + "/" + (Runtime.getRuntime().totalMemory() >> 20) + " MB";
}
static void checkMemory() {
System.err.println(memoryStatus());
}
static long prevTimeStamp = Long.MIN_VALUE;
static void updateTimer() {
prevTimeStamp = System.currentTimeMillis();
}
static long elapsedTime() {
return (System.currentTimeMillis() - prevTimeStamp);
}
static void checkTimer() {
System.err.println(elapsedTime() + " ms");
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long Fac[14] = {1, 1, 2, 6, 24,
120, 720, 5040, 40320, 362880,
3628800, 39916800, 479001600, 6227020800LL};
int Total, Data[5000];
bool Lucky(int T) {
while (T) {
if (T % 10 != 4 && T % 10 != 7) return false;
T /= 10;
}
return true;
}
void Init() {
Total = 0;
for (int i = 1; i <= 9; i++)
for (int j = 0; j < (1 << i); j++) {
int S = 0;
for (int k = 0; k < i; k++) S = S * 10 + ((j & (1 << k)) ? 4 : 7);
Data[Total++] = S;
}
sort(Data, Data + Total);
}
int Count(int N) {
if (Data[Total - 1] <= N) return Total;
int P = 0;
while (Data[P] <= N) P++;
return P;
}
int Count1(int N, int K) {
if (K >= Fac[N]) return -1;
int Ans = 0, Kth[20], A[20];
for (int i = 1; i <= N; i++) Kth[i] = i;
for (int i = 1; i <= N; i++) {
A[i] = Kth[K / Fac[N - i] + 1];
K %= Fac[N - i];
if (Lucky(i) && Lucky(A[i])) Ans++;
int P = 1;
while (Kth[P] != A[i]) P++;
for (int j = P; j <= N - i; j++) Kth[j] = Kth[j + 1];
}
return Ans;
}
int Count2(int N, int K) {
int Ans = Count(N), Kth[20], A[20];
for (int i = 1; i <= 13; i++) Kth[i] = N + i;
for (int i = 1; i <= 13; i++) {
A[i] = Kth[K / Fac[13 - i] + 1];
K %= Fac[13 - i];
if (Lucky(N + i) && Lucky(A[i])) Ans++;
int P = 1;
while (Kth[P] != A[i]) P++;
for (int j = P; j <= 13 - i; j++) Kth[j] = Kth[j + 1];
}
return Ans;
}
int main() {
Init();
int N, K;
cin >> N >> K;
K--;
cout << ((N <= 13) ? Count1(N, K) : Count2(N - 13, K)) << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, k, len;
long long base[20];
int num[20];
bool mark[20];
int solve(int x) {
int ret = 0;
for (int L = 1; L <= 9; L++)
for (int i = 0; i < (1 << L); i++) {
int k = 0;
for (int j = 0; j < L; j++) k = k * 10 + ((i & (1 << j)) ? 4 : 7);
if (k <= x) ret++;
}
return ret;
}
bool lucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
scanf("%d%d", &n, &k);
int ans;
if (k == 1)
ans = solve(n);
else {
k--;
len = 2;
base[1] = base[2] = 1;
while (base[len] <= k) base[++len] = base[len - 1] * (len - 1);
len--;
if (n < len) {
printf("-1\n");
return 0;
}
ans = solve(n - len);
for (int i = 0; i < len; i++) {
int t = (k / base[len - i]) + 1;
k %= base[len - i];
int j = 0;
while (t--) {
while (mark[j]) j++;
if (t) j++;
}
mark[j] = true;
num[i] = j;
if (lucky(i + n - len + 1) && lucky(num[i] + n - len + 1)) ans++;
}
}
printf("%d\n", ans);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int getall(long long x, int n) {
if (x > n) return 0;
return 1 + getall(x * 10 + 4, n) + getall(x * 10 + 7, n);
}
bool is_good(int x) {
if (!x) return 0;
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
vector<int> kth(int n, int k) {
vector<long long> f(n);
f[0] = 1;
for (auto i = 1; i < n; i++) f[i] = i * f[i - 1];
vector<int> perm(n), u(n, 0);
for (auto i = 0; i < n; i++) {
for (auto j = 0; j < n; j++) {
if (u[j]) continue;
long long add = f[n - 1 - i];
if (add < k) {
k -= add;
continue;
}
perm[i] = j;
break;
}
u[perm[i]] = 1;
}
return perm;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
long long fac = 1;
int cnt;
for (auto i = 1; i < n + 1; i++) {
fac *= i;
cnt = i;
if (fac >= k) break;
}
if (fac < k) {
cout << -1;
return 0;
}
int tot = getall(0, n) - 1;
vector<int> perm = kth(cnt, k);
for (auto i = 0; i < cnt; i++) {
int pos = n - cnt + 1 + i, val = perm[i] + n - cnt + 1;
if (is_good(val) && !is_good(pos)) --tot;
}
cout << tot;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int Lucky[1024], size;
void dfs(int r, int val) {
if (val) Lucky[size++] = val;
if (r == 9) {
return;
}
dfs(r + 1, val * 10 + 4);
dfs(r + 1, val * 10 + 7);
}
bool isLucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int sum[13];
int res(int x, int p) {
if (x == 0) return p + 1;
int t = x / sum[p];
if (t * sum[p] == x) return t;
return t + 1;
}
void init() {
size = 0;
dfs(0, 0);
sort(Lucky, Lucky + size);
sum[0] = 1;
sum[1] = 1;
for (int i = 2; i <= 12; i++) sum[i] = sum[i - 1] * i;
}
vector<int>::iterator it;
int main() {
init();
int n, k, len, po, ans;
while (scanf("%d%d", &n, &k) != EOF) {
ans = 0;
bool flag = true;
for (len = 1; len <= 12; len++) {
if (sum[len] >= k) break;
}
po = n - len;
if (po < 0) {
flag = false;
printf("-1\n");
continue;
}
ans = upper_bound(Lucky, Lucky + size, po) - Lucky;
vector<int> ind;
for (int i = po + 1; i <= n; i++) {
ind.push_back(i);
}
len--;
for (int i = po + 1; i <= n; i++, len--) {
int tmp = k / sum[len];
it = ind.begin() + res(k, len) - 1;
k -= tmp * sum[len];
tmp = *it;
if (isLucky(tmp) && isLucky(i)) ans++;
ind.erase(it);
}
printf("%d\n", ans);
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
const int N = 100100;
int a[N], cnt, n, k;
long long F[111], sum;
const int IDX[] = {4, 7};
void DP(int id) {
if (sum > n) return;
if (sum) a[++cnt] = sum;
for (int i = 0; i < 2; i++) {
sum = sum * 10 + IDX[i];
DP(i + 1);
sum /= 10;
}
}
int MAX, ans;
bool OK[N];
int main() {
cin >> n >> k;
DP(1);
sort(a + 1, a + cnt + 1);
F[0] = 1;
for (int i = 1; i <= 20; i++) F[i] = F[i - 1] * i;
if (n <= 20 && k > F[n]) {
cout << "-1";
return 0;
}
for (int i = 1; i <= 20; i++)
if (F[i] >= k) {
MAX = i;
break;
}
memset(OK, true, sizeof(OK));
for (int i = 1; i <= MAX; i++) {
int j = 1;
for (j = 1; j <= MAX; j++)
if (OK[j])
if (k > F[MAX - i])
k -= F[MAX - i];
else
break;
OK[j] = false;
if (binary_search(a + 1, a + cnt + 1, n - MAX + i) &&
binary_search(a + 1, a + cnt + 1, n - MAX + j))
ans++;
}
for (int i = 1; i <= cnt; i++)
if (a[i] <= n - MAX)
ans++;
else
break;
cout << ans;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long int mul[1000];
int i, j, n, m, t, tt, k;
void prepare() {
mul[0] = 1;
i = 0;
while (mul[i] < 2000000000) {
mul[i + 1] = (i + 1) * mul[i];
i++;
}
tt = i;
}
void kper(int p[1000], int n, long long int k) {
bool checked[1000];
memset(checked, 0, sizeof(checked));
int i, j;
for (i = 1; i <= n; i++) {
j = 1;
while (1) {
while (checked[j]) j++;
if (mul[n - i] >= k) {
break;
}
k -= mul[n - i];
j++;
}
checked[j] = 1;
p[i] = j;
}
}
long long int p[20000];
void prepare2() {
p[1] = 4;
p[2] = 7;
;
t = 2;
j = 1;
int r;
for (i = 1; i <= 10; i++) {
r = t;
while (j <= r) {
p[++t] = p[j] * 10 + 4;
p[++t] = p[j] * 10 + 7;
j++;
}
}
sort(&p[1], &p[1 + t]);
}
bool lucky(long long int a) {
while (a) {
if (a % 10 != 4 && a % 10 != 7) return 0;
a /= 10;
}
return 1;
}
int q[1000], qq[1000], qqq[1000];
int main() {
prepare();
prepare2();
scanf("%d %d", &n, &k);
if (n <= tt && mul[n] < k) {
printf("-1\n");
return 0;
}
i = 1;
while (mul[i] < k) i++;
j = n - i;
if (j < 1) j = 1;
int a, b, c, ans = 0;
a = 1;
while (p[a] <= j) {
ans++;
a++;
}
kper(q, i, k);
for (j = n; j > n - i; j--) {
qq[j - (n - i)] = j;
}
for (j = 1; j <= i; j++) {
qqq[j] = qq[q[j]];
}
for (j = 1; j <= i; j++) {
if (lucky(qqq[j]) && lucky(j + (n - i))) {
ans++;
}
}
printf("%d\n", ans);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long nmax = 100;
vector<long long> lucky;
long long s_3(long long i) {
string s = "";
long long x = i;
while (x) {
s += char(x % 3);
x /= 3;
}
x = 0;
long long k = 1;
for (int j = 0; j < s.size(); j++) x += k * (long long)s[j], k *= 10;
return x;
}
void zap(int n) {
for (int i = 0; i <= n; i++) lucky.push_back(s_3(i));
}
bool islucky(long long x) {
for (; x; x /= 10)
if (x % 10 != 4 && x % 10 != 7) return false;
return true;
}
void prepare() {
for (int i = 1; i <= 9; ++i) {
for (int j = 0; j < (1 << i); ++j) {
int x = 0;
for (int t = i - 1; t >= 0; --t) {
x = x * 10 + ((j >> t) & 1 ? 7 : 4);
}
lucky.push_back(x);
}
}
}
int main() {
prepare();
long long n, m;
cin >> n >> m;
long long l = 0, s = 1;
while (s < m) s *= ++l;
if (n < l) {
cout << -1 << '\n';
return 0;
}
vector<long long> a;
long long ans =
upper_bound(lucky.begin(), lucky.end(), n - l) - lucky.begin();
for (int i = 1; i <= l; i++) a.push_back(n - l + i);
for (int i = 1; i <= l; i++) {
s /= l + 1 - i;
long long x = (m - 1) / s;
if (islucky(n - l + i) && islucky(a[x])) ans++;
a.erase(a.begin() + x);
m -= x * s;
}
cout << ans << endl;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int per[20];
int fac[] = {1, 1, 2, 6, 24, 120, 720,
5040, 40320, 362880, 3628800, 39916800, 479001600};
bool used[20];
int KT(int *s, int n) {
int sum = 0;
for (int i = 0; i < n; ++i) {
int cnt = 0;
for (int j = i + 1; j < n; ++j)
if (s[j] < s[i]) ++cnt;
sum += cnt * fac[n - i - 1];
}
return sum;
}
void invKT(int *s, int k, int n) {
k--;
for (int i = 0; i < n; ++i) {
int cnt = k / fac[n - i - 1];
for (int j = 0; j <= cnt; ++j) {
if (used[j]) ++cnt;
}
per[i] = cnt + 1;
used[cnt] = true;
k %= fac[n - i - 1];
}
}
vector<long long> lucky;
void dfs(string u) {
if (u.size() > 9) return;
if (u.size()) lucky.push_back(atoi(u.c_str()));
dfs(u + "4");
dfs(u + "7");
}
void init() {
dfs("");
lucky.push_back(4444444444LL);
sort(lucky.begin(), lucky.end());
}
int n, k;
int main() {
init();
while (cin >> n >> k) {
if (n < 13 && k > fac[n]) {
cout << "-1\n";
continue;
}
int change = 13;
for (int i = 1; i < 13; ++i)
if (k < fac[i]) {
change = i;
break;
}
invKT(per, k, change);
int lef = n - change;
int ans = 0;
for (int i = 0; i < lucky.size(); ++i) {
if (lucky[i] > n) {
break;
}
if (lucky[i] <= lef)
ans++;
else {
int num = per[lucky[i] - lef - 1] + lef;
for (int j = 0; j < lucky.size() && lucky[j] <= n; j++) {
if (num == lucky[j]) ans++;
}
}
}
cout << ans << '\n';
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 20;
const int K = 16;
long long f[N];
vector<long long> get_per(long long n, long long k) {
f[0] = 1;
for (int i = 1; N > i; i++) {
f[i] = f[i - 1] * i;
}
vector<long long> ret;
ret.resize(n);
set<int> st;
for (int i = 1; n >= i; i++) {
st.insert(i);
}
for (int i = 0; n > i; i++) {
int times = 0;
auto now = st.begin();
while (f[n - i - 1] < k) {
k -= f[n - i - 1];
now++;
}
ret[i] = (*now);
st.erase(now);
}
return ret;
}
int main() {
vector<long long> v;
for (int sz = 1; 9 >= sz; sz++) {
for (int i = 0; (1 << sz) > i; i++) {
long long now = 0;
for (int j = 0; sz > j; j++) {
if (((1 << j) & i) != 0) {
now *= 10;
now += 4;
} else {
now *= 10;
now += 7;
}
}
v.push_back(now);
}
}
v.push_back(4444444444LL);
sort(v.begin(), v.end());
int n, k;
cin >> n >> k;
f[0] = 1;
for (int i = 1; N > i; i++) f[i] = f[i - 1] * i;
if (n <= K && f[n] < k) {
puts("-1");
return 0;
}
if (n <= K) {
vector<long long> ret = get_per(n, k);
long long ans = 0;
for (int i = 0; n > i; i++) {
if (i + 1 == 4 || i + 1 == 7) {
if (ret[i] == 4 || ret[i] == 7) {
ans++;
}
}
}
cout << ans << endl;
return 0;
}
long long tmp = n - K;
long long now = n - K;
auto iter = lower_bound(v.begin(), v.end(), tmp);
if ((*iter) != now) --iter;
long long ans = iter - v.begin() + 1;
set<long long> lucky;
for (long long i : v) lucky.insert(i);
vector<long long> ret = get_per(K, k);
for (int i = 0; K > i; i++) {
int pos = i + now + 1;
if (lucky.find(pos) != lucky.end()) {
long long val = ret[i] + now;
if (lucky.find(val) != lucky.end()) ans++;
}
}
cout << ans << endl;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long f[20];
vector<long long> lu;
void go(int x, int y, long long p) {
if (x == y) return;
lu.push_back(p * 10 + 4);
lu.push_back(p * 10 + 7);
go(x, y + 1, p * 10 + 4);
go(x, y + 1, p * 10 + 7);
}
bool ok(long long x) {
if (!x) return 0;
while (x) {
if (!(x % 10 == 4 || x % 10 == 7)) return 0;
x /= 10;
}
return 1;
}
void radix(long long k, vector<long long> &num) {
k--;
int len = num.size();
int aw, get;
for (get = 1, aw = len - 1; get < len; get++, aw--)
if (f[get] > k) break;
get--;
while (k) {
long long a = k / f[get];
k = k % f[get];
swap(num[aw], num[aw + a]);
sort(num.begin() + aw + 1, num.end());
aw++;
get--;
}
}
int main() {
go(13, 0, 0);
sort(lu.begin(), lu.end());
int p = lu.size();
f[0] = 1;
for (long long i = 1; i < 15; i++) f[i] = f[i - 1] * i;
long long n, k;
vector<long long> num;
while (cin >> n >> k) {
long long ans = 0;
if (n > 13) {
for (long long i = 12; i >= 0; i--) num.push_back(n - i);
for (int i = 0; i < p; i++) {
if (lu[i] <= n - 13) ans++;
}
radix(k, num);
for (long long i = 12, j = 0; i >= 0; i--, j++) {
if (ok(num[j]) && ok(n - i)) ans++;
}
} else if (f[n] >= k) {
for (int i = 0; i < n; i++) num.push_back(i + 1);
radix(k, num);
for (int i = 0; i < n; i++) {
if (ok(num[i]) && (ok(i + 1))) ans++;
}
}
if (n <= 13)
if (f[n] < k) ans = -1;
cout << ans << endl;
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int permutation[100];
long long fact[100];
void writeFact(int n) {
fact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;
}
void kthPermutation(int n, int i) {
i--;
int j, k = 0;
for (k = 0; k < n; ++k) {
permutation[k] = i / fact[n - 1 - k];
i = i % fact[n - 1 - k];
}
for (k = n - 1; k > 0; k--)
for (j = k - 1; j >= 0; j--)
if (permutation[j] <= permutation[k]) permutation[k]++;
for (i = 0; i < n; i++) permutation[i]++;
}
long long lucky[100000];
void findLucky(int n) {
lucky[0] = 4;
lucky[1] = 7;
int i = 0, now = 2;
while (lucky[i] < n) {
lucky[now] = lucky[i] * 10 + 4;
now++;
lucky[now] = lucky[i] * 10 + 7;
now++;
i++;
}
}
int aksFact(int k) {
int i = 0;
while (k > fact[i]) i++;
return i;
}
int sumLucky(int n) {
int i = 0;
while (lucky[i] <= n) i++;
return i;
}
bool isLucky(int n, int m) {
while (n > 0) {
if (n % 10 != 4 && n % 10 != 7) return false;
n /= 10;
}
while (m > 0) {
if (m % 10 != 4 && m % 10 != 7) return false;
m /= 10;
}
return true;
}
int main() {
writeFact(15);
int n, k;
cin >> n >> k;
if (n < 14 && k > fact[n]) {
cout << -1 << endl;
return 0;
}
findLucky(n + 100);
int a = aksFact(k);
int ans = sumLucky(n - a);
kthPermutation(a, k);
for (int i = 0; i < a; i++) {
permutation[i] += n - a;
ans += isLucky(permutation[i], n - a + i + 1);
}
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
const int N = 100010;
const int inf = 0x3f3f3f3f;
using namespace std;
long long dp[N];
int used[21];
bool vis[21];
void dfs(int n, long long k, int cnt) {
if (n == 0) return;
int id = 1;
for (int i = 1; i < n; i++)
if (k > dp[n - 1])
k -= dp[n - 1], id = i + 1;
else
break;
for (int i = 1, j = 0; i < 20; i++) {
if (vis[i]) continue;
j++;
if (j == id) vis[i] = 1, used[cnt] = i;
if (j == id) break;
}
dfs(n - 1, k, cnt + 1);
}
int main() {
long long n, k;
cin >> n >> k;
vector<long long> vt;
set<long long> st;
for (int k = 1; k < 11; k++) {
for (long long i = 0; i < 1ll << k; i++) {
long long cnt = 0;
for (int j = 0; j < k; j++)
if (i & (1ll << j))
cnt = cnt * 10 + 7;
else
cnt = cnt * 10 + 4;
vt.push_back(cnt);
st.insert(cnt);
}
}
sort(vt.begin(), vt.end());
dp[0] = 1;
for (int i = 1; i < 20; i++) dp[i] = i * dp[i - 1];
if (n <= 19 && dp[n] < k) {
puts("-1");
return 0;
}
dfs(min(n, 19ll), k, 1);
long long ret = 0;
if (n <= 19) {
ret = 0;
if (used[4] == 4 || used[4] == 7) ret++;
if (used[7] == 7 || used[7] == 4) ret++;
} else {
ret = vt.size() - (vt.end() - upper_bound(vt.begin(), vt.end(), n - 19));
for (int i = 1; i <= 19; i++)
if (st.count(used[i] + n - 19) && st.count(n - 19 + i)) ret++;
}
cout << ret << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int islucky(long long int a) {
long long int temp = a;
if (temp == 0) return 0;
while (temp != 0) {
if (temp % 10 != 4 && temp % 10 != 7) return 0;
temp /= 10;
}
return 1;
}
int main() {
long long int v[10010];
memset(v, 0, sizeof(v));
v[0] = 4;
v[1] = 7;
for (int end = 2, cnt = 0; end <= 1500; cnt++) {
v[end++] = v[cnt] * 10 + 4;
v[end++] = v[cnt] * 10 + 7;
}
long long int facto[15];
long long int n, k;
facto[0] = 1;
for (int i = 1; i < 15; i++) facto[i] = facto[i - 1] * i;
cin >> n >> k;
k -= 1;
long long int to_be_changed = 0;
if (n <= 15)
if (k >= facto[n]) {
cout << -1 << endl;
return 0;
}
for (to_be_changed = 1; to_be_changed <= 15; to_be_changed++)
if (facto[to_be_changed] > k) break;
long long int a[100];
for (int l = 0; l < to_be_changed; l++) {
a[l] = n - to_be_changed + l + 1;
}
long long int sum = 0;
for (int i = 0; i <= 1500; i++) {
if (v[i] >= (n - to_be_changed + 1)) break;
sum += 1;
}
long long int size = to_be_changed;
long long int perm_no = k;
long long int r = perm_no;
long long int m = 0;
long long int position = 0;
sort(a, a + size);
while (position <= size - 1) {
m = r / facto[size - 1 - position];
r = r % facto[size - 1 - position];
long long int t = *(a + position + m);
*(a + position + m) = *(a + position);
*(a + position) = t;
sort(a + position + 1, a + size);
position++;
if (r == 0) break;
}
for (int i = 0; i < to_be_changed; i++) {
sum += (islucky(a[i]) && islucky(n - size + i + 1));
}
cout << sum << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
static const int INF = 500000000;
template <class T>
void debug(T a, T b) {
for (; a != b; ++a) cerr << *a << ' ';
cerr << endl;
}
long long int luck[100005];
int cnt = 0;
long long int n, k;
long long int fact[20];
int used[20];
bool isLucky(long long int a) {
int res = 1;
while (a > 0) {
if (a % 10 != 4 && a % 10 != 7) res = 0;
a /= 10;
}
return res;
}
int main() {
fact[0] = 1;
for (int i = 0; i < 19; ++i) fact[i + 1] = fact[i] * (i + 1);
for (int i = 1; i <= 10; ++i) {
for (int j = 0; j < 1 << i; ++j) {
long long int num = 0;
for (int k = 0; k < i; ++k) num = num * 10 + (j >> k & 1 ? 7 : 4);
--num;
luck[cnt++] = num;
}
}
sort(luck, luck + cnt);
cin >> n >> k;
--k;
int res = 0;
int end = n - 1 - 13;
vector<int> srch;
for (int i = 0; i < cnt; ++i) {
if (luck[i] <= end) {
++res;
} else if (luck[i] >= n)
break;
else {
srch.push_back(luck[i]);
}
}
vector<long long int> perm;
int size = min(13ll, n);
for (int i = 0; i < size; ++i) {
int seek = 0;
while (seek < size && used[seek]) ++seek;
while (k >= fact[size - 1 - i]) {
k -= fact[size - 1 - i];
++seek;
while (seek < size && used[seek]) ++seek;
if (seek == size) {
puts("-1");
return 0;
}
}
used[seek] = 1;
perm.push_back(seek);
}
if (end < 0) {
for (int i = 0; i < srch.size(); ++i)
if (isLucky(perm[srch[i]] + 1)) ++res;
} else {
for (int i = 0; i < srch.size(); ++i) {
if (isLucky(perm[srch[i] - end - 1] + 1 + end + 1)) ++res;
}
}
cout << res << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | from itertools import *
n, k = map(int, raw_input().split())
MAXK = 1000000000
LNUMS = []
lc = [ [0], [] ]
for i in range(10):
idx = i%2
lc[1-idx] = []
for v in lc[idx]:
lc[1-idx] += [v*10+4, v*10+7]
LNUMS += lc[1-idx]
FAC = [1]
j = 1
d = 1
while d <= MAXK:
j += 1
d *= j
FAC.append(d)
visit = [0]*20
c = n
def lucky(i):
while i:
if i % 10 not in [4, 7]:
return False
i //= 10
return True
nn = max(0, n-len(FAC))
def nextn(i):
d = nn
while i:
d += 1
if visit[d-nn] == 0:
i -= 1
visit[d-nn] = 1
return d
if n < len(FAC) and FAC[n-1] < k:
ans = -1
else:
k -= 1
ans = len(list(takewhile(lambda x: x <= nn, LNUMS)))
for i in range(nn+1, n+1):
ii = n-i
if ii >= len(FAC):
nv = i
else:
di = FAC[ii-1] # (ii-1) could be -1
nv = nextn(k//di+1)
k %= di
if lucky(i) and lucky(nv):
ans += 1
print ans
| PYTHON |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
for (int i = 0; i < (1 << 9); i++) {
int k = i, n = 0, j = 0;
while (k > 0) {
if (k % 2 == 1)
n = (n * 10) + 7;
else
n = (n * 10) + 4;
k /= 2;
j++;
}
if (n != 0) v.push_back(n);
for (; j < 9; j++) {
n = (n * 10) + 4;
v.push_back(n);
}
}
sort(v.begin(), v.end());
long long p = 1LL;
int n, i, j;
long long k;
scanf("%d %lld", &n, &k);
for (i = 1; i <= n; i++) {
p = p * (long long)i;
if (p >= k) break;
}
if (i == n + 1) {
cout << "-1";
return 0;
}
p = p / i;
vector<int> second;
j = i;
for (i = n - i + 1; i <= n; i++) second.push_back(i);
int ans = 0;
int y = j;
for (i = n - j + 1; i <= n; i++) {
int m = k / p;
if (k % p != 0) m++;
m--;
int first = 0, t;
for (t = 0; t < v.size(); t++)
if (v[t] == i) first++;
for (t = 0; t < v.size(); t++)
if (v[t] == second[m]) first++;
if (first == 2) ans++;
second.erase(second.begin() + m);
if (k % p == 0)
k = p;
else
k = k % p;
j--;
if (i != n) p = p / j;
}
ans = ans + upper_bound(v.begin(), v.end(), n - y) - v.begin();
cout << ans;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<int> a;
int aa[20];
void search(int x) {
if (x != 0) a.push_back(x);
if (x > 100000000) return;
search(x * 10 + 4);
search(x * 10 + 7);
}
int islucky(int x) {
char tmp[20];
int i, l;
sprintf(tmp, "%d", x);
l = strlen(tmp);
for (i = 0; i < l; i++)
if ((tmp[i] != '4') && (tmp[i] != '7')) return 0;
return 1;
}
int main() {
int st, cnt, n, k, i, j, ans;
int used[20];
long long tmp;
a.clear();
search(0);
sort(a.begin(), a.end());
scanf("%d%d", &n, &k);
tmp = 1;
for (i = 1;; i++) {
tmp *= i;
if (tmp >= k) break;
}
if (n < i) {
printf("-1\n");
return 0;
}
ans = 0;
st = n - i;
cnt = i;
for (i = 0; i < a.size(); i++)
if (a[i] <= st) ans++;
st++;
memset(used, 0, sizeof(used));
for (i = 0; i < cnt; i++) {
tmp /= (cnt - i);
for (j = 0; j < cnt; j++)
if (used[j] == 0) {
if (k <= tmp) {
used[j] = 1;
break;
}
k -= tmp;
}
if ((islucky(st + i) == 1) && (islucky(j + st) == 1)) {
ans++;
}
}
printf("%d\n", ans);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
bool lucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) break;
x /= 10;
}
if (x == 0)
return true;
else
return false;
}
int fact(int n) {
int x = 1, i;
for (i = 1; i <= n; i++) x *= i;
return x;
}
int a[10000], b[100];
int main() {
int n, k, p = 0, q = 2, r = 2, sum = 0, i, j;
long long x = 1;
scanf("%d %d", &n, &k);
a[0] = 4;
a[1] = 7;
for (i = 0; i < 8; i++) {
for (j = p; j < q; j++) {
a[r++] = a[j] * 10 + 4;
a[r++] = a[j] * 10 + 7;
}
p = q;
q = r;
}
for (i = 1; i <= n; i++) {
x *= i;
if (x >= k) break;
}
if (i > n) {
puts("-1");
return 0;
}
for (j = 0; j < r; j++) {
if (a[j] > n - i) break;
sum++;
}
q = 0;
for (j = n - i + 1; j <= n; j++) b[q++] = j;
for (j = n - i + 1; j < n; j++) {
p = k / fact(n - j);
if (k % fact(n - j) == 0) p--;
if (lucky(j) == 1 && lucky(b[p]) == 1) sum++;
for (i = p; i < q; i++) b[i] = b[i + 1];
k -= p * fact(n - j);
}
if (lucky(n) == 1 && lucky(b[0]) == 1) sum++;
printf("%d\n", sum);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline T pow2(T a) {
return a * a;
}
template <class T>
inline bool mineq(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
vector<long long> luck;
long long fact[20];
bool islucky(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7) return false;
n /= 10;
}
return true;
}
long long asf(long long n, long long k, long long pref) {
bool used[20] = {};
long long ans = 0;
if (fact[n] < k) return -1;
for (int i = 0; i < n; i++) {
for (int j = 1; j < n + 1; j++)
if (!used[j]) {
if (fact[n - i - 1] < k) {
k -= fact[n - i - 1];
} else {
used[j] = true;
if (islucky(pref + i + 1) && islucky(pref + j)) ans++;
break;
}
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
luck.push_back(4);
luck.push_back(7);
int m = 0;
for (int i = 0; i < 10; i++) {
int n = luck.size();
for (int j = m; j < n; j++) {
luck.push_back(luck[j] * 10 + 4);
luck.push_back(luck[j] * 10 + 7);
}
m = n;
}
sort(luck.begin(), luck.end());
fact[0] = 1;
for (int i = 1; i < 16; i++) fact[i] = i * fact[i - 1];
long long n, k;
cin >> n >> k;
if (n <= 15) {
cout << asf(n, k, 0);
} else {
long long t = asf(15, k, n - 15);
if (t != -1) {
int k = upper_bound(luck.begin(), luck.end(), (n - 15)) - luck.begin();
cout << t + k;
} else
cout << t;
}
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1E-7;
long long lt[10000], tot, fa[15];
long long n, m;
int b[20];
void work(int x, long long w, int dp) {
if (x == dp) {
lt[tot++] = w;
return;
}
work(x + 1, w * 10 + 4, dp);
work(x + 1, w * 10 + 7, dp);
}
bool lucky(long long u) {
if (u == 0) return false;
while (u > 0) {
if (u % 10 != 4 && u % 10 != 7) return false;
u /= 10;
}
return true;
}
int main() {
tot = 0;
for (int i = 1; i < 11; ++i) work(0, 0, i);
fa[0] = 1;
for (int i = 1; i < 15; ++i) fa[i] = fa[i - 1] * i;
cin >> n >> m;
long long ans = 0;
if (n < 14 && m > fa[n]) {
cout << -1 << endl;
return 0;
}
int l;
--m;
for (l = 14; fa[l] > m; --l)
;
for (int i = 0; lt[i] < n - l; ++i, ++ans)
;
for (int k = l + 1; k > 0; --k) {
int t = m / fa[k - 1];
int cur = 0;
while (b[cur]) ++cur;
for (int i = 0; i < t; ++i) do {
++cur;
} while (b[cur]);
b[cur] = 1;
if (lucky(n - k + 1) && lucky(cur + n - l)) ++ans;
m %= fa[k - 1];
}
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<long long> tail;
vector<long long> used;
long long n, k;
bool isGood(long long x) {
while (x > 0) {
if (x % 10 != 7 && x % 10 != 4) return false;
x /= 10;
}
return true;
}
vector<long long> v;
int main() {
long long i, j;
long long x;
long long to = 4444444444LL;
v.push_back(to);
long long bits;
long long tmp;
for ((bits) = (1); (bits) < (10); ++(bits))
for ((i) = (0); (i) < (1 << bits); ++(i)) {
j = i;
x = 0;
for ((tmp) = (0); (tmp) < (bits); ++(tmp)) {
if (j & 1)
x = x * 10 + 7;
else
x = x * 10 + 4;
j >>= 1;
}
v.push_back((long long)x);
}
sort(v.begin(), v.end());
cin >> n;
cin >> k;
x = 1;
long long fact = 1;
while (fact < k) {
++x;
fact *= x;
}
if (x > n) {
puts("-1");
return 0;
}
--k;
fact /= x;
--x;
tail.push_back(k / fact);
used.push_back(k / fact);
k %= fact;
long long top;
while (x > 0) {
fact /= x;
--x;
top = k / fact;
for (i = 0; i < used.size(); ++i)
if (used[i] <= top) {
++top;
} else
break;
tail.push_back(top);
used.push_back(top);
sort(used.begin(), used.end());
k %= fact;
}
long long ts = tail.size();
for (i = 0; i < tail.size(); ++i) tail[i] = n + tail[i] - tail.size() + 1;
long long ans = 0;
for ((i) = (0); (i) < (v.size()); ++(i))
if (n - ts < v[i]) break;
ans = i;
long long fr = n - ts + 1;
for (i = 0; i < ts; ++i)
if (isGood(i + fr) && isGood(tail[i])) ++ans;
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int Len = 2333333;
char buf[Len], *p1 = buf, *p2 = buf, duf[Len], *q1 = duf;
inline char gc();
inline int rd();
inline void pc(char c);
inline void rt(int x);
inline void flush();
int n, k, t, K, ans, f[15], p[15];
vector<int> V;
inline int check(int x) {
while (x)
if (x % 10 != 4 && x % 10 != 7)
return 0;
else
x /= 10;
return 1;
}
inline void Dfs(int x, int d) {
if (x > 9 || d > n) return;
if (x)
if (d < n - t)
++ans;
else if (check(p[n - d]))
++ans;
Dfs(x + 1, d * 10 + 4), Dfs(x + 1, d * 10 + 7);
}
int main() {
n = rd(), K = k = rd(), f[0] = 1;
for (register int i = 1; i <= 12; ++i) f[i] = f[i - 1] * i;
if (n < 13 && k > f[n]) return printf("-1"), 0;
while (k >= f[t] && t < 13) ++t;
--t, --k;
for (register int i = n - t; i <= n; ++i) V.push_back(i);
for (register int i = t; i >= 0; --i) {
int a = k / f[i], b = k % f[i];
k = b, p[i] = V[a], V.erase(V.begin() + a);
}
Dfs(0, 0), rt(ans);
return flush(), 0;
}
inline char gc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Len, stdin), p1 == p2)
? -1
: *p1++;
}
inline int rd() {
char c;
int f = 1;
while (!isdigit(c = gc()) && c != '-')
;
c == '-' ? f = -1, c = gc() : 0;
int x = c ^ 48;
while (isdigit(c = gc())) x = ((x + (x << 2)) << 1) + (c ^ 48);
return x * f;
}
inline void pc(char c) {
q1 == duf + Len &&fwrite(q1 = duf, 1, Len, stdout), *q1++ = c;
}
inline void rt(int x) {
x < 0 ? pc('-'), x = -x : 0, pc((x >= 10 ? rt(x / 10), x % 10 : x) + 48);
}
inline void flush() { fwrite(duf, 1, q1 - duf, stdout); }
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ProblemC {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
String[] nk = in.readLine().split(" ");
long n = Long.valueOf(nk[0]);
long k = Long.valueOf(nk[1]);
long perm = 1;
int l = 0;
while (perm < k) {
l++;
perm *= l;
}
if (l > n) {
out.println(-1);
out.flush();
return;
}
out.println(solveA(n - l) + solveB(n - l + 1, n, k-1));
out.flush();
}
private static long solveB(long f, long t, long k) {
if (f > t) {
return 0;
}
long luckluck = 0;
long d = (t - f) + 1;
List<Long> remain = new ArrayList<Long>();
for (long x = f ; x <= t ; x++) {
remain.add(x);
}
long index = f;
while (index <= t) {
long perm = 1;
for (long p = 1 ; p < d ; p++) {
perm *= p;
}
int canHave = (int)(k / perm);
k -= canHave * perm;
long selected = remain.remove(canHave);
if (islucky(index) && islucky(selected)) {
luckluck++;
}
index++;
d--;
}
return luckluck;
}
static boolean islucky(long num) {
return String.valueOf(num).replaceAll("4", "").replaceAll("7", "").length() == 0;
}
static List<Long> lucky;
public static int dfs(long max, long x) {
if (x > max) {
return 0;
}
return 1 + dfs(max, x*10+4) + dfs(max, x*10+7);
}
private static int solveA(long l) {
return dfs(l, 4) + dfs(l, 7);
}
public static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
void JIZZ() {
cout << "";
exit(0);
}
const long double PI = 3.14159265358979323846264338327950288;
const long double eps = 1e-13;
const long long mod = 1e9 + 7;
vector<long long> luc;
void init() {
for (int dg = 1; dg <= 10; ++dg) {
for (int i = 0; i < (1 << dg); ++i) {
long long x = 0;
for (int j = dg - 1; j >= 0; --j) {
if (i & (1 << j)) {
x = x * 10 + 7;
} else
x = x * 10 + 4;
}
luc.push_back(x);
}
}
}
long long fac[15], a[15], ori[15];
int main() {
init();
ios_base::sync_with_stdio(0);
cin.tie(0);
fac[0] = 1;
for (int i = 1; i <= 13; ++i) {
fac[i] = fac[i - 1] * i;
}
int n, k;
cin >> n >> k;
if (n <= 13 && k > fac[n]) return cout << "-1" << endl, 0;
if (k == 1) {
auto it = upper_bound(luc.begin(), luc.end(), n);
cout << it - luc.begin() << endl;
exit(0);
}
int move = 1;
while (fac[move] < k) ++move;
;
;
int fix = n - move;
int ans = upper_bound(luc.begin(), luc.end(), fix) - luc.begin();
;
;
vector<long long> candi;
for (int i = fix + 1, ptr = 0; i <= n; ++i, ++ptr)
ori[ptr] = i, candi.push_back(i);
;
;
--k;
for (int i = move, ptr = 0; i > 0; --i, ++ptr) {
int it = k / fac[i - 1];
;
;
a[ptr] = candi[it];
candi.erase(candi.begin() + it);
;
;
k %= fac[i - 1];
}
for (int i = 0; i < move; ++i)
if (*lower_bound(luc.begin(), luc.end(), a[i]) == a[i] &&
*lower_bound(luc.begin(), luc.end(), fix + i + 1) == fix + i + 1)
++ans;
cout << ans << endl;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1E9 + 7, INF = 2E18 + 5;
const double PI = 2 * acos(0.0);
const long double EPS = 1.0E-14;
long long arrFact[15];
vector<long long> vecInit, vec;
long long n, k, ans;
void make_Permutation() {
vec.push_back(0);
long long ind, per, sz, len;
len = vecInit.size();
for (int i = 0; i < len; i++) {
ind = 0, per = vecInit.size() - 1, sz = vecInit.size();
while (k > arrFact[per]) {
k -= arrFact[per];
ind++;
ind %= sz;
}
vec.push_back(vecInit[ind]);
vecInit.erase(vecInit.begin() + ind);
}
return;
}
bool isLucky(long long num) {
long long val;
while (num > 0) {
val = num % 10;
num /= 10;
if (val != 4 and val != 7) {
return 0;
}
}
return 1;
}
void luckyNumber(long long lim, long long num) {
if (num > lim) return;
ans++;
luckyNumber(lim, num * 10 + 4);
luckyNumber(lim, num * 10 + 7);
return;
}
int main() {
arrFact[0] = arrFact[1] = 1;
for (int i = 2; i <= 14; i++) {
arrFact[i] = i * arrFact[i - 1];
}
ans = 0;
long long ind;
cin >> n >> k;
if (n >= 13) {
luckyNumber(n - 13, 0);
ans--;
for (int i = n - 13 + 1; i <= n; i++) {
vecInit.push_back(i);
}
make_Permutation();
ind = n - 13;
for (int i = 1; i < vec.size(); i++) {
if (isLucky(i + ind) and isLucky(vec[i])) {
ans++;
}
}
cout << ans;
} else {
if (arrFact[n] < k)
cout << "-1\n";
else {
for (int i = 1; i <= n; i++) {
vecInit.push_back(i);
}
make_Permutation();
for (int i = 1; i < vec.size(); i++) {
if (isLucky(i) and isLucky(vec[i])) {
ans++;
}
}
cout << ans;
}
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline bool isgood(int a) {
while (a)
if ((a % 10 != 4) && (a % 10 != 7))
return false;
else
a /= 10;
return true;
}
inline int countgood(int r) {
int ans = 0, b[10];
long long a = 0;
for (int i = 0; i < 10; i++) b[i] = 0;
for (;;) {
int i = 0;
while (b[i] == 7) {
b[i] = 4;
i++;
}
b[i] = (b[i] == 0) ? 4 : 7;
for (i = 9, a = 0; i >= 0; i--) a = a * 10 + b[i];
if (a > r) break;
ans++;
}
return ans;
}
int f[14], v[14], t[14];
int main() {
f[0] = 1;
for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * i;
int n, k;
cin >> n >> k;
k--;
if (n <= 13) {
if (f[n] <= k) {
cout << -1;
return 0;
}
for (int i = 0; i < n; i++) v[i] = i + 1;
for (int i = n; i; i--) {
int j = k / f[i - 1];
k %= f[i - 1];
t[n - i + 1] = v[j];
for (; j < i - 1; j++) v[j] = v[j + 1];
}
int ans = 0;
for (int i = 1; i <= n; i++)
if (isgood(i) && isgood(t[i])) ans++;
cout << ans;
return 0;
}
for (int i = 0; i < 13; i++) v[i] = n - 12 + i;
for (int i = 13; i; i--) {
int j = k / f[i - 1];
k %= f[i - 1];
t[14 - i] = v[j];
for (; j < i - 1; j++) v[j] = v[j + 1];
}
int ans = 0;
for (int i = 1; i <= 13; i++)
if (isgood(n - 13 + i) && isgood(t[i])) ans++;
ans += countgood(n - 13);
cout << ans;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
set<long long> Luckys, adads;
long long n, k, fact = 1, f = 1, facts[100], ans;
bool mark[100];
vector<int> Permutation;
bool isLucky(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7) return 0;
n /= 10;
}
return 1;
}
void Lucky_Make(long long n, long long MAX) {
if (n > MAX) return;
Luckys.insert(n);
Lucky_Make(n * 10 + 4, MAX);
Lucky_Make(n * 10 + 7, MAX);
}
void Calc_Permutation(int n, int k) {
int ans = 0;
for (int i = 1; i <= n; i++) adads.insert(i);
for (int i = 1; i <= n; i++) {
int now = k / facts[n - i], j;
k %= facts[n - i];
set<long long>::iterator it = adads.begin();
for (j = 0; j < now; j++) it++;
Permutation.push_back(*it);
adads.erase(it);
}
}
int Count(int f) {
int ans = 0;
for (int i = 0; i < Permutation.size(); i++)
if (isLucky(Permutation[i] + n - f) && isLucky(i + 1 + n - f)) ans++;
return ans;
}
int main() {
cin >> n >> k;
facts[0] = 1;
for (; fact < k; f++) {
fact *= f;
facts[f] = fact;
}
f--;
if (f > n) {
cout << -1 << endl;
return 0;
}
Lucky_Make(4, n - f);
Lucky_Make(7, n - f);
ans = Luckys.size();
Calc_Permutation(f, k - 1);
ans += Count(f);
cout << ans << endl;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-6;
const double PI = acos(-1.0);
const int oo = 0x3f3f3f3f;
long long read() {
long long d, f = 1;
char c;
while (!isdigit(c = getchar()))
if (c == '-') f = -1;
d = c ^ '0';
while (isdigit(c = getchar())) d = (d * 10) + (c ^ '0');
return d * f;
}
vector<long long> happy, fac;
void dfs(long long num) {
if (num > int(1e9)) return;
happy.push_back(num);
dfs(num * 10 + 4);
dfs(num * 10 + 7);
}
void init() {
fac.push_back(1);
int i = 1;
while (fac[i - 1] * i <= int(1e9)) fac.push_back(fac[i - 1] * i), ++i;
dfs(4);
dfs(7);
sort(happy.begin(), happy.end());
}
int main() {
init();
int n, k;
while (cin >> n >> k) {
if (n < fac.size() && k > fac[n])
puts("-1");
else {
--k;
int len = upper_bound(fac.begin(), fac.end(), k) - fac.begin();
vector<int> prem(len + 1);
bool flag[20] = {0};
for (int i = 1; i <= len; ++i) {
int rank = -1;
for (int j = 1; j <= len; ++j)
if (!flag[j]) {
++rank;
if (rank == k / fac[len - i]) {
prem[i] = j + n - len;
flag[j] = 1;
k %= fac[len - i];
break;
}
}
}
int cnt1 = 0;
for (int i = 1; i <= len; ++i)
if (binary_search(happy.begin(), happy.end(), n - len + i) &&
!binary_search(happy.begin(), happy.end(), prem[i]))
++cnt1;
int cnt2 = upper_bound(happy.begin(), happy.end(), n) - happy.begin();
cout << cnt2 - cnt1 << endl;
}
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.BinaryRefAddr;
public class Main
{
static class KPermutation {
//elementos del arreglo del cual buscaremos cual es la k-esima permutacion
long[] elem;
int n;//cantidad de elemntos del arreglo
long[] fact;
public KPermutation(long[] elem)
{
this.elem=elem;
n=elem.length;
fact=new long[n+1];
fact[0]=1;
for(int i=1;i<n+1;i++)
fact[i]=i*fact[i-1];
}
/*
* Devuelve la k-esima permutacion de elem
* ordenada lexicograficamente
*
* Nota:Las permutaciones se enumeran a partir del cero
* */
public long[] GetKPermutation(long k)
{
long[] ans=new long[n];
//marcar posiciones ocupadas y elementos ya posicionados
boolean[] mark=new boolean[n];
//locallizando la base actorial de k
for(int i=n;i>=1;i--)
{
if(k>=fact[i-1])
{
int coc=(int)(k/fact[i-1]);
int count=0;
for(int j=0;j<n;j++)
{
if(!mark[j])
{
if(count==coc)
{
ans[n-i]=elem[j];
mark[j]=true;
break;
}
else count++;
}
}
//finding the coc no marked element
//mark[n-i+coc]=true;
k-=fact[i-1]*coc;
}
else //finding the minimun no marked element
{
for(int j=0;j<n;j++)
{
if(!mark[j])
{
ans[n-i]=elem[j];
mark[j]=true;
break;
}
}
}
}
return ans;
}
}
//testing
static long[] lucky=new long[1022];
static int tot=0;
public static void Dfs(long x,int th)
{
if(x>0){lucky[tot]=x;
tot++;
}
if(th>0)
{
Dfs(10*x+4,th-1);
Dfs(10*x+7,th-1);
}
}
static boolean IsLucky(long x) {
while(x>0) {
long y = x%10;
if (y!=4 && y!=7) return false;
x /= 10;
}
return true;
}
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
PrintStream out=System.out;
//ver en que orden se hace esto
int n=in.nextInt();
int k=in.nextInt();
k--;//NOsotros cotamos las permutaciones a partir del 0 y ellos del 1
Dfs(0,9);//calculando los numeros lucky
Arrays.sort(lucky,0,tot);
long[] elem;
if(n<=14)
{
elem=new long[n];
for(int i=1;i<n+1;i++)
elem[i-1]=i;
}
else
{
elem=new long[14];
for(int i=1;i<=14;i++)
elem[14-i]=n-i+1;
}
KPermutation perm=new KPermutation(elem);
if(n<=14 && k>= perm.fact[(int)n])
{
out.println(-1);
return;
}
long[] ans=perm.GetKPermutation(k);
int start=n-elem.length;//elemento a partir del cual empiezo a contar
int count=0;
for(int i=0;i<ans.length;i++)
{
if(IsLucky(start+i+1)&&IsLucky(ans[i]))
count++;
}
//determinando luckys numbers entre 1 y start
int lucky_index=Arrays.binarySearch(lucky, start);
if(lucky_index<0)lucky_index=~lucky_index;
count+=lucky_index;
out.println(count);
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int dx4[5] = {0, -1, 0, 1, 0};
const int dy4[5] = {0, 0, 1, 0, -1};
const int dx8[9] = {0, -1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[9] = {0, 0, 1, 0, -1, 1, -1, 1, -1};
const int maxn = 4e5 + 3;
const double PI = acos(-1.0);
const long long mod = 1e9 + 7;
long long po(long long a, long long b, long long mod) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) {
if (a == 0) {
return b;
} else {
return gcd(b % a, a);
}
}
void YES() {
puts("YES");
exit(0);
}
void Yes() {
puts("Yes");
exit(0);
}
void NO() {
puts("NO");
exit(0);
}
void No() {
puts("No");
exit(0);
}
void one() {
puts("-1");
exit(0);
}
long long n, k;
long long fac[20];
bool isluck(long long x) {
while (x) {
int k = x % 10;
if (!(k == 4 || k == 7)) {
return false;
}
x /= 10;
}
return true;
}
int used[20];
vector<int> prem(long long n, long long k) {
vector<int> ans;
k--;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (used[j]) continue;
if (k < fac[n - i]) {
ans.push_back(j);
used[j] = 1;
break;
} else {
k -= fac[n - i];
}
}
}
return ans;
}
vector<long long> g;
void is_ll(long long x) {
if (x <= n) {
g.push_back(x);
is_ll(x * 10 + 4);
is_ll(x * 10 + 7);
}
}
void solve() {
fac[0] = 1;
for (int i = 1; i <= 15; i++) {
fac[i] = fac[i - 1] * i;
}
scanf("%lld", &(n));
scanf("%lld", &(k));
if (n < 13 && k > fac[n]) {
puts("-1");
return;
}
long long N = min(n, 13LL);
vector<int> val = prem(N, k);
is_ll(4);
is_ll(7);
int ans = 0;
int sur = n - N + 1;
for (int i = 0; i < N; i++) {
if (isluck(i + sur) && isluck(val[i] + sur - 1)) ans++;
}
for (int i = 0; i < g.size(); i++) {
if (g[i] < sur) ans++;
}
cout << ans << endl;
}
int main() {
solve();
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | def lucky(x):
s=str(x)
return s.count('4')+s.count('7')==len(s)
def Gen_lucky(n):
if(len(n)==1):
if(n<"4"):
return 0
if(n<"7"):
return 1
return 2
s=str(n)
if(s[0]<'4'):
return 0
if(s[0]=='4'):
return Gen_lucky(s[1:])
if(s[0]<'7'):
return 2**(len(s)-1)
if(s[0]=='7'):
return 2**(len(s)-1)+Gen_lucky(s[1:])
else:
return 2**len(s)
def Form(X,k):
if(k==0):
return X
for i in range(len(X)):
if(k>=F[len(X)-i-1]):
h=k//F[len(X)-i-1]
r=k%F[len(X)-i-1]
G=list(X[i+1:])
G.remove(X[i+h])
G=[X[i]]+G
return Form(X[:i]+[X[i+h]]+G,r)
p=1
F=[1]
i=1
while(p<=10**15):
p*=i
F.append(p)
i+=1
n,k=map(int,input().split())
if(n<=14):
if(k>F[n]):
print(-1)
else:
L=Form(list(range(1,n+1)),k-1)
x=0
for i in range(n):
if(lucky(i+1) and lucky(L[i])):
x+=1
print(x)
else:
L=Form(list(range(n-14,n+1)),k-1)
ss=str(n-15)
x=0
for i in range(1,len(ss)):
x+=2**i
x+=Gen_lucky(ss)
for i in range(n-14,n+1):
if(lucky(L[i-n+14]) and lucky(i)):
x+=1
print(x)
| PYTHON3 |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000000000ll;
const long double EPS = 1E-9;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double PI = 2 * acos(.0);
long long fact[15];
void factorial(int x) {
fact[0] = 1;
fact[1] = 1;
for (int i = 2; i < (int)(x + 1); i++) fact[i] = i * fact[i - 1];
return;
}
vector<long long> kthPerm(int x, int k) {
bool used[15];
for (int i = 0; i < (int)(x + 1); i++) used[i] = 0;
long long base = 0;
vector<long long> resp;
for (int i = 0; i < (int)(x); i++) {
long long rad = fact[x - i - 1];
for (int j = 1; j < (int)(x + 1); j++) {
if (used[j]) continue;
if (base + rad >= k) {
resp.push_back(j);
used[j] = 1;
break;
} else {
base += rad;
}
}
}
return resp;
}
vector<long long> luck;
void genLucky(long long i) {
if (i >= 1000000000ll) return;
long long k = i * 10ll + 7ll;
long long l = i * 10ll + 4ll;
luck.push_back(k);
luck.push_back(l);
genLucky(k);
genLucky(l);
}
bool isL(int x) {
while (x > 0) {
int y = x % 10;
if (!(y == 4 || y == 7)) return 0;
x /= 10;
}
return 1;
}
int main() {
int n, k;
cin >> n >> k;
factorial(13);
int cont = 0;
if (n < 13) {
if (k > fact[n])
cout << "-1" << endl;
else {
vector<long long> pp = kthPerm(n, k);
for (int i = 0; i < (int)((pp).size()); i++) {
if (isL(pp[i]) && isL(i + 1)) cont++;
}
cout << cont << endl;
}
} else {
vector<long long> pp = kthPerm(13, k);
genLucky(0ll);
int left = n - 13;
for (int i = 0; i < (int)((luck).size()); i++) {
if (luck[i] <= (left)) {
cont++;
}
}
for (int i = 0; i < (int)((pp).size()); i++) {
pp[i] += left;
}
for (int i = 0; i < (int)(13); i++) {
if (isL(pp[i]) && isL(left + i + 1)) cont++;
}
cout << cont << endl;
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, k;
int s[20], d[20], t[20], r[20];
int a[2000];
void dfs(long long x) {
if (x > 1000000000) return;
a[++a[0]] = x;
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
int main() {
cin >> n;
cin >> k;
k--;
a[0] = 0;
dfs(4);
dfs(7);
a[0] = 2000000000;
int x = 1;
s[1] = 1;
for (int i = 1; i < 12; i++) {
x++;
s[i + 1] = (s[i] * x);
}
if (n <= 12 && s[n] <= k) {
cout << -1 << endl;
return 0;
}
for (int i = min(12, n - 1); i >= 1; i--) {
d[min(12, n - 1) - i] = k / s[i];
k = (k % s[i]);
if (i == 1) d[min(12, n - 1)] = k;
}
for (int i = 0; i < min(13, n); i++) t[i] = i;
for (int i = 0; i < min(13, n); i++) {
r[i] = t[d[i]] + (n - min(n, 13) + 1);
swap(t[d[i]], t[min(12, n - 1) - i]);
sort(t, t + (min(12, n - 1) - i));
}
sort(a, a + 1023);
a[1021] = 777777777;
a[1021] = 777777774;
a[1022] = 777777777;
int ans = upper_bound(a, a + 1022, n - 13) - a;
for (int i = 0; i < min(n, 13); i++) {
if (binary_search(a, a + 1023, i + (n - min(12, n - 1)))) {
if (binary_search(a, a + 1023, r[i])) ans++;
}
}
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, k, f[13], p[13];
vector<int> lk;
map<int, bool> bea;
void gen(int x) {
if (x) lk.push_back(x), bea[x] = true;
if (x > int(1e8)) return;
gen(x * 10 + 4);
gen(x * 10 + 7);
}
int main() {
scanf("%d%d", &n, &k);
f[0] = 1;
for (int i = 1; i <= 12; ++i) f[i] = f[i - 1] * i;
if (n <= 12 && k > f[n]) {
printf("-1");
return 0;
}
gen(0);
lk.push_back(int(1e9) + 1);
sort(lk.begin(), lk.end());
int d;
for (int i = 1; i <= n; ++i)
if (f[i] >= k) {
d = i;
break;
}
int ans = 0;
for (int i = 0; i < lk.size(); ++i)
if (lk[i] > n - d) {
ans = i;
break;
}
for (int i = 1; i <= d; ++i) {
for (int j = 1; j <= d; ++j)
if (!p[j]) {
if (k > f[d - i])
k -= f[d - i];
else {
p[j] = 1;
if (bea[n - d + i] && bea[n - d + j]) ++ans;
break;
}
}
}
printf("%d", ans);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
const int N = 2000010;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int dir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, -1, -1, -1, 1, 1, -1, 1, 1};
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
void read(T& x) {
char c;
bool op = 0;
while (c = getchar(), c < '0' || c > '9')
if (c == '-') op = 1;
x = c - '0';
while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0';
if (op) x = -x;
}
template <class T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
long long n, k;
long long ans;
long long fac[N] = {1};
long long res[N];
long long tot = 0;
long long find(long long x) {
sort(res + 1, res + 1 + tot);
long long ret = res[x];
for (int i = x + 1; i <= tot; i++) {
res[i - 1] = res[i];
}
--tot;
return ret;
}
void dfs(long long x, long long lim) {
if (x > lim) return;
if (x != 0) ++ans;
dfs(x * 10 + 4, lim);
dfs(x * 10 + 7, lim);
}
bool ck(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
void solve() {
read(n), read(k);
long long p = -1;
for (int i = 1; i <= n; i++) {
fac[i] = fac[i - 1] * i;
res[++tot] = n - i + 1;
if (fac[i] >= k) {
p = i;
break;
}
}
k--;
if (p == -1) {
puts("-1");
return;
}
dfs(0, n - p);
for (long long i = p, val, pos; i >= 1; i--) {
val = find(k / fac[i - 1] + 1);
pos = n - i + 1;
if (ck(val) && ck(pos)) {
++ans;
}
k = k % fac[i - 1];
}
write(ans);
putchar('\n');
}
int main() {
solve();
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: piyushd
* Date: 3/26/11
* Time: 10:53 PM
* To change this template use File | Settings | File Templates.
*/
public class TaskC {
long[] fact;
void precal() {
this.fact = new long[15];
fact[0] = 1;
for(int i = 1; i < 15; i++)
fact[i] = i * fact[i - 1];
}
boolean isLucky(int x) {
while(x > 0) {
int n = x % 10;
if(n != 4 && n != 7) return false;
x /= 10;
}
return true;
}
final long LIM = 123456789123L;
ArrayList<Long> lucky;
void generate(long x) {
if(x < LIM) lucky.add(x);
else return;
generate(x * 10 + 4);
generate(x * 10 + 7);
}
int totalLucky(int x) {
if(x < 4) return 0;
this.lucky = new ArrayList<Long>();
generate(4);
generate(7);
Collections.sort(lucky);
int low = 0, high = lucky.size();
while (high - low > 1) {
int mid = low + (high - low) / 2;
if(lucky.get(mid) <= x) low = mid;
else high = mid;
}
return low + 1;
}
void run() {
int n = nextInt(), k = nextInt();
precal();
if(n < 15) {
if(k > fact[n]) {
System.out.println(-1);
return;
}
}
int[] a = new int[15];
Arrays.fill(a, -1);
boolean[] used = new boolean[15];
int ix = 0, total = 0;
while (ix < 15) {
int cnt = 0, cur = -1;
for(int i = 0; i < 15; i++)
if(!used[i]) {
if(fact[15 - ix - 1] + total < k) {
total += fact[15 - ix - 1];
continue;
}
else {
cur = i;
break;
}
}
a[ix] = cur;
ix++;
used[cur] = true;
}
int ans = 0, correct = 0;
for(int i = 0; i < 15; i++) {
if(a[i] == i) correct++;
else break;
}
for(int i = correct; i < 15; i++) {
int num = a[i] - correct + 1 + n - (15 - correct);
int index = i - correct + 1 + n - (15 - correct);
if(isLucky(num) && isLucky(index)) ans++;
}
ans += totalLucky(n - 15 + correct);
System.out.println(ans);
}
int nextInt() {
try {
int c = System.in.read();
if (c == -1) return c;
while (c != '-' && (c < '0' || '9' < c)) {
c = System.in.read();
if (c == -1) return c;
}
if (c == '-') return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
long nextLong() {
try {
int c = System.in.read();
if (c == -1) return -1;
while (c != '-' && (c < '0' || '9' < c)) {
c = System.in.read();
if (c == -1) return -1;
}
if (c == '-') return -nextLong();
long res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
String nextLine() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (c == '\r' || c == '\n')
c = System.in.read();
do {
res.append((char) c);
c = System.in.read();
} while (c != '\r' && c != '\n');
return res.toString();
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
new TaskC().run();
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* Author -
* User: kansal
* Date: 10/27/11
* Time: 8:49 PM
*/
public class CF91E {
public static void main(String[] args) {
reader = new BufferedReader(new InputStreamReader(System.in));
List<Integer> luckyNums = genAll();
long[] f = new long[14];
f[0] = 1;
for(int i = 1; i < 14; ++i) {
f[i] = i * f[i-1];
}
int n = nextInt(), k = nextInt();
if (n <= 13 && k > f[n]) {
System.out.println(-1);
return;
}
int x = 1;
for(; f[x] < k; ++x);
int res = 0;
for(int lucky: luckyNums) {
if (lucky < n-x+1) ++res;
}
int[] p = getKthPerm(k-1, n-x+1, n, x, f);
for(int i = 0; i < x; ++i) {
if (isLucky(p[i]) && isLucky(n-x+1+i)) {
++res;
}
}
System.out.println(res);
}
private static int[] getKthPerm(int k, int a, int b, int n, long[] fac) {
int[] res = new int[n];
for(int i = a; i <= b; ++i) {
res[i-a] = i;
}
for(int i = 0; i < n; ++i) {
int x = (int)(k / fac[n-i-1]);
int y = res[x+i];
for(int j = x + i - 1; j >= i; --j) {
res[j+1] = res[j];
}
res[i] = y;
k = (int)(k % fac[n-i-1]);
}
return res;
}
private static boolean isLucky(int x) {
if (x == 0) return false;
while (x > 0) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
private static List<Integer> genAll() {
List<Integer> ret = new ArrayList<Integer>();
for(int i = 1; i < 10; ++i) {
for(int set = 0; set < (1 << i); ++set) {
int x = 0;
for(int j = 0; j < i; ++j) {
if (((set >> j) & 1) == 1) {
x = x * 10 + 7;
}
else {
x = x * 10 + 4;
}
}
ret.add(x);
}
}
return ret;
}
public static BufferedReader reader;
public static StringTokenizer tokenizer = null;
static String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
static public int nextInt() {
return Integer.parseInt(nextToken());
}
static public long nextLong() {
return Long.parseLong(nextToken());
}
static public String next() {
return nextToken();
}
static public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int c(long long a, long long b) {
if (a > b) return 0;
return 1 + c(a * 10 + 4, b) + c(a * 10 + 7, b);
}
int is(long long a) {
return a == 0 || (a % 10 == 4 || a % 10 == 7) && is(a / 10);
}
long long f[20];
bool u[20];
int main() {
int n, k;
cin >> n >> k;
k--;
f[0] = 1;
for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * i;
if (n <= 13 && f[n] <= k) {
cout << -1;
return 0;
}
int ans = c(0, n) - 1;
for (int i = 13; i >= 0; i--) {
int shift = k / f[i], c = -1;
while (shift >= 0) {
shift -= !u[++c];
}
u[c] = 1;
int t = c - 13 + n;
if (t > 0) {
if (is(n - i)) ans += is(t) - 1;
}
k %= f[i];
}
cout << ans;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long a[10005];
int f[20], nums[20], c[20];
int num;
int g(int x) {
int ret = 0;
for (int i = 1; i <= num; i++)
if (a[i] <= x) ret++;
return ret;
}
bool lucky(int x) {
while (x) {
int c = x % 10;
if (c != 4 && c != 7) return false;
x /= 10;
}
return true;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
f[0] = 1;
for (int i = 1; i <= 12; i++) f[i] = i * f[i - 1];
f[13] = 2123456789;
if (n <= 12)
if (f[n] < k) {
printf("-1\n");
return 0;
}
k--;
for (int i = 1; i <= 13; i++) nums[i] = n - 13 + i;
for (int i = 13; i >= 1; i--) {
int tmp = k / f[i - 1] + 1;
c[13 - i + 1] = nums[tmp];
for (int j = tmp; j <= 13; j++) nums[j] = nums[j + 1];
k %= f[i - 1];
}
num = 0;
for (int br = 1; br <= 10; br++)
for (int i = 1; i <= (1 << br); i++) {
int tmp = i;
num++;
for (int j = 1; j <= br; j++) {
if (tmp % 2)
a[num] = a[num] * 10LL + 4LL;
else
a[num] = a[num] * 10LL + 7LL;
tmp /= 2;
}
}
sort(a + 1, a + num + 1);
int sol = 0;
for (int i = max(n - 12, 1); i <= n; i++) {
if (lucky(c[13 - (n - i)]) && lucky(i)) sol++;
}
printf("%d\n", g(n - 13) + sol);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret) {
char c;
int sgn;
T bit = 0.1;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && c != '.' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
if (c == ' ' || c == '\n') {
ret *= sgn;
return 1;
}
while (c = getchar(), c >= '0' && c <= '9') ret += (c - '0') * bit, bit /= 10;
ret *= sgn;
return 1;
}
using namespace std;
long long f[22];
int n;
long long k;
int a[22];
set<int> st;
set<int>::iterator it;
void geta(int n, long long k) {
k--;
int idx = n - 1;
for (int i = int(1); i <= int(n); i++) st.insert(i);
for (int i = int(1); i <= int(n); i++) {
int cot = 1;
if (idx >= 0)
while (k >= f[idx]) cot++, k -= f[idx];
it = st.begin();
for (int j = int(1); j <= int(cot - 1); j++) it++;
a[i] = *it;
st.erase(*it);
idx--;
}
}
long long b[4444], bn;
int main() {
f[0] = 1;
for (int i = int(1); i <= int(13); i++) f[i] = f[i - 1] * i;
scanff(n);
scanff(k);
for (int j = int(1); j <= int(10); j++)
for (int i = int(0); i <= int(1 << j); i++) {
int x = i;
long long t = 0;
for (int k = int(1); k <= int(j); k++) {
t = t * 10;
if (x & 1)
t += 4;
else
t += 7;
x = x / 2;
}
b[++bn] = t;
}
sort(b + 1, b + 1 + bn);
bn = unique(b + 1, b + 1 + bn) - b - 1;
if (n <= 13) {
if (f[n] < k) {
printf("-1\n");
return 0;
}
geta(n, k);
int ans = 0;
if (a[4] == 4 || a[4] == 7) ans++;
if (a[7] == 7 || a[7] == 4) ans++;
printf("%d\n", ans);
} else {
n = n - 13;
int idx = lower_bound(b + 1, b + 1 + bn, n) - b;
int ans = idx - 1;
if (b[idx] <= n) ans++;
geta(13, k);
for (int i = int(1); i <= int(13); i++) {
int idx = i + n;
int val = a[i] + n;
if (b[lower_bound(b + 1, b + 1 + bn, idx) - b] == idx &&
b[lower_bound(b + 1, b + 1 + bn, val) - b] == val)
ans++;
}
printf("%d\n", ans);
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long p[20];
int n, k, ans;
void pcheck(int num) {
char s[20];
int i;
sprintf(s, "%d", num);
for (i = 0; s[i]; i++)
if (s[i] != '4' && s[i] != '7') return;
ans++;
}
void check(long long num) {
if (num > n) return;
if (n - num > 15)
pcheck(num);
else {
int kk, pp[20], sk = k - 1, i, j;
kk = min(n, 17);
for (i = 1; i <= kk; i++) pp[i] = n - kk + i;
for (i = 1; i <= kk; i++)
if (sk >= p[kk - i]) {
int d = sk / p[kk - i];
int save = pp[i + d];
for (j = i + d; j > i; j--) pp[j] = pp[j - 1];
pp[j] = save;
sk -= d * p[kk - i];
}
pcheck(pp[kk - (n - num)]);
}
}
void solve(long long num, int d) {
if (d > 9) return;
if (num) check(num);
solve(num * 10 + 4, d + 1);
solve(num * 10 + 7, d + 1);
}
int main() {
int i;
cin >> n >> k;
p[1] = p[0] = 1;
for (i = 2; i < 18; i++) p[i] = i * p[i - 1];
if (n < 18 && p[n] < k)
cout << "-1";
else {
ans = 0;
solve(0, 0);
cout << ans;
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long N = 14;
long long n, k, ans, fac[N];
long long get(string &s) {
long long res = 0;
for (char c : s) res = 10 * res + (long long)(c - '0');
return res;
}
string get_s(long long x) {
string res;
while (x) {
res += (char)((x % 10) + '0');
x /= 10;
}
reverse(res.begin(), res.end());
return res;
}
string nxt(string s) {
long long indx = 0;
while (indx < s.size() && (s[indx] == '4' || s[indx] == '7')) indx++;
if (indx == (long long)s.size()) return s;
if (s[indx] < '4') {
for (long long i = indx; i < s.size(); i++) s[i] = '4';
return s;
}
if (s[indx] < '7') {
s[indx] = '7';
for (long long i = indx + 1; i < s.size(); i++) s[i] = '4';
return s;
}
for (long long i = indx - 1; ~i; i--) {
if (s[i] == '4') {
s[i] = '7';
for (long long j = i + 1; j < s.size(); j++) s[j] = '4';
return s;
}
}
string res;
for (long long i = 0; i <= (long long)s.size(); i++) res += '4';
return res;
}
bool luk(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
fac[0] = 1;
for (long long i = 1; i < N; i++) fac[i] = fac[i - 1] * i;
cin >> n >> k;
if (n < N && fac[n] < k) return cout << -1, 0;
string now = "1";
while (get(now) <= n) {
now = nxt(now);
long long x = get(now);
if (n - x > 12) ans++;
now = get_s(get(now) + 1);
}
vector<long long> v;
for (long long i = max(1LL, n - 12); i <= n; i++) v.push_back(i);
long long rem = max(1LL, n - 12);
while (v.size() && k) {
for (long long j = 0; j <= v.size(); j++)
if (k > j * fac[(long long)v.size() - 1] &&
k <= (j + 1) * fac[(long long)v.size() - 1]) {
k -= j * fac[(long long)v.size() - 1];
swap(v[0], v[j]);
sort(v.begin() + 1, v.end());
if (luk(rem) && luk(v[0])) ans++;
v.erase(v.begin(), v.begin() + 1);
rem++;
break;
}
}
for (long long i = 0; i < v.size(); i++)
if (luk(rem + i) && luk(v[i])) ans++;
cout << ans;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long fact[20], n, k, anss;
vector<int> vec, vec2;
long long num(int a) {
int j = 0;
for (int i = 31; i >= 0; i--)
if ((a >> i) & 1) {
j = i;
break;
}
long long ans = 0;
for (int i = j - 1; i >= 0; i--) {
ans *= 10;
if ((a >> i) & 1)
ans += 7;
else
ans += 4;
}
return ans;
}
bool lucky(int a) {
while (a > 0) {
if (a % 10 != 4 && a % 10 != 7) return false;
a /= 10;
}
return true;
}
int main() {
fact[0] = 1;
for (long long i = 1; i < 14; i++) fact[i] = fact[i - 1] * i;
cin >> n >> k;
if (n < 14 && k > fact[n]) {
cout << -1 << endl;
return 0;
}
k--;
int j = 1;
while (fact[j] <= k) j++;
j = n - j + 1;
for (int i = j; i <= n; i++) vec.push_back(i);
int Max = n + 1;
for (int i = j; i <= n; i++)
if (lucky(i)) {
Max = i;
break;
}
for (int i = j; i <= n; i++) {
int num = k / fact[n - i];
if (lucky(vec[num]) && lucky(i)) anss++;
k -= num * fact[n - i];
vec2.clear();
for (int i = 0; i < num; i++) vec2.push_back(vec[i]);
for (int i = num + 1; i < (int)vec.size(); i++) vec2.push_back(vec[i]);
vec.clear();
for (int i = 0; i < (int)vec2.size(); i++) vec.push_back(vec2[i]);
}
int a = 2;
while (num(a) < Max) {
anss++;
a++;
}
cout << anss << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, k;
int s[20], d[20], t[20], r[20];
int a[2000];
void dfs(long long x) {
if (x > 1000000000) return;
a[a[2000]++] = x;
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
int main() {
cin >> n;
cin >> k;
k--;
a[2000] = 0;
dfs(4);
dfs(7);
int x = 1;
s[1] = 1;
for (int i = 1; i < 12; i++) {
x++;
s[i + 1] = (s[i] * x);
}
if (n <= 12 && s[n] <= k) {
cout << -1 << endl;
return 0;
}
for (int i = min(12, n - 1); i >= 1; i--) {
d[min(12, n - 1) - i] = k / s[i];
k = (k % s[i]);
if (i == 1) d[min(12, n - 1)] = k;
}
for (int i = 0; i < min(13, n); i++) t[i] = i;
for (int i = 0; i < min(13, n); i++) {
r[i] = t[d[i]] + (n - min(n, 13) + 1);
swap(t[d[i]], t[min(12, n - 1) - i]);
sort(t, t + (min(12, n - 1) - i));
}
sort(a, a + 1022);
int ans = upper_bound(a, a + 1022, n - 13) - a;
for (int i = 0; i < min(n, 13); i++) {
if (binary_search(a, a + 1023, i + (n - min(12, n - 1)))) {
if (binary_search(a, a + 1023, r[i])) ans++;
}
}
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author AlexFetisov
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC_cf91 solver = new TaskC_cf91();
solver.solve(1, in, out);
out.close();
}
static class TaskC_cf91 {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int res = 0;
List<Integer> perm = new ArrayList<Integer>();
int begin = 1, end = n;
if (n > 13) {
for (int i = n, j = 0; j < 13; --i, ++j) {
perm.add(i);
}
Collections.sort(perm);
res += getLuckyUpTo(n - 13);
begin = n - 12;
end = n;
} else {
for (int i = 1; i <= n; ++i) {
perm.add(i);
}
}
if (k > f(perm.size())) {
out.println("-1");
return;
}
perm = getK(k, perm);
for (int i = begin; i <= end; ++i) {
if (isLucky(perm.get(i - begin)) && isLucky(i)) {
++res;
}
}
out.println(res);
}
private List<Integer> getK(int k, List<Integer> ip) {
List<Integer> p = new ArrayList<Integer>();
int n = ip.size();
boolean[] have = new boolean[n];
for (int i = 0; i < n; ++i) {
for (int d = 0; d < n; ++d) {
if (!have[d]) {
long rest = f(n - i - 1);
if (rest >= k) {
p.add(d);
have[d] = true;
break;
} else {
k -= rest;
}
}
}
}
int[] shuffled = new int[n];
for (int i = 0; i < n; ++i) {
shuffled[i] = ip.get(p.get(i));
}
for (int i = 0; i < n; ++i) {
p.set(i, shuffled[i]);
}
return p;
}
private int getLuckyUpTo(int upTo) {
int res = 0;
for (int len = 1; len <= 10; ++len) {
for (int mask = 0; mask < (1 << len); ++mask) {
long number = 0;
for (int i = 0; i < len; ++i) {
if (BitUtils.checkBit(mask, i)) {
number = number * 10L + 7;
} else {
number = number * 10L + 4;
}
}
if (number <= upTo) {
++res;
}
}
}
return res;
}
boolean isLucky(int x) {
if (x == 0) return false;
while (x != 0) {
int c = x % 10;
x /= 10;
if (c != 4 && c != 7) return false;
}
return true;
}
long f(int upTo) {
long res = 1L;
for (long i = 2; i <= upTo; ++i) {
res *= i;
}
return res;
}
}
static class BitUtils {
public static boolean checkBit(int mask, int bit) {
return (mask & (1 << bit)) > 0;
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer stt;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
return null;
}
}
public String nextString() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextString());
}
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 210;
const int M = 200000;
const int K = 510;
const long long LIT = 1000000000LL;
const int INF = 1 << 30;
long long n, k;
vector<long long> num;
void dfs(long long u) {
if (u) num.push_back(u);
if (u > LIT) return;
dfs(u * 10 + 4);
dfs(u * 10 + 7);
}
void init() {
dfs(0);
sort(num.begin(), num.end());
}
bool init2() {
long long tmp = 1;
for (long long i = 1; i <= n; i++) {
tmp *= i;
if (tmp >= k) return 1;
}
cout << -1 << endl;
return 0;
}
bool valid(int x) {
for (int i = 0; i < num.size(); i++)
if (num[i] == x) return 1;
return 0;
}
vector<long long> cal(vector<long long> v, long long m) {
int len = v.size();
vector<long long> ret = vector<long long>(len);
for (int i = len - 1; i >= 0; i--) {
ret[i] = m % (len - i);
m /= len - i;
}
for (int i = len - 1; i >= 0; i--) {
for (int j = i - 1; j >= 0; j--) {
if (ret[j] > ret[i]) continue;
ret[i]++;
}
}
for (int i = 0; i < len; i++) ret[i] = v[ret[i]];
return ret;
}
void solve() {
vector<long long> v;
long long bnd = max(n - 15, 1LL);
for (long long i = bnd; i <= n; i++) v.push_back(i);
v = cal(v, --k);
int ret = 0;
for (int i = 0; i < num.size(); i++) {
if (num[i] > bnd) break;
ret++;
}
for (int i = 0; i < v.size(); i++) {
long long p = bnd + i;
long long m = v[i];
if (valid(p) && valid(m)) ret++;
}
cout << ret << endl;
}
int main() {
init();
while (cin >> n >> k) {
if (!init2()) continue;
solve();
}
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long inf = 5e18;
vector<string> num_string({"4", "7"});
map<long long, int> mp;
vector<long long> fact;
bool check(long long mid, long long n, long long k) {
if (n - mid >= fact.size()) {
return false;
} else {
return fact[n - mid] <= k;
}
}
int main() {
fact.push_back(1);
while (fact.back() < 6e12) {
fact.push_back(fact.back() * (fact.size()));
}
long long fs = fact.size();
long long n, k;
cin >> n >> k;
if (n < fact.size() and fact[n] < k) {
cout << -1 << endl;
return 0;
}
k--;
while (num_string.back().size() < 10) {
vector<string> next_num = num_string;
for (string s : num_string) {
if (s.size() == num_string.back().size()) {
s.push_back('4');
next_num.push_back(s);
s.back() = '7';
next_num.push_back(s);
}
}
num_string = next_num;
}
for (string s : num_string) {
mp[(stoll(s))] = 1;
}
long long ans = 0;
for (auto& x : mp) {
if (x.first <= n) {
ans++;
}
}
int i = max(1ll, n - fs + 1);
vector<long long> remainingElements;
for (int j = i; j <= n; j++) {
remainingElements.push_back(j);
}
while (i <= n) {
long long rem = n - i;
if (fact[rem] <= k) {
long long div = k / fact[rem];
k -= div * fact[rem];
if (mp[i] and !mp[remainingElements[div]]) {
ans--;
}
remainingElements.erase(remainingElements.begin() + div);
} else {
if (mp[i] and !mp[remainingElements[0]]) {
ans--;
}
remainingElements.erase(remainingElements.begin());
}
i++;
}
cout << ans << endl;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long int fact(long long int n) {
if (n == 0) return 1;
return n * fact(n - 1);
}
vector<int> permutacion(int n, int k) {
if (n == 1) return vector<int>(1, 1);
for (int val = 1; val <= n; val++) {
if (val * fact(n - 1) >= k) {
vector<int> v = permutacion(n - 1, k - (val - 1) * fact(n - 1));
for (int i = 0; i < int(v.size()); i++)
if (v[i] >= val) v[i]++;
vector<int> nextv(1, val);
for (int i = 0; i < int(v.size()); i++) nextv.push_back(v[i]);
return nextv;
}
}
return vector<int>(1, 1);
}
void escriu(vector<int> v) {
for (int i = 0; i < int(v.size()); i++) cout << v[i] << ",";
cout << endl;
}
int main() {
vector<long long int> number;
for (int numdig = 1; numdig <= 10; numdig++) {
for (long long int c = 0; c < (1LL << numdig); c++) {
long long int num = 0;
long long int val = c;
for (int i = 0; i < numdig; i++) {
int dig = 4;
if (val % 2 == 1) dig = 7;
val /= 2;
num = 10 * num + dig;
}
number.push_back(num);
}
}
sort(number.begin(), number.end());
set<long long int> s;
for (int i = 0; i < int(number.size()); i++) s.insert(number[i]);
long long int n, k;
cin >> n >> k;
vector<int> p = permutacion(15, k);
if (n < 15 and fact(n) < k) {
cout << -1 << endl;
} else {
long long int sol = 0;
for (int i = 0; i < int(number.size()); i++) {
long long int index = number[i];
if (index < n - 15 + 1) {
sol++;
} else if (index <= n) {
long long int desp = n - 15;
if (s.find(p[index - (n - 15 + 1)] + desp) != s.end()) {
sol++;
}
}
}
cout << sol << endl;
}
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 1000;
const int inf = 1e9;
int per[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
bool is_lucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
long long makelucky(long long x, int l) {
long long lucky = 0;
while (l--) {
if (x & 1)
lucky = lucky * 10 + 7;
else
lucky = lucky * 10 + 4;
x /= 2;
}
return lucky;
}
long long solve(int n) {
set<long long> st;
for (long long l = 1; l <= 10; l++)
for (long long i = 0; i < ((long long)1 << l); i++)
st.insert(makelucky(i, l));
long long res = 0;
while (!st.empty()) {
long long lucky = *st.begin();
st.erase(*st.begin());
res += (lucky <= n - 13);
}
return res;
}
long long fact(long long x) {
long long res = 1;
for (int i = 1; i <= x; i++) res *= i;
return res;
}
void kthper(int k, int l, int r) {
if (r - l == 0) return;
long long f = fact(r - l), t = 0;
while ((t + 1) * f <= k) t++;
if (t) {
int b = per[l];
per[l] = per[l + t];
for (int i = l + t; i >= l + 2; i--) per[i] = per[i - 1];
per[l + 1] = b;
}
kthper(k - t * f, l + 1, r);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
if (n <= 14) {
long long res = 1;
for (int i = 1; i <= n; i++) res *= i;
if (k > res) return cout << -1 << endl, 0;
}
int r = min(n, (long long)13);
long long ans = solve(n);
kthper(k - 1, 1, r);
for (int i = 1; i <= r; i++)
ans += (is_lucky(n - r + i) && is_lucky(per[i] + n - r));
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int c = 15;
int n, k;
int r;
long long f[c];
vector<int> v;
int ans;
void go(long long i) {
if (i > (long long)n) return;
if (i) {
if (i <= n - r)
ans++;
else
v.push_back(i);
}
go(i * 10 + 4);
go(i * 10 + 7);
}
bool b[c];
bool q[c];
int main() {
int i;
f[0] = 1;
for (i = 1; i < c; ++i) f[i] = f[i - 1] * i;
scanf("%d%d", &n, &k);
if (n < c && f[n] < k) {
printf("-1\n");
return 0;
}
i = 1;
while (f[i] < k) ++i;
r = i;
go(0);
sort(v.begin(), v.end());
for (i = 0; i < v.size(); ++i) b[v[i] - (n - r)] = 1;
k--;
i = 1;
int j;
while (i <= r) {
int d = (k / f[r - i]) + 1;
k %= f[r - i];
j = 1;
while (d) {
if (!q[j]) --d;
if (d) ++j;
}
q[j] = 1;
if (b[i] && b[j]) ++ans;
++i;
}
printf("%d\n", ans);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
bool islucky(long long n) {
while (n) {
if (n % 10 == 4 || n % 10 == 7) {
} else
return 0;
n = n / 10;
}
return 1;
}
long long fact(long long k) {
long long n = 1;
while (k > 1) {
n = n * k;
k--;
}
return n;
}
vector<long long> factoradic_number(long long k) {
vector<long long> fac;
for (int i = 0; i <= 13; i++) {
fac.push_back((fact(i)));
}
long long kk = k, coef = 0;
;
vector<long long> factoradic;
int rp = 1;
while (kk >= fac[rp]) rp++;
while (kk) {
rp--;
coef = kk / fac[rp];
factoradic.push_back((coef));
kk = kk - fac[rp] * coef;
if (kk == 0) {
while (rp != 1) {
factoradic.push_back((0));
rp--;
}
}
}
return factoradic;
}
vector<long long> kthpermutation(vector<long long> v, long long k) {
vector<long long> factoradic;
vector<long long> permutation;
factoradic = factoradic_number(k);
while (factoradic.size() < v.size() - 1)
factoradic.insert(factoradic.begin(), 0);
int sz = v.size();
while (permutation.size() != sz - 1) {
permutation.push_back((v[factoradic[0]]));
v.erase(v.begin() + factoradic[0], v.begin() + factoradic[0] + 1);
factoradic.erase(factoradic.begin(), factoradic.begin() + 1);
}
permutation.push_back((v[0]));
return permutation;
}
int main() {
long long n, k;
cin >> n >> k;
vector<long long> fac;
for (int i = 0; i <= 13; i++) {
fac.push_back((fact(i)));
}
if (n <= 13) {
if (fac[n] < k) {
cout << "-1";
return 0;
}
}
k--;
vector<long long> num;
int sz = 2;
long long r, l;
num.push_back((4));
num.push_back((7));
int noe = 0;
while (num[num.size() - 1] != 777777777) {
sz = num.size();
for (int i = (sz - 1) / 2; i < sz; i++) {
long long p = num[i];
long long q = p * 10 + 4;
num.push_back((q));
q = p * 10 + 7;
num.push_back((q));
}
sz = num.size();
}
bool flag = 0;
long long nn = n;
if (n > 13) {
flag = 1;
n = n - 13;
}
int i = 0;
vector<long long> pq;
long long lll = 1;
if (flag) {
while (n >= num[i] && flag && i < num.size()) {
noe++;
i++;
}
lll = n + 1;
while (n++ < nn) {
pq.push_back((n));
}
n = nn;
} else {
nn = 1;
lll = 1;
while (n >= nn) {
pq.push_back((nn));
nn++;
}
}
vector<long long> pq1 = kthpermutation(pq, k);
int ii = 0;
for (; lll <= n; lll++) {
if (islucky(lll) && islucky(pq1[ii])) noe++;
ii++;
}
cout << noe;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
bool lucky(int x) {
for (; x; x /= 10)
if (x % 10 != 4 && x % 10 != 7) return false;
return true;
}
unsigned long long luck[] = {
4, 7, 44, 47, 74, 77, 444,
447, 474, 477, 744, 747, 774, 777,
4444, 4447, 4474, 4477, 4744, 4747, 4774,
4777, 7444, 7447, 7474, 7477, 7744, 7747,
7774, 7777, 44444, 44447, 44474, 44477, 44744,
44747, 44774, 44777, 47444, 47447, 47474, 47477,
47744, 47747, 47774, 47777, 74444, 74447, 74474,
74477, 74744, 74747, 74774, 74777, 77444, 77447,
77474, 77477, 77744, 77747, 77774, 77777, 444444,
444447, 444474, 444477, 444744, 444747, 444774, 444777,
447444, 447447, 447474, 447477, 447744, 447747, 447774,
447777, 474444, 474447, 474474, 474477, 474744, 474747,
474774, 474777, 477444, 477447, 477474, 477477, 477744,
477747, 477774, 477777, 744444, 744447, 744474, 744477,
744744, 744747, 744774, 744777, 747444, 747447, 747474,
747477, 747744, 747747, 747774, 747777, 774444, 774447,
774474, 774477, 774744, 774747, 774774, 774777, 777444,
777447, 777474, 777477, 777744, 777747, 777774, 777777,
4444444, 4444447, 4444474, 4444477, 4444744, 4444747, 4444774,
4444777, 4447444, 4447447, 4447474, 4447477, 4447744, 4447747,
4447774, 4447777, 4474444, 4474447, 4474474, 4474477, 4474744,
4474747, 4474774, 4474777, 4477444, 4477447, 4477474, 4477477,
4477744, 4477747, 4477774, 4477777, 4744444, 4744447, 4744474,
4744477, 4744744, 4744747, 4744774, 4744777, 4747444, 4747447,
4747474, 4747477, 4747744, 4747747, 4747774, 4747777, 4774444,
4774447, 4774474, 4774477, 4774744, 4774747, 4774774, 4774777,
4777444, 4777447, 4777474, 4777477, 4777744, 4777747, 4777774,
4777777, 7444444, 7444447, 7444474, 7444477, 7444744, 7444747,
7444774, 7444777, 7447444, 7447447, 7447474, 7447477, 7447744,
7447747, 7447774, 7447777, 7474444, 7474447, 7474474, 7474477,
7474744, 7474747, 7474774, 7474777, 7477444, 7477447, 7477474,
7477477, 7477744, 7477747, 7477774, 7477777, 7744444, 7744447,
7744474, 7744477, 7744744, 7744747, 7744774, 7744777, 7747444,
7747447, 7747474, 7747477, 7747744, 7747747, 7747774, 7747777,
7774444, 7774447, 7774474, 7774477, 7774744, 7774747, 7774774,
7774777, 7777444, 7777447, 7777474, 7777477, 7777744, 7777747,
7777774, 7777777, 44444444, 44444447, 44444474, 44444477, 44444744,
44444747, 44444774, 44444777, 44447444, 44447447, 44447474, 44447477,
44447744, 44447747, 44447774, 44447777, 44474444, 44474447, 44474474,
44474477, 44474744, 44474747, 44474774, 44474777, 44477444, 44477447,
44477474, 44477477, 44477744, 44477747, 44477774, 44477777, 44744444,
44744447, 44744474, 44744477, 44744744, 44744747, 44744774, 44744777,
44747444, 44747447, 44747474, 44747477, 44747744, 44747747, 44747774,
44747777, 44774444, 44774447, 44774474, 44774477, 44774744, 44774747,
44774774, 44774777, 44777444, 44777447, 44777474, 44777477, 44777744,
44777747, 44777774, 44777777, 47444444, 47444447, 47444474, 47444477,
47444744, 47444747, 47444774, 47444777, 47447444, 47447447, 47447474,
47447477, 47447744, 47447747, 47447774, 47447777, 47474444, 47474447,
47474474, 47474477, 47474744, 47474747, 47474774, 47474777, 47477444,
47477447, 47477474, 47477477, 47477744, 47477747, 47477774, 47477777,
47744444, 47744447, 47744474, 47744477, 47744744, 47744747, 47744774,
47744777, 47747444, 47747447, 47747474, 47747477, 47747744, 47747747,
47747774, 47747777, 47774444, 47774447, 47774474, 47774477, 47774744,
47774747, 47774774, 47774777, 47777444, 47777447, 47777474, 47777477,
47777744, 47777747, 47777774, 47777777, 74444444, 74444447, 74444474,
74444477, 74444744, 74444747, 74444774, 74444777, 74447444, 74447447,
74447474, 74447477, 74447744, 74447747, 74447774, 74447777, 74474444,
74474447, 74474474, 74474477, 74474744, 74474747, 74474774, 74474777,
74477444, 74477447, 74477474, 74477477, 74477744, 74477747, 74477774,
74477777, 74744444, 74744447, 74744474, 74744477, 74744744, 74744747,
74744774, 74744777, 74747444, 74747447, 74747474, 74747477, 74747744,
74747747, 74747774, 74747777, 74774444, 74774447, 74774474, 74774477,
74774744, 74774747, 74774774, 74774777, 74777444, 74777447, 74777474,
74777477, 74777744, 74777747, 74777774, 74777777, 77444444, 77444447,
77444474, 77444477, 77444744, 77444747, 77444774, 77444777, 77447444,
77447447, 77447474, 77447477, 77447744, 77447747, 77447774, 77447777,
77474444, 77474447, 77474474, 77474477, 77474744, 77474747, 77474774,
77474777, 77477444, 77477447, 77477474, 77477477, 77477744, 77477747,
77477774, 77477777, 77744444, 77744447, 77744474, 77744477, 77744744,
77744747, 77744774, 77744777, 77747444, 77747447, 77747474, 77747477,
77747744, 77747747, 77747774, 77747777, 77774444, 77774447, 77774474,
77774477, 77774744, 77774747, 77774774, 77774777, 77777444, 77777447,
77777474, 77777477, 77777744, 77777747, 77777774, 77777777, 444444444,
444444447, 444444474, 444444477, 444444744, 444444747, 444444774, 444444777,
444447444, 444447447, 444447474, 444447477, 444447744, 444447747, 444447774,
444447777, 444474444, 444474447, 444474474, 444474477, 444474744, 444474747,
444474774, 444474777, 444477444, 444477447, 444477474, 444477477, 444477744,
444477747, 444477774, 444477777, 444744444, 444744447, 444744474, 444744477,
444744744, 444744747, 444744774, 444744777, 444747444, 444747447, 444747474,
444747477, 444747744, 444747747, 444747774, 444747777, 444774444, 444774447,
444774474, 444774477, 444774744, 444774747, 444774774, 444774777, 444777444,
444777447, 444777474, 444777477, 444777744, 444777747, 444777774, 444777777,
447444444, 447444447, 447444474, 447444477, 447444744, 447444747, 447444774,
447444777, 447447444, 447447447, 447447474, 447447477, 447447744, 447447747,
447447774, 447447777, 447474444, 447474447, 447474474, 447474477, 447474744,
447474747, 447474774, 447474777, 447477444, 447477447, 447477474, 447477477,
447477744, 447477747, 447477774, 447477777, 447744444, 447744447, 447744474,
447744477, 447744744, 447744747, 447744774, 447744777, 447747444, 447747447,
447747474, 447747477, 447747744, 447747747, 447747774, 447747777, 447774444,
447774447, 447774474, 447774477, 447774744, 447774747, 447774774, 447774777,
447777444, 447777447, 447777474, 447777477, 447777744, 447777747, 447777774,
447777777, 474444444, 474444447, 474444474, 474444477, 474444744, 474444747,
474444774, 474444777, 474447444, 474447447, 474447474, 474447477, 474447744,
474447747, 474447774, 474447777, 474474444, 474474447, 474474474, 474474477,
474474744, 474474747, 474474774, 474474777, 474477444, 474477447, 474477474,
474477477, 474477744, 474477747, 474477774, 474477777, 474744444, 474744447,
474744474, 474744477, 474744744, 474744747, 474744774, 474744777, 474747444,
474747447, 474747474, 474747477, 474747744, 474747747, 474747774, 474747777,
474774444, 474774447, 474774474, 474774477, 474774744, 474774747, 474774774,
474774777, 474777444, 474777447, 474777474, 474777477, 474777744, 474777747,
474777774, 474777777, 477444444, 477444447, 477444474, 477444477, 477444744,
477444747, 477444774, 477444777, 477447444, 477447447, 477447474, 477447477,
477447744, 477447747, 477447774, 477447777, 477474444, 477474447, 477474474,
477474477, 477474744, 477474747, 477474774, 477474777, 477477444, 477477447,
477477474, 477477477, 477477744, 477477747, 477477774, 477477777, 477744444,
477744447, 477744474, 477744477, 477744744, 477744747, 477744774, 477744777,
477747444, 477747447, 477747474, 477747477, 477747744, 477747747, 477747774,
477747777, 477774444, 477774447, 477774474, 477774477, 477774744, 477774747,
477774774, 477774777, 477777444, 477777447, 477777474, 477777477, 477777744,
477777747, 477777774, 477777777, 744444444, 744444447, 744444474, 744444477,
744444744, 744444747, 744444774, 744444777, 744447444, 744447447, 744447474,
744447477, 744447744, 744447747, 744447774, 744447777, 744474444, 744474447,
744474474, 744474477, 744474744, 744474747, 744474774, 744474777, 744477444,
744477447, 744477474, 744477477, 744477744, 744477747, 744477774, 744477777,
744744444, 744744447, 744744474, 744744477, 744744744, 744744747, 744744774,
744744777, 744747444, 744747447, 744747474, 744747477, 744747744, 744747747,
744747774, 744747777, 744774444, 744774447, 744774474, 744774477, 744774744,
744774747, 744774774, 744774777, 744777444, 744777447, 744777474, 744777477,
744777744, 744777747, 744777774, 744777777, 747444444, 747444447, 747444474,
747444477, 747444744, 747444747, 747444774, 747444777, 747447444, 747447447,
747447474, 747447477, 747447744, 747447747, 747447774, 747447777, 747474444,
747474447, 747474474, 747474477, 747474744, 747474747, 747474774, 747474777,
747477444, 747477447, 747477474, 747477477, 747477744, 747477747, 747477774,
747477777, 747744444, 747744447, 747744474, 747744477, 747744744, 747744747,
747744774, 747744777, 747747444, 747747447, 747747474, 747747477, 747747744,
747747747, 747747774, 747747777, 747774444, 747774447, 747774474, 747774477,
747774744, 747774747, 747774774, 747774777, 747777444, 747777447, 747777474,
747777477, 747777744, 747777747, 747777774, 747777777, 774444444, 774444447,
774444474, 774444477, 774444744, 774444747, 774444774, 774444777, 774447444,
774447447, 774447474, 774447477, 774447744, 774447747, 774447774, 774447777,
774474444, 774474447, 774474474, 774474477, 774474744, 774474747, 774474774,
774474777, 774477444, 774477447, 774477474, 774477477, 774477744, 774477747,
774477774, 774477777, 774744444, 774744447, 774744474, 774744477, 774744744,
774744747, 774744774, 774744777, 774747444, 774747447, 774747474, 774747477,
774747744, 774747747, 774747774, 774747777, 774774444, 774774447, 774774474,
774774477, 774774744, 774774747, 774774774, 774774777, 774777444, 774777447,
774777474, 774777477, 774777744, 774777747, 774777774, 774777777, 777444444,
777444447, 777444474, 777444477, 777444744, 777444747, 777444774, 777444777,
777447444, 777447447, 777447474, 777447477, 777447744, 777447747, 777447774,
777447777, 777474444, 777474447, 777474474, 777474477, 777474744, 777474747,
777474774, 777474777, 777477444, 777477447, 777477474, 777477477, 777477744,
777477747, 777477774, 777477777, 777744444, 777744447, 777744474, 777744477,
777744744, 777744747, 777744774, 777744777, 777747444, 777747447, 777747474,
777747477, 777747744, 777747747, 777747774, 777747777, 777774444, 777774447,
777774474, 777774477, 777774744, 777774747, 777774774, 777774777, 777777444,
777777447, 777777474, 777777477, 777777744, 777777747, 777777774, 777777777,
4444444444};
long long fact[] = {1, 1, 2, 6,
24, 120, 720, 5040,
40320, 362880, 3628800, 39916800,
479001600, 6227020800, 87178291200, 1307674368000};
void input(vector<int> &M, int n) {
M.resize(n + 1);
for (int i = 1; i <= n; i++) scanf("%d", &M[i]);
}
long long n, k;
int main() {
cin >> n >> k;
if (n <= 15 && k > fact[n])
printf("-1\n");
else {
long long st = n - 15;
if (st <= 0) st = 1;
vector<int> perm;
set<long long> was;
for (long long i = st; i <= n; i++) was.insert(i);
int j = n - st + 1;
for (long long i = st; i <= n; i++, j--) {
set<long long>::iterator it = was.begin();
while (k > fact[j - 1]) it++, k -= fact[j - 1];
perm.push_back(*it);
was.erase(it);
}
int ans = 0;
for (int i = 0; i < perm.size(); i++)
if (lucky(st + i) && lucky(perm[i])) ans++;
int q = 0;
while (luck[q] < st) q++, ans++;
printf("%d\n", ans);
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, k;
int n1;
vector<long long> v;
int c[20];
int a[20];
bool viz[20];
int f[20];
inline void get(int i) {
int cif = 0;
long long zece = 1;
long long r = 0;
for (zece = 1; i > 0; i >>= 1, zece *= 10LL) {
++cif;
if ((i & 1) != 0)
r += 7LL * zece;
else
r += 4LL * zece;
}
if (r != 0LL) v.push_back(r);
for (++cif; cif < 11; ++cif, zece *= 10LL) {
r += 4LL * zece;
v.push_back(r);
}
}
void back(int k1) {
if (k1 == n1 + 1) return;
for (int i = 1; i <= n1; ++i) {
if (viz[i]) continue;
if (k > f[n1 - k1])
k -= f[n1 - k1];
else {
viz[i] = true;
a[k1] = i;
back(k1 + 1);
return;
}
}
printf("-1\n");
exit(0);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < 1024; ++i) {
get(i);
}
sort(v.begin(), v.end());
int start = max(1, n - 12);
f[0] = f[1] = 1;
for (int i = 2; i < 13; ++i) f[i] = f[i - 1] * i;
n1 = n - start + 1;
back(1);
int rez = 0;
long long start1 = (long long)start;
for (size_t i = 0, lim = v.size(); i < lim && v[i] < start1; ++i) {
++rez;
}
int x = start;
for (int i = 1; i <= n1; ++i, ++x) {
c[i] = x;
}
for (int i = 1; i <= n1; ++i) {
if (find(v.begin(), v.end(), (long long)(i - 1) + start1) != v.end() &&
find(v.begin(), v.end(), (long long)c[a[i]]) != v.end())
++rez;
}
printf("%d\n", rez);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long n, k;
long long fact[20];
inline long long XF(int x) { return x > 13 ? 10000000000LL : fact[x]; }
int L, R;
map<int, int> pv;
vector<int> avb;
int lucky(int w) {
while (w) {
int T = w % 10;
if (T != 4 && T != 7) return 0;
w /= 10;
}
return 1;
}
int main() {
cin >> n >> k;
fact[0] = 1;
for (int i = 1; i <= 13; ++i) fact[i] = i * fact[i - 1];
if (XF(n) < k) {
cout << "-1\n";
return 0;
}
R = n;
L = R - min(n, 13LL) + 1;
for (int i = L; i <= R; ++i) avb.push_back(i);
for (int i = L; i <= R; ++i) {
int j = 0;
for (j = 0;; ++j)
if (XF(R - i) < k)
k -= XF(R - i);
else
break;
pv[i] = avb[j];
avb.erase(avb.begin() + j);
}
int ans = 0;
for (int len = 1; len <= 9; ++len)
for (int msk = 0; msk < (1 << len); ++msk) {
long long T = 0;
for (int i = 0; i < len; ++i) T = T * 10 + ((msk & (1 << i)) ? 4 : 7);
if (T <= n) {
if (!pv.count(T))
ans += lucky(T);
else
ans += lucky(pv[T]);
}
}
cout << ans << '\n';
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.util.*;
public class Main {
public static long factorial(int n) {
long res = 1;
for (int i = n; i > 1; i--)
res *= i;
return res;
}
private static ArrayList<Integer> res(ArrayList<Integer> v, int k) {
ArrayList<Integer> res = new ArrayList<Integer>();
long fact;
int indx, limit = v.size();
for (int i = 0; i < limit; i++) {
fact = factorial(v.size() - 1);
indx = (int) (k / fact);
if (k % fact == 0)
indx--;
k -= fact * indx;
int temp = v.get(indx);
res.add(temp);
v.remove(indx);
}
return res;
}
static int lucky[] = new int[1022];
static int size = 0;
private static void generate(int i, String s) {
if (i == 10)
return;
if (s.length() > 0)
lucky[size++] = new Integer(s);
generate(i + 1, s + 4);
generate(i + 1, s + 7);
}
public static void main(String[] args) {
generate(0, "");
Arrays.sort(lucky);
Scanner myScanner = new Scanner(System.in);
int k, n, startIndx = 0;
n = myScanner.nextInt();
k = myScanner.nextInt();
for (int i = 0; i < 14; i++)
if (factorial(i + 1) >= k) {
startIndx = n - i;
break;
}
if (startIndx <= 0) {
System.out.println("-1");
System.exit(0);
}
ArrayList<Integer> init = new ArrayList<Integer>();
for (int i = startIndx; i <= n; i++)
init.add(i);
ArrayList<Integer> perm = res(init, k);
int res = 0;
for (int i = 0; i < perm.size(); i++)
if (Arrays.binarySearch(lucky, i + startIndx) >= 0
&& Arrays.binarySearch(lucky, perm.get(i)) >= 0)
res++;
for (int i = 0; i < lucky.length; i++)
if (lucky[i] < startIndx)
res++;
System.out.println(res);
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long n, k, m;
long long ed;
long long f[20], cur[20], a[20], vis[20];
long long ans = 0;
void solve(long long now) {
if (now > m) return;
ans++;
solve(now * 10 + 4);
solve(now * 10 + 7);
}
bool legal(long long x) {
while (x > 0) {
long long tmp = x % 10;
if (tmp != 4 && tmp != 7) return false;
x /= 10;
}
return true;
}
int main() {
cin >> n >> k;
ed = 1;
f[ed] = 1;
f[0] = 1;
while (f[ed] < k) {
f[ed + 1] = f[ed] * (ed + 1);
++ed;
}
m = n - ed;
if (m < 0) {
cout << -1 << endl;
return 0;
}
memset(vis, false, sizeof(vis));
for (int i = 1; i <= ed; i++) {
cur[i] = (k + f[ed - i] - 1) / f[ed - i];
int cnt = 0;
for (int j = 1;; j++) {
if (!vis[j]) cnt++;
if (cnt == cur[i]) {
a[i] = j;
vis[j] = true;
break;
}
}
k -= f[ed - i] * (cur[i] - 1);
}
solve(4);
solve(7);
for (int i = 1; i <= ed; i++) {
long long num = a[i] + m;
long long pos = i + m;
if (legal(num) && legal(pos)) ans++;
}
cout << ans << endl;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | //package round91;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.BitSet;
public class C {
InputStream is;
PrintWriter out;
String INPUT = "";
long[] lucky;
void solve()
{
int n = ni(), k = ni();
lucky = new long[2000];
int pos = rec(lucky, 0, 0);
lucky = Arrays.copyOf(lucky, pos);
Arrays.sort(lucky);
long[] fact = new long[14];
fact[0] = 1;
for(int i = 1;i < 14;i++)fact[i] = fact[i-1]*i;
if(n <= 13){
if(k > fact[n]){
out.println(-1);
}else{
long p = k - 1;
int ct = 0;
BitSet used = new BitSet();
for(int i = n-1;i >= 0;i--){
long q = p / fact[i];
p = p % fact[i];
int tar = used.nextClearBit(0);
for(int j = 0;j < q;j++){
tar = used.nextClearBit(tar+1);
}
used.set(tar);
if(isLucky(n-i) && isLucky(tar+1)){
ct++;
}
}
out.println(ct);
}
}else{
int ct = 0;
for(int i = 0;i < lucky.length;i++){
if(lucky[i] <= n-13){
ct++;
}
}
long p = k - 1;
BitSet used = new BitSet();
for(int i = 12;i >= 0;i--){
long q = p / fact[i];
p = p % fact[i];
int tar = used.nextClearBit(0);
for(int j = 0;j < q;j++){
tar = used.nextClearBit(tar+1);
}
used.set(tar);
if(isLucky(n-i) && isLucky(tar+n-13+1)){
ct++;
}
}
out.println(ct);
}
}
boolean isLucky(long n)
{
if(n == 0)return false;
if(n==4 || n==7)return true;
return (n%10==4||n%10==7) && isLucky(n/10);
}
int rec(long[] lucky, int pos, long n)
{
if(n > 1000000000)return pos;
if(n != 0)lucky[pos++] = n;
pos = rec(lucky, pos, n*10+4);
pos = rec(lucky, pos, n*10+7);
return pos;
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception
{
new C().run();
}
public int ni()
{
try {
int num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public long nl()
{
try {
long num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public String ns()
{
try{
int b = 0;
StringBuilder sb = new StringBuilder();
while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
if(b == -1)return "";
sb.append((char)b);
while(true){
b = is.read();
if(b == -1)return sb.toString();
if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
sb.append((char)b);
}
} catch (IOException e) {
}
return "";
}
public char[] ns(int n)
{
char[] buf = new char[n];
try{
int b = 0, p = 0;
while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
if(b == -1)return null;
buf[p++] = (char)b;
while(p < n){
b = is.read();
if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
buf[p++] = (char)b;
}
return Arrays.copyOf(buf, p);
} catch (IOException e) {
}
return null;
}
double nd() { return Double.parseDouble(ns()); }
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long LINF = ~(((long long)0x1) << 63) / 2;
const int INF = 0X3F3F3F3F;
const double eps = 1e-7;
const long long limit = 1000000000LL;
vector<long long> c;
void dfs(long long now) {
if (now > limit) return;
if (now != 0) c.push_back(now);
dfs(now * 10 + 4);
dfs(now * 10 + 7);
}
int loc[15], lim;
void solve(int pos, int k) {
int now = 1, i, j, ans = 0, cnt = 0;
if (pos == lim + 1) return;
for (i = 1; i <= pos - 1; i++) now *= i;
for (i = 1; i <= lim; i++)
if (!loc[i]) {
ans += now;
cnt++;
if (ans >= k) {
loc[i] = lim - pos + 1;
solve(pos - 1, k - now * (cnt - 1));
break;
}
}
}
bool isOk(int d) {
bool flag = true;
while (d) {
int c = d % 10;
if (c != 4 && c != 7) flag = false;
d /= 10;
}
return flag;
}
int main() {
int n, k, i, j;
cin >> n >> k;
long long ans = 1;
bool ok = false;
dfs(0);
sort(c.begin(), c.end());
for (i = 1; i <= n; i++) {
ans *= i;
if (ans >= k) {
ok = true;
lim = i;
break;
}
}
if (!ok) {
cout << "-1" << endl;
} else {
solve(lim, k);
int ans = 0;
for (i = 0; i < c.size() && c[i] <= n; i++)
if (c[i] < n - lim)
ans++;
else {
if (isOk(loc[(int)(c[i] - (n - lim))] + n - lim)) ans++;
}
cout << ans << endl;
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
void RunProgram();
int main() { RunProgram(); }
long long fact[14], k;
int n, m, d[20];
bool avail[20];
void MakeD(int n) {
memset(avail, true, sizeof(avail));
for (int i = 1; i <= n; i++) {
int j;
for (j = 1; j <= n; j++)
if (avail[j])
if (k > fact[n - i])
k -= fact[n - i];
else
break;
j = min(j, n);
d[i] = j;
avail[j] = false;
}
}
bool isLucky(long long x) {
do {
int t = x % 10;
x /= 10;
if (t != 4 && t != 7) return false;
} while (x > 0);
return true;
}
int SolveSmall(int n) {
if (fact[n] < k) return -1;
MakeD(n);
int res = 0;
if (4 <= n) res += isLucky(d[4]);
if (7 <= n) res += isLucky(d[7]);
return res;
}
int Count(long long LIM) {
vector<long long> a;
a.push_back(0);
for (int i = 0; i < (int)a.size(); i++) {
long long v = a[i] * 10 + 4;
if (v <= LIM) a.push_back(v);
v = a[i] * 10 + 7;
if (v <= LIM) a.push_back(v);
}
return (int)a.size() - 1;
}
int SolveBig(int n) {
MakeD(14);
int res = Count(n - 14), delta = n - 14;
for (int i = 1; i <= 14; i++)
res += isLucky(d[i] + delta) && isLucky(i + delta);
return res;
}
void RunProgram() {
fact[0] = 1;
for (int i = 1; i < 14; i++) fact[i] = fact[i - 1] * i;
cin >> n >> k;
if (n < 14)
cout << SolveSmall(n);
else
cout << SolveBig(n);
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class C {
static List<Integer> luckyNumbers = new ArrayList<Integer>();
static long factorials[] = new long[13];
static void calculateLuckyNumbers(int n , int i){
if(i >= 10) return;
int nn = n*10 + 4;
luckyNumbers.add(nn);
calculateLuckyNumbers(nn, i+1);
nn = n*10 + 7;
luckyNumbers.add(nn);
calculateLuckyNumbers(nn, i+1);
}
static boolean isLucky(long x){
while(x>0){
int d =(int)( x%10);
if(d != 4 && d!= 7 ){
return false;
}
x/=10;
}
return true;
}
public static void main(String[] args) {
calculateLuckyNumbers(0 , 1);
factorials[1] = 1;
for(int i = 2 ; i<=12 ; i++ ){
factorials[i] = factorials[i-1]*(i);
}
Scanner in = new Scanner(System.in);
long n = in.nextInt();
long k = in.nextInt();
in.close();
if( n<13 && k>factorials[(int)n] ){
System.out.println(-1);
}else{
long index = Math.max(0, n-13);
int count = 0;
for(Integer i : luckyNumbers){
if(i<=index) count++;
}
List<Long> l = new LinkedList<Long>();
List<Long> ll = new LinkedList<Long>();
for( long i = index+1 ; i<=n ; i++){
l.add(i);
}
k--;
while(k>0){
long r = k/factorials[l.size()-1];
k = k%factorials[l.size()-1];
long num = l.remove((int)r);
ll.add(num);
}
ll.addAll(l);
for(int i = 0 ; i<ll.size() ; i++){
if(isLucky(i+index+1) && isLucky(ll.get(i))){
count++;
}
}
System.out.println(count);
}
}
}
| JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int intmax = 0x3f3f3f3f;
const long long lldmax = 0x3f3f3f3f3f3f3f3fll;
double eps = 1e-8;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline T lowbit(T n) {
return (n ^ (n - 1)) & n;
}
template <class T>
inline int countbit(T n) {
return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class T>
inline T checkmod(T n, T m) {
return (n % m + m) % m;
}
inline int rand(int a, int b) {
if (a >= b) return a;
return rand() % (b - a) + a;
}
template <class T>
inline T lcm(T a, T b) {
if (a < 0) return lcm(-a, b);
if (b < 0) return lcm(a, -b);
return a * (b / gcd(a, b));
}
template <class T>
inline T gcd(T a, T b) {
if (a < 0) return gcd(-a, b);
if (b < 0) return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T euclid(T a, T b, T &x, T &y) {
if (a < 0) {
T d = euclid(-a, b, x, y);
x = -x;
return d;
}
if (b < 0) {
T d = euclid(a, -b, x, y);
y = -y;
return d;
}
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
T d = euclid(b, a % b, x, y);
T t = x;
x = y;
y = t - (a / b) * y;
return d;
}
}
template <class T>
inline vector<pair<T, int> > factorize(T n) {
vector<pair<T, int> > R;
for (T i = 2; n > 1;) {
if (n % i == 0) {
int C = 0;
for (; n % i == 0; C++, n /= i)
;
R.push_back(make_pair(i, C));
}
i++;
if (i > n / i) i = n;
}
if (n > 1) R.push_back(make_pair(n, 1));
return R;
}
template <class T>
inline bool isPrimeNumber(T n) {
if (n <= 1) return false;
for (T i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
template <class T>
inline T eularFunction(T n) {
vector<pair<T, int> > R = factorize(n);
T r = n;
for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1);
return r;
}
template <class T>
inline int dblcmp(T a, const T b) {
a -= b;
return a > eps ? 1 : (a < -eps ? -1 : 0);
}
template <class T>
inline int sgn(T a) {
return a > eps ? 1 : (a < -eps ? -1 : 0);
}
const int N = 13;
long long tmp[N];
set<int> st;
int n, k;
void dfs(long long a) {
if (a > n) return;
st.insert(a);
dfs(a * 10 + 4);
dfs(a * 10 + 7);
}
bool judge() {
long long ans = 1;
for (int i = 1; i <= n; ++i) {
if (ans > k) break;
ans *= i;
}
return ans >= k;
}
void gao(int a[], int n, int k) {
if (n == 1) return;
int p = k / tmp[n - 1];
int t = a[p];
for (int i = p; i > 0; --i) a[i] = a[i - 1];
a[0] = t;
gao(a + 1, n - 1, k % tmp[n - 1]);
}
int main() {
tmp[0] = 1;
for (int i = 1; i < N; ++i) tmp[i] = tmp[i - 1] * i;
cin >> n >> k;
dfs(4);
dfs(7);
if (!judge()) {
puts("-1");
} else {
int ans = 0;
int m = max(0, n - N);
for (set<int>::iterator it = st.begin(); it != st.end(); ++it) {
if (*it <= m) {
ans++;
}
}
int a[20];
for (int i = 0; i < n - m; ++i) a[i] = i + m + 1;
gao(a, n - m, k - 1);
for (int i = 0; i < n - m; ++i) {
if (st.find(m + 1 + i) != st.end() && st.find(a[i]) != st.end()) ans++;
}
cout << ans << endl;
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long fac[20], n, k;
long long answer;
void rec(long long x, long long lim) {
if (x > lim) return;
answer++;
rec(10 * x + 4, lim);
rec(10 * x + 7, lim);
}
long long calc(long long lim) {
rec(4, lim);
rec(7, lim);
}
long long ohMyLuck(long long x) {
while (x > 0) {
if (x % 10 == 7 or x % 10 == 4) {
x /= 10;
} else {
return 0;
}
}
return 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n >> k;
fac[0] = 1;
for (int i = 1; i <= 15; i++) fac[i] = (fac[i - 1] * i);
if (n <= 13 and fac[n] < k) {
cout << -1 << endl;
return 0;
}
k--;
int mis = -1;
for (int i = 1; i <= 15; i++)
if (k >= fac[i]) mis = i;
calc(n - (mis + 1));
vector<int> vec, my;
for (int i = n - mis; i <= n; i++) vec.push_back(i);
for (int i = vec.size() - 1; i >= 0; i--) {
int d = k / fac[i];
k = k % fac[i];
my.push_back(vec[d]);
vec.erase(vec.begin() + d);
}
for (int i = 0; i < my.size(); i++) {
if (ohMyLuck(my[i]) and ohMyLuck(n - mis + i)) {
answer++;
}
}
cout << answer << endl;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
const int MAXN = 1e4 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
long long F[40];
int calc(int l, long long n) {
if (n >= l) return 0;
int res = 0;
res += calc(l, n * 10 + 4);
res += calc(l, n * 10 + 7);
return res + 1;
}
bool check(int x) {
bool ok = true;
while (x) {
int t = x % 10;
if (t != 4 && t != 7) ok = false;
x /= 10;
}
return ok;
}
int n, k;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
F[0] = 1LL;
int pos = -1;
for (int i = 1; i <= n; i++) {
F[i] = F[i - 1] * (long long)i;
if (F[i] >= (long long)k) {
pos = i;
break;
}
}
if (pos == -1) {
cout << -1;
return 0;
}
int s = n - pos + 1;
int res = calc(s, 4) + calc(s, 7);
vector<int> V;
for (int i = s; i <= n; i++) V.push_back(i);
for (int i = s; i <= n; i++) {
int j = n - i;
int z, t;
if (F[j] <= k) {
z = (k - 1) / F[j];
t = V[z];
k = (k - 1) % F[j] + 1;
} else {
z = 0;
t = V[0];
}
V.erase(V.begin() + z);
if (check(t) && check(i)) res++;
}
cout << res;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
set<long long> S;
void go(const long long &x, const int &M, const long long &U) {
if (x > U) return;
if (x > M) S.insert(x);
go(10 * x + 4, M, U), go(10 * x + 7, M, U);
}
bool ok(long long x) {
for (; x; x /= 10) {
int a = x % 10;
if (a != 4 && a != 7) return false;
}
return true;
}
int main() {
long long N, K;
cin >> N >> K;
long long F = -1;
if (N < 14) {
F = 1;
for (long long i = 1; i <= N; ++i) F *= i;
}
int M = 1;
for (long long f = 1; f * M < K; f *= M, M++)
;
if (M > (int)N) {
cout << -1 << endl;
return 0;
}
M = min((int)N, 20);
go(0, 0, N - M);
vector<long long> a(M);
for (int _n(M), i(0); i < _n; i++) a[i] = N - M + i + 1;
if (K < F || F == -1) {
for (long long k = K - 1; k > 0;) {
long long f = 1;
int i = 1;
while (f * (i + 1) <= k) f *= ++i;
int j = k / f;
swap(a[M - i - 1], a[M - i + j - 1]);
sort(&a[0] + M - i, &a[0] + M);
k -= f * j;
}
} else if (K == F) {
reverse((a).begin(), (a).end());
}
int ans = 0;
for (int _n(M), i(0); i < _n; i++)
if (ok(N - M + i + 1) && ok(a[i])) ans++;
ans += S.size();
cout << ans << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100099;
int n, k;
vector<long long> lucky;
void gen(long long num) {
lucky.push_back(num);
if (num > n) return;
gen(num * 10 + 4);
gen(num * 10 + 7);
}
vector<int> d;
long long precalc[20];
bool used[20];
void findk(int n, int k, int len) {
if (len < 0) return;
long long sum = 0;
for (int dig = 1; dig <= n; dig++) {
if (!used[dig]) {
if (sum + precalc[len] >= k) {
used[dig] = 1;
d.push_back(dig);
break;
} else
sum += precalc[len];
}
}
findk(n, k - sum, len - 1);
}
bool check(int n, int k, int pos) {
for (int i = 0, _n = (20); i < _n; i++) used[i] = 0;
d.clear();
findk(n, k, n - 1);
if (d[pos - 1] == pos) return 1;
return 0;
}
bool good(long long t) {
while (t) {
if (t % 10 != 7 && t % 10 != 4) return 0;
t /= 10;
}
return 1;
}
int main() {
scanf("%d%d", &n, &k);
gen(4);
gen(7);
long long fact = 1;
precalc[0] = 1;
precalc[1] = 1;
for (int i = 2; i < 20; i++) precalc[i] = precalc[i - 1] * i;
int p = 0;
for (int i = 1; i <= n + 1; i++) {
fact *= i;
p++;
if (fact >= k) break;
}
if (p > n) {
printf("-1\n");
return 0;
}
int last = n - p;
int ans = 0;
sort(lucky.begin(), lucky.end());
for (int i = 0; i < lucky.size(); i++) {
if (lucky[i] <= last) ans++;
if (lucky[i] > n) break;
if (lucky[i] > last) {
for (int i = 0, _n = (20); i < _n; i++) used[i] = 0;
d.clear();
findk(p, k, p - 1);
for (int i = (n - p + 1), _b = (n); i <= _b; i++)
if (good(i) && good(d[i - last - 1] + last)) ans++;
break;
}
}
printf("%d\n", ans);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long i, j, k, n, m, t, f, x, y, cnt;
long long nl, tmp, gcnt, ans;
long long luckyNum[20007], fct[16], v[16], ad[16];
queue<long long> qa, qb;
set<long long> st;
long long findFct(long long n) {
i = 0;
for (i = 1; i < 16; i++) {
if (fct[i] > n) {
return i;
}
}
}
void setV(long long n) {
long long i;
n++;
for (i = 1; i < 16; i++) {
if (!v[i]) {
n--;
}
if (n == 0) {
v[i] = true;
gcnt++;
ad[gcnt] = i + nl;
break;
}
}
}
int main() {
memset(luckyNum, 0, sizeof(luckyNum));
cnt = 0;
qa.push(4);
luckyNum[cnt++] = 4;
st.insert(4);
qa.push(7);
luckyNum[cnt++] = 7;
st.insert(7);
for (i = 1; i < 10; i++) {
while (!qa.empty()) {
x = qa.front();
qa.pop();
qb.push(x * 10 + 4);
luckyNum[cnt++] = x * 10 + 4;
st.insert(x * 10 + 4);
qb.push(x * 10 + 7);
luckyNum[cnt++] = x * 10 + 7;
st.insert(x * 10 + 7);
}
while (!qb.empty()) {
qa.push(qb.front());
qb.pop();
}
}
fct[0] = 1;
for (i = 1; i < 16; i++) {
fct[i] = fct[i - 1] * i;
}
while (cin >> n >> k) {
memset(v, 0, sizeof(v));
memset(ad, 0, sizeof(ad));
gcnt = 0;
ans = 0;
k--;
m = findFct(k);
nl = n - m;
if (nl < 0) {
cout << "-1" << endl;
continue;
}
i = 0;
while (luckyNum[i] <= nl) {
ans++;
i++;
}
tmp = m;
m--;
while (k > 0) {
t = k / fct[m];
k %= fct[m];
m--;
setV(t);
}
for (i = 1; i <= tmp; i++) {
if (!v[i]) {
gcnt++;
ad[gcnt] = i + nl;
}
}
for (i = 1; i <= gcnt; i++) {
if (st.find(ad[i]) != st.end() && st.find(i + nl) != st.end()) {
ans++;
}
}
cout << ans << endl;
}
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Map.Entry;
public class Solution implements Runnable {
public static void main(String[] args) {
(new Thread(new Solution())).start();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String r = in.readLine();
if (r == null) return null;
st = new StringTokenizer(r);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
long[] fact = new long[20];
int[] per(int q) {
int[] res = new int[13];
int[] was = new int[13];
for (int i = 0; i < 13; i++) {
int qq = (int)((long)q / fact[12 - i]);
q = (int)((long)q % fact[12 - i]);
int j = 0;
while (qq > 0 || was[j] == 1) {
if (was[j] == 0) qq--;
j++;
}
res[i] = j + 1;
was[j] = 1;
}
return res;
}
boolean lucky(long q) {
if (q == 0) return false;
while (q > 0) {
if (q % 10 != 4 && q % 10 != 7) return false;
q /= 10;
}
return true;
}
long[] lucky = new long[2000];
int ln = 0;
void solve() throws Exception {
for (int i = 1; i <= 9; i++) {
for (int j = 0; j < (1 << i); j++) {
long q = 0;
int jj = j;
for (int k = 0; k < i; k++) {
q *= 10;
if (jj % 2 == 0) q += 4; else q += 7;
jj /= 2;
}
lucky[ln++] = q;
}
}
Arrays.sort(lucky, 0, ln);
fact[0] = fact[1] = 1;
for (int i = 2; i < 20; i++) fact[i] = i * fact[i - 1];
int n = nextInt();
int k = nextInt();
if (n <= 13 && fact[n] < k) {
out.println(-1);
return;
}
int[] per = per(k - 1);
int ans = 0;
if (n <= 13) {
for (int i = 1; i <= n; i++) {
if (lucky(i) && lucky(per[i + 12 - n] - 13 + n)) ans++;
}
} else {
int qq = Arrays.binarySearch(lucky, 0, ln, n - 13);
if (qq >= 0) qq++; else qq = -(qq + 1);
ans += qq;
for (int i = 0; i < 13; i++) {
if (lucky(n - 13 + 1 + i) && lucky(per[i] + n - 13)) ans++;
}
}
out.println(ans);
}
public void run() {
Locale.setDefault(Locale.UK);
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// in = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter("output.txt");
solve();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
out.flush();
}
}
} | JAVA |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
void gen(vector<int> &nums, long long int x) {
if (x > 1000000000) return;
if (x > 0) nums.push_back(x);
gen(nums, 10 * x + 4);
gen(nums, 10 * x + 7);
}
bool isLucky(int x) {
while (x > 0) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
int n, k;
long long int fac[16];
bool used[16];
int perm[16];
cin >> n >> k;
memset(perm, 0, sizeof(perm));
memset(used, 0, sizeof(used));
fac[0] = 1;
for (int i = 1; i < 16; i++) fac[i] = i * fac[i - 1];
int m = min(15, n);
if (fac[m] < k) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < m; i++) {
int p = 1;
while (k > fac[m - 1 - i]) {
k -= fac[m - 1 - i];
++p;
}
for (int j = 0; j < m; j++) {
if (!used[j]) --p;
if (!p) {
perm[i] = n - m + j + 1;
used[j] = true;
break;
}
}
}
vector<int> lucky;
gen(lucky, 0);
int ret = 0;
for (int i = 0; i < (int)lucky.size(); i++) {
int num = lucky[i];
if (num > n) continue;
if (n > 15) {
if (num <= n - 15)
++ret;
else {
int x = 14 - (n - num);
if (isLucky(perm[x])) ++ret;
}
} else {
if (isLucky(perm[num - 1])) ++ret;
}
}
cout << ret << endl;
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = -1;
long long n, K, pot[20], fact[20], ans;
set<long long> luckySet;
bool used[20];
long long arr[20];
void kth(int pos, int size, long long k) {
if (pos == size + 1) {
return;
}
long long p = 1;
while (true) {
assert(p <= 20);
if (used[p]) {
p++;
continue;
}
if (k > fact[size - pos]) {
k -= fact[size - pos];
p++;
continue;
}
break;
}
arr[pos - 1] = p;
used[p] = true;
kth(pos + 1, size, k);
}
bool isLucky(long long v, const vector<long long> &lucky) {
for (int i = (0); i < (int)(lucky.size()); i++) {
if (lucky[i] == v) return true;
}
return false;
}
int main() {
scanf("%lld %lld", &n, &K);
pot[0] = 1;
for (int i = (1); i < (int)(18); i++) {
pot[i] = pot[i - 1] * 10LL;
}
fact[0] = 1;
for (int i = (1); i < (int)(20); i++) {
fact[i] = fact[i - 1] * i;
}
if (n <= 15 && fact[n] < K) {
printf("-1\n");
return 0;
}
for (int bm = (0); bm < (int)(1024); bm++) {
for (int j = (1); j < (int)(11); j++) {
long long l = 0;
for (int k = (0); k < (int)(j); k++) {
l += (bm & (1 << k)) ? 7 * pot[k] : 4 * pot[k];
}
luckySet.insert(l);
}
}
vector<long long> lucky(luckySet.begin(), luckySet.end());
for (int i = (0); i < (int)(lucky.size()); i++) {
if (lucky[i] <= n - 15) {
ans++;
}
}
int sz = min(n, 15LL);
kth(1, sz, K);
for (int i = (0); i < (int)(sz); i++) {
arr[i] += n - sz;
if (isLucky(n - sz + 1 + i, lucky) && isLucky(arr[i], lucky)) {
ans++;
}
}
printf("%lld\n", ans);
return 0;
}
| CPP |
122_E. Lucky Permutation | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long gao(int x, int c) {
long long ret = 0, g = 1;
for (; c--; g *= 10, x >>= 1) {
ret += g * ((x & 1) ? 7 : 4);
}
return ret;
}
vector<long long> v;
long long fct[16];
int c[16];
bool p[16];
bool lk(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
int i, j;
fct[0] = 1;
for (i = 1; i <= (15); ++i) fct[i] = fct[i - 1] * i;
for (i = 1; i < 12; ++i)
for (j = 0; j < 1 << i; ++j) v.push_back(gao(j, i));
sort(v.begin(), v.end());
long long n, k;
cin >> n >> k;
if (n <= 15 && k > fct[n]) {
puts("-1");
return 0;
}
int u;
long long h;
for (u = 0, h = 1; h < k; h *= (++u))
;
int ans = (n - u >= 4)
? (upper_bound(v.begin(), v.end(), n - u + 1) - v.begin())
: 0;
k--;
for (i = 0; i < u; ++i) {
for (j = 0, h = k / fct[u - i - 1] + 1;; ++j) {
if (!p[j]) --h;
if (h == 0) {
k %= fct[u - i - 1];
p[j] = true;
break;
}
}
c[i] = j;
}
for (int i = 0; i < u; ++i) {
if (lk(n - u + c[i] + 1) && lk(n - u + i + 1)) ++ans;
}
printf("%d\n", ans);
return 0;
}
| CPP |
Subsets and Splits