import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.BitSet; /** * Created by michal on 17.11.16. */ public class slo { private static int numbersOfbits(long x) { return Long.SIZE - Long.numberOfLeadingZeros(x); } private static int compareNumbers(BitSet a, BitSet b) { /* -1 if a < b 0 if a == b +1 if a > b */ int num_bits_a = a.length(); int num_bits_b = b.length(); if (num_bits_a < num_bits_b) { return -1; } else if(num_bits_a > num_bits_b) { return 1; } else { for (int i=num_bits_a; i >= 0; --i) { if(a.get(i) && !b.get(i)) { return 1; } if(!a.get(i) && b.get(i)) { return -1; } } } return 0; } private static BitSet add(BitSet a, BitSet b, boolean isBneg) { int a_len = a.length(); int b_len = b.length(); BitSet x; BitSet y; if (a_len < b_len) { x = b; y = a; } else { x = a; y = b; } BitSet result = new BitSet(a_len + 1); boolean over = false; for (int i=0,j=0; i<a_len; ++i, ++j) { if (x.get(i) != y.get(i)) { if (!over) { // 1 + 0 = 1 result.set(i); } } else if (x.get(i) && y.get(i)) { if (over) { // 1 + 1 +1 = 1 result.set(i); } else { // 1 + 1 = 0 over = true; } } else { if (over) { //1 + 0 + 0 = 1 result.set(i); over = false; } } } if (over && !isBneg) { result.set(a_len); } return result; } private static BitSet numToBitSet(long x) { int num_bits = numbersOfbits(x); BitSet bitSet = new BitSet(num_bits); for (int i = 0; i < num_bits; ++i) { if (x % 2 != 0) { bitSet.set(i); } x >>= 1; } return bitSet; } private static int calcLetter(int prev, boolean lower) { //lower == true => L //lower == false => R int c = 0; if (prev == 0) { //a if(lower) { c = 1; //b } else { c = 2; //c } } if (prev == 1) { //b if(lower) { c = 0; //a } else { c = 2; //c } } if (prev == 2) { //c if(lower) { c = 0; //a } else { c = 1; //b } } return c; } public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String[] values = input.readLine().split(" "); int n = Integer.parseInt(values[0]); //10^6 long k = Long.parseLong(values[1]); //10^18 BitSet s_bits = new BitSet(n); s_bits.set(0, n); BitSet k_bits = numToBitSet(k); int k_bits_num = k_bits.length(); BitSet s_bits_neg = new BitSet(k_bits_num); s_bits_neg.set(0); s_bits_neg.set(k_bits_num - 1); int begin = 0; while (compareNumbers(k_bits, s_bits) > 0) { begin += 1; k_bits = add(k_bits, s_bits_neg, true); } if (begin > 2) { System.out.print("NIE"); return; } else { System.out.print((char) (begin + 'a')); } while (k > 1) { s_bits.set(s_bits.length() - 1, false); //7 3 if (s_bits.length() == 1) { return; } int compare = compareNumbers(s_bits, k_bits); if (compare > 0) { begin = calcLetter(begin, true); //2* k_bits = add(k_bits, negate(s_bits, k_bits.length()), true); } else if (compare <= 0) { begin = calcLetter(begin, false); //1* } System.out.print((char) (begin + 'a')); --k; } } private static BitSet negate(BitSet s_bits, int length) { BitSet neg = new BitSet(length); boolean doNegate = false; for (int i=0; i<length; ++i) { if (s_bits.get(i)) { if(!doNegate) { doNegate = true; neg.set(i, true); } } else { if(doNegate) { neg.set(i, true); } } } return neg; } }
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.BitSet; /** * Created by michal on 17.11.16. */ public class slo { private static int numbersOfbits(long x) { return Long.SIZE - Long.numberOfLeadingZeros(x); } private static int compareNumbers(BitSet a, BitSet b) { /* -1 if a < b 0 if a == b +1 if a > b */ int num_bits_a = a.length(); int num_bits_b = b.length(); if (num_bits_a < num_bits_b) { return -1; } else if(num_bits_a > num_bits_b) { return 1; } else { for (int i=num_bits_a; i >= 0; --i) { if(a.get(i) && !b.get(i)) { return 1; } if(!a.get(i) && b.get(i)) { return -1; } } } return 0; } private static BitSet add(BitSet a, BitSet b, boolean isBneg) { int a_len = a.length(); int b_len = b.length(); BitSet x; BitSet y; if (a_len < b_len) { x = b; y = a; } else { x = a; y = b; } BitSet result = new BitSet(a_len + 1); boolean over = false; for (int i=0,j=0; i<a_len; ++i, ++j) { if (x.get(i) != y.get(i)) { if (!over) { // 1 + 0 = 1 result.set(i); } } else if (x.get(i) && y.get(i)) { if (over) { // 1 + 1 +1 = 1 result.set(i); } else { // 1 + 1 = 0 over = true; } } else { if (over) { //1 + 0 + 0 = 1 result.set(i); over = false; } } } if (over && !isBneg) { result.set(a_len); } return result; } private static BitSet numToBitSet(long x) { int num_bits = numbersOfbits(x); BitSet bitSet = new BitSet(num_bits); for (int i = 0; i < num_bits; ++i) { if (x % 2 != 0) { bitSet.set(i); } x >>= 1; } return bitSet; } private static int calcLetter(int prev, boolean lower) { //lower == true => L //lower == false => R int c = 0; if (prev == 0) { //a if(lower) { c = 1; //b } else { c = 2; //c } } if (prev == 1) { //b if(lower) { c = 0; //a } else { c = 2; //c } } if (prev == 2) { //c if(lower) { c = 0; //a } else { c = 1; //b } } return c; } public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String[] values = input.readLine().split(" "); int n = Integer.parseInt(values[0]); //10^6 long k = Long.parseLong(values[1]); //10^18 BitSet s_bits = new BitSet(n); s_bits.set(0, n); BitSet k_bits = numToBitSet(k); int k_bits_num = k_bits.length(); BitSet s_bits_neg = new BitSet(k_bits_num); s_bits_neg.set(0); s_bits_neg.set(k_bits_num - 1); int begin = 0; while (compareNumbers(k_bits, s_bits) > 0) { begin += 1; k_bits = add(k_bits, s_bits_neg, true); } if (begin > 2) { System.out.print("NIE"); return; } else { System.out.print((char) (begin + 'a')); } while (k > 1) { s_bits.set(s_bits.length() - 1, false); //7 3 if (s_bits.length() == 1) { return; } int compare = compareNumbers(s_bits, k_bits); if (compare > 0) { begin = calcLetter(begin, true); //2* k_bits = add(k_bits, negate(s_bits, k_bits.length()), true); } else if (compare <= 0) { begin = calcLetter(begin, false); //1* } System.out.print((char) (begin + 'a')); --k; } } private static BitSet negate(BitSet s_bits, int length) { BitSet neg = new BitSet(length); boolean doNegate = false; for (int i=0; i<length; ++i) { if (s_bits.get(i)) { if(!doNegate) { doNegate = true; neg.set(i, true); } } else { if(doNegate) { neg.set(i, true); } } } return neg; } } |