import java.util.*; /** * Created by tom on 2016-11-15. */ public class slo { /** * Including the empty word "". Assuming first letter is set. */ private static long getNumberOfWords(long maxLength) { return (1L << Math.min(maxLength, 60)) - 1; } private static String getKthWord(char prevLetter, long maxLength, long k) { StringBuilder sb = new StringBuilder(""); while (k > 0) { long numberOfWords = getNumberOfWords(maxLength); if (k <= numberOfWords) { prevLetter = ('a' == prevLetter) ? 'b' : 'a'; } else if (k <= 2 * numberOfWords) { k -= numberOfWords; prevLetter = ('a' == prevLetter || 'b' == prevLetter) ? 'c' : 'b'; } else if (k <= 3 * numberOfWords) { k -= 2 * numberOfWords; prevLetter = 'c'; } else { return "NIE"; } sb.append(prevLetter); maxLength--; k--; } return sb.toString(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextLong(); long k = in.nextLong(); System.out.println(slo.getKthWord('x', n, k)); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import java.util.*; /** * Created by tom on 2016-11-15. */ public class slo { /** * Including the empty word "". Assuming first letter is set. */ private static long getNumberOfWords(long maxLength) { return (1L << Math.min(maxLength, 60)) - 1; } private static String getKthWord(char prevLetter, long maxLength, long k) { StringBuilder sb = new StringBuilder(""); while (k > 0) { long numberOfWords = getNumberOfWords(maxLength); if (k <= numberOfWords) { prevLetter = ('a' == prevLetter) ? 'b' : 'a'; } else if (k <= 2 * numberOfWords) { k -= numberOfWords; prevLetter = ('a' == prevLetter || 'b' == prevLetter) ? 'c' : 'b'; } else if (k <= 3 * numberOfWords) { k -= 2 * numberOfWords; prevLetter = 'c'; } else { return "NIE"; } sb.append(prevLetter); maxLength--; k--; } return sb.toString(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextLong(); long k = in.nextLong(); System.out.println(slo.getKthWord('x', n, k)); } } |