|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + |
| 8 | + static int[][] parents; |
| 9 | + static int[] depths; |
| 10 | + static ArrayList<Integer>[] lists; |
| 11 | + static int N; |
| 12 | + static int logN; |
| 13 | + public static void main(String[] args) throws Exception { |
| 14 | + |
| 15 | + int N = read(); |
| 16 | + |
| 17 | + int size = 0; |
| 18 | + |
| 19 | + int sum = 0; |
| 20 | + int nxt = 2; |
| 21 | + int before = 0; |
| 22 | + while(sum < N) { |
| 23 | + before = sum; |
| 24 | + sum += nxt; |
| 25 | + nxt <<= 1; |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + nxt >>= 1; |
| 30 | + StringBuilder sb = new StringBuilder(); |
| 31 | + while(nxt > 1) { |
| 32 | + nxt >>= 1; |
| 33 | + |
| 34 | + System.out.println(before + " " + nxt); |
| 35 | + if(before + nxt < N) { |
| 36 | + sb.append('7'); |
| 37 | + N -= nxt; |
| 38 | + } else { |
| 39 | + sb.append('4'); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + System.out.println(sb); |
| 44 | + } |
| 45 | + |
| 46 | + private static int lca(int a, int b) { |
| 47 | + |
| 48 | + int da = depths[a]; |
| 49 | + int db = depths[b]; |
| 50 | + |
| 51 | + |
| 52 | + System.out.println("aaa" + " " + a + " " + b); |
| 53 | + |
| 54 | + if(da < db) { |
| 55 | + b = calculateDiff(b, db - da); |
| 56 | + } else { |
| 57 | + a = calculateDiff(a, da - db); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + System.out.println("bbb" + " " + a + " " + b); |
| 62 | + |
| 63 | + if(a == b) return a; |
| 64 | + |
| 65 | + for(int i = logN; i >= 0; i--) { |
| 66 | + |
| 67 | + int ka = parents[a][i]; |
| 68 | + int kb = parents[b][i]; |
| 69 | + |
| 70 | + if(ka == kb) continue; |
| 71 | + |
| 72 | + a = ka; |
| 73 | + b = kb; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + return parents[a][0]; |
| 78 | + } |
| 79 | + |
| 80 | + private static int calculateDiff(int a, int diff) { |
| 81 | + |
| 82 | + for(int i = 0; (1 << i) <= diff; i++) { |
| 83 | + |
| 84 | + int bit = 1 << i; |
| 85 | + |
| 86 | + if((diff | bit) != diff) continue; |
| 87 | + |
| 88 | + a = parents[a][i]; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + return a; |
| 93 | + } |
| 94 | + |
| 95 | + private static void dfs(int idx, int depth, int parent) { |
| 96 | + |
| 97 | + depths[idx] = depth; |
| 98 | + |
| 99 | + for(int i : lists[idx]) { |
| 100 | + if(parent == i) continue; |
| 101 | + parents[i][0] = idx; |
| 102 | + dfs(i, depth + 1, idx); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + private static int read() throws IOException { |
| 108 | + int d, o = 0; |
| 109 | + boolean negative = false; |
| 110 | + |
| 111 | + while ((d = System.in.read()) <= 32); |
| 112 | + |
| 113 | + if (d == '-') { |
| 114 | + negative = true; |
| 115 | + d = System.in.read(); |
| 116 | + } |
| 117 | + |
| 118 | + do { |
| 119 | + o = (o << 3) + (o << 1) + (d & 15); // o = o * 10 + (d - '0') |
| 120 | + } while ((d = System.in.read()) >= '0' && d <= '9'); |
| 121 | + |
| 122 | + return negative ? -o : o; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +``` |
0 commit comments