Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by IntelliJ IDEA.
- * User: Taras_Brzezinsky
- * Date: 10/27/11
- * Time: 5:55 PM
- * To change this template use File | Settings | File Templates.
- */
- import sun.misc.Sort;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.*;
- public class TaskC extends Thread {
- public TaskC(int type) {
- try {
- if (type == 0) {
- this.input = new BufferedReader(new InputStreamReader(System.in));
- this.output = new PrintWriter(System.out);
- } else {
- this.input = new BufferedReader(new FileReader(INPUT_FILE));
- this.output = new PrintWriter(OUTPUT_FILE);
- }
- this.setPriority(Thread.MAX_PRIORITY);
- } catch (Throwable e) {
- e.printStackTrace();
- System.exit(666);
- }
- }
- static boolean isLucky(int value) {
- char []now = Integer.toString(value).toCharArray();
- for (char c : now) {
- if (c != '4' && c != '7') {
- return false;
- }
- }
- return true;
- }
- static void generate(long now, long length) {
- if (length == 10) {
- return;
- }
- luckies.add(now * 10 + 4);
- luckies.add(now * 10 + 7);
- generate(now * 10 + 4, length + 1);
- generate(now * 10 + 7, length + 1);
- }
- //12 ! - max
- private void solve() throws Throwable {
- long facts[] = new long[14];
- facts[0] = 1;
- for (int i = 1; i < facts.length; ++i) {
- facts[i] = facts[i - 1] * i;
- }
- int n = nextInt();
- long k = nextLong();
- if (n <= 12) {
- if (facts[n] < k) {
- output.println(-1);
- return;
- }
- }
- int result = 0;
- int N = 1;
- while (facts[N] < k) {
- ++N;
- }
- long MAX = (long) n - N;
- int index = 0;
- while (index < values.size() && values.get(index) <= MAX) {
- ++result;
- ++index;
- }
- int values[] = new int[N];
- boolean used[] = new boolean[N];
- Arrays.fill(values, -1);
- for (int i = 0; i < N; ++i) {
- for (int wanted = 0; wanted < N; ++wanted) {
- if (used[wanted]) {
- continue;
- }
- long remaining = facts[N - 1 - i];
- if (remaining < k) {
- k -= remaining;
- } else {
- values[i] = wanted + 1;
- used[wanted] = true;
- break;
- }
- }
- }
- for (int i = 0; i < N; ++i) {
- int pos = (int)MAX + i + 1;
- if (isLucky(pos) && isLucky((int) MAX + values[i])) {
- ++result;
- }
- }
- output.println(result);
- }
- public void run() {
- try {
- solve();
- } catch (Throwable e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- System.exit(666);
- } finally {
- output.flush();
- output.close();
- }
- }
- public static void main(String[] args) {
- new TaskC(0).start();
- }
- private int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- private long nextLong() throws IOException {
- return Long.parseLong(nextToken());
- }
- private double nextDouble() throws IOException {
- return Double.parseDouble(nextToken());
- }
- private String nextToken() throws IOException {
- while (tokens == null || !tokens.hasMoreTokens()) {
- tokens = new StringTokenizer(input.readLine());
- }
- return tokens.nextToken();
- }
- static ArrayList<Long> values = new ArrayList<Long>();
- static HashSet<Long> luckies = new HashSet<Long>();
- static {
- generate(0, 1);
- values.addAll(luckies);
- Collections.sort(values);
- }
- private static final String INPUT_FILE = null;
- private static final String OUTPUT_FILE = null;
- private BufferedReader input;
- private PrintWriter output;
- private StringTokenizer tokens = null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement