Advertisement
Guest User

Untitled

a guest
Oct 27th, 2011
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. /**
  2.  * Created by IntelliJ IDEA.
  3.  * User: Taras_Brzezinsky
  4.  * Date: 10/27/11
  5.  * Time: 5:55 PM
  6.  * To change this template use File | Settings | File Templates.
  7.  */
  8.  
  9. import sun.misc.Sort;
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.InputStreamReader;
  13. import java.io.PrintWriter;
  14. import java.io.FileReader;
  15. import java.io.IOException;
  16. import java.util.*;
  17.  
  18. public class TaskC extends Thread {
  19.     public TaskC(int type) {
  20.         try {
  21.             if (type == 0) {
  22.                 this.input = new BufferedReader(new InputStreamReader(System.in));
  23.                 this.output = new PrintWriter(System.out);
  24.             } else {
  25.                 this.input = new BufferedReader(new FileReader(INPUT_FILE));
  26.                 this.output = new PrintWriter(OUTPUT_FILE);
  27.             }
  28.             this.setPriority(Thread.MAX_PRIORITY);
  29.         } catch (Throwable e) {
  30.             e.printStackTrace();
  31.             System.exit(666);
  32.         }
  33.     }
  34.  
  35.     static boolean isLucky(int value) {
  36.         char []now = Integer.toString(value).toCharArray();
  37.         for (char c : now) {
  38.             if (c != '4' && c != '7') {
  39.                 return false;
  40.             }
  41.         }
  42.         return true;
  43.     }
  44.  
  45.     static void generate(long now, long length) {
  46.         if (length == 10) {
  47.             return;
  48.         }
  49.         luckies.add(now * 10 + 4);
  50.         luckies.add(now * 10 + 7);
  51.         generate(now * 10 + 4, length + 1);
  52.         generate(now * 10 + 7, length + 1);
  53.     }
  54.  
  55.     //12 ! - max
  56.     private void solve() throws Throwable {
  57.         long facts[] = new long[14];
  58.         facts[0] = 1;
  59.         for (int i = 1; i < facts.length; ++i) {
  60.             facts[i] = facts[i - 1] * i;
  61.         }
  62.         int n = nextInt();
  63.         long k = nextLong();
  64.         if (n <= 12) {
  65.             if (facts[n] < k) {
  66.                 output.println(-1);
  67.                 return;
  68.             }
  69.         }
  70.         int result = 0;
  71.  
  72.         int N = 1;
  73.         while (facts[N] < k) {
  74.             ++N;
  75.         }
  76.         long MAX = (long) n - N;
  77.         int index = 0;
  78.         while (index < values.size() && values.get(index) <= MAX) {
  79.             ++result;
  80.             ++index;
  81.         }
  82.  
  83.         int values[] = new int[N];
  84.         boolean used[] = new boolean[N];
  85.         Arrays.fill(values, -1);
  86.         for (int i = 0; i < N; ++i) {
  87.             for (int wanted = 0; wanted < N; ++wanted) {
  88.                 if (used[wanted]) {
  89.                     continue;
  90.                 }
  91.                 long remaining = facts[N - 1 - i];
  92.                 if (remaining < k) {
  93.                     k -= remaining;
  94.                 } else {
  95.                     values[i] = wanted + 1;
  96.                     used[wanted] = true;
  97.                     break;
  98.                 }
  99.             }
  100.         }
  101.         for (int i = 0; i < N; ++i) {
  102.             int pos = (int)MAX + i + 1;
  103.             if (isLucky(pos) && isLucky((int) MAX + values[i])) {
  104.                 ++result;
  105.             }
  106.         }
  107.         output.println(result);
  108.  
  109.     }
  110.  
  111.     public void run() {
  112.         try {
  113.             solve();
  114.         } catch (Throwable e) {
  115.             System.err.println(e.getMessage());
  116.             e.printStackTrace();
  117.             System.exit(666);
  118.         } finally {
  119.             output.flush();
  120.             output.close();
  121.         }
  122.     }
  123.  
  124.     public static void main(String[] args) {
  125.         new TaskC(0).start();
  126.     }
  127.  
  128.     private int nextInt() throws IOException {
  129.         return Integer.parseInt(nextToken());
  130.     }
  131.  
  132.     private long nextLong() throws IOException {
  133.         return Long.parseLong(nextToken());
  134.     }
  135.  
  136.     private double nextDouble() throws IOException {
  137.         return Double.parseDouble(nextToken());
  138.     }
  139.  
  140.     private String nextToken() throws IOException {
  141.         while (tokens == null || !tokens.hasMoreTokens()) {
  142.             tokens = new StringTokenizer(input.readLine());
  143.         }
  144.         return tokens.nextToken();
  145.     }
  146.  
  147.     static ArrayList<Long> values = new ArrayList<Long>();
  148.     static HashSet<Long> luckies = new HashSet<Long>();
  149.  
  150.     static {
  151.         generate(0, 1);
  152.         values.addAll(luckies);
  153.         Collections.sort(values);
  154.     }
  155.  
  156.     private static final String INPUT_FILE = null;
  157.     private static final String OUTPUT_FILE = null;
  158.     private BufferedReader input;
  159.     private PrintWriter output;
  160.     private StringTokenizer tokens = null;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement