#include <iostream> #include <cmath> using namespace std; char getLetterByIndex(int index) { return (char)(index + ((int)'a')); } int getIndexByLetter(char letter) { return ((int)letter) - ((int)'a'); } unsigned long long int integer_pow(long long int x, long long int n) { unsigned long long int r = 1; while (n--) r *= x; return r; } unsigned long long int getMaxWordsCount(long long int n) { return 3 * (integer_pow(2,n) - 1); // zredukowany wzor na sume ciagu geometrycznego gdzie q=2,n=n,a1=3 } unsigned long long int getMaxWordsCountWhenPrefixed(long long int n) { return (integer_pow(2,n) - 1); // zredukowany wzor na sume ciagu geometrycznego gdzie q=2,n=n,a1=3 } int slo2(long long int n, long long int k) { long long int currentK, currentHalf; char previousLetter = 'x'; char currentLetter; char letters[3][2]; letters[0][0] = 'b'; letters[0][1] = 'c'; letters[1][0] = 'a'; letters[1][1] = 'c'; letters[2][0] = 'a'; letters[2][1] = 'b'; /*cout << "getMaxWordsCount(n = 63) " << getMaxWordsCount(63) << "\n"; cout << "getMaxWordsCount(n = 62) " << getMaxWordsCount(62) << "\n"; cout << "getMaxWordsCount(n = 61) " << getMaxWordsCount(61) << "\n"; cout << "getMaxWordsCount(n = 60) " << getMaxWordsCount(60) << "\n"; cout << "getMaxWordsCount(n = 59) " << getMaxWordsCount(59) << "\n"; cout << "getMaxWordsCount(n = 58) " << getMaxWordsCount(58) << "\n"; cout << "getMaxWordsCount(n = 57) " << getMaxWordsCount(57) << "\n"; */ /* getMaxWordsCount(n = 63) 9223372036854775805 <- przepelnienie getMaxWordsCount(n = 62) 13835058055282163709 getMaxWordsCount(n = 61) 6917529027641081853 getMaxWordsCount(n = 60) 3458764513820540925 getMaxWordsCount(n = 59) 1729382256910270461 getMaxWordsCount(n = 58) 864691128455135229 getMaxWordsCount(n = 57) 432345564227567613 --------------------------------------------- 10^18 1000000000000000000 (czyli maksymalne dane k) */ long long int maxWordsCount, oneThirdOfMax, halfOfMax; long long int tmp = 0; long long int powerLimit = 59; // should be 59 if (n < powerLimit) { // n powyzej 59 ma wiecej slow niz dla maksymalnego danego k (10^18) - patrz wyniki wyzej maxWordsCount = getMaxWordsCount(n); //cout << "maxWordsCount: " << maxWordsCount << "\n"; if (k > maxWordsCount) { cout << "NIE"; return 0; } oneThirdOfMax = maxWordsCount / 3; //cout << "ceil((float)k / oneThirdOfMax) -1 -> " << ceil((float)k / oneThirdOfMax) -1 << "\n"; //cout << "((int)ceil(k / oneThirdOfMax) - 1) -> " << ((int)ceil(k / oneThirdOfMax) - 1) << "\n"; //cout << (char)((int)'a' + ceil(k / oneThirdOfMax) - 1) << "\n"; previousLetter = getLetterByIndex((int)ceil((float)k / oneThirdOfMax) - 1); //cout << "\n"; cout << previousLetter; // print first letter currentK = (k-1) % oneThirdOfMax; } else { tmp = n; while (n >= powerLimit) { previousLetter = (previousLetter == 'a') ? 'b' : 'a'; //cout << "\n"; cout << previousLetter; n--; k--; //if (k == 0) { // break; //} } if (n < tmp) { n++; k++; } currentK = k-1; } while (n > 1) { //cout << "currentK: " << currentK << "\n"; if (currentK == 0) { return 0; } maxWordsCount = getMaxWordsCountWhenPrefixed(n); halfOfMax = (long long int)floor((maxWordsCount-1) / 2); currentHalf = (currentK <= halfOfMax) ? 0 : 1; /*cout << "previousLetter: " << previousLetter << "\n"; cout << "maxWordsCount: " << maxWordsCount << "\n"; cout << "halfOfMax: " << halfOfMax << "\n"; cout << "currentHalf: " << currentHalf << "\n"; cout << "getIndexByLetter(previousLetter): " << getIndexByLetter(previousLetter) << "\n"; */ currentLetter = letters[getIndexByLetter(previousLetter)][currentHalf]; //cout << "\n"; cout << currentLetter; previousLetter = currentLetter; //maxWordsCount = oneThirdOfMax; //oneThirdOfMax = (long long int)((maxWordsCount-1)/2); //currentK = (currentK-1) % oneThirdOfMax; currentK = (currentK-1) % halfOfMax; n--; } return 0; } int main() { long long int n; long long int k; cin >> n; cin >> k; long long int i = k; while (i<=k) { slo2(n, i); //cout << " " << i << "\n"; i++; } return 0; }
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 | #include <iostream> #include <cmath> using namespace std; char getLetterByIndex(int index) { return (char)(index + ((int)'a')); } int getIndexByLetter(char letter) { return ((int)letter) - ((int)'a'); } unsigned long long int integer_pow(long long int x, long long int n) { unsigned long long int r = 1; while (n--) r *= x; return r; } unsigned long long int getMaxWordsCount(long long int n) { return 3 * (integer_pow(2,n) - 1); // zredukowany wzor na sume ciagu geometrycznego gdzie q=2,n=n,a1=3 } unsigned long long int getMaxWordsCountWhenPrefixed(long long int n) { return (integer_pow(2,n) - 1); // zredukowany wzor na sume ciagu geometrycznego gdzie q=2,n=n,a1=3 } int slo2(long long int n, long long int k) { long long int currentK, currentHalf; char previousLetter = 'x'; char currentLetter; char letters[3][2]; letters[0][0] = 'b'; letters[0][1] = 'c'; letters[1][0] = 'a'; letters[1][1] = 'c'; letters[2][0] = 'a'; letters[2][1] = 'b'; /*cout << "getMaxWordsCount(n = 63) " << getMaxWordsCount(63) << "\n"; cout << "getMaxWordsCount(n = 62) " << getMaxWordsCount(62) << "\n"; cout << "getMaxWordsCount(n = 61) " << getMaxWordsCount(61) << "\n"; cout << "getMaxWordsCount(n = 60) " << getMaxWordsCount(60) << "\n"; cout << "getMaxWordsCount(n = 59) " << getMaxWordsCount(59) << "\n"; cout << "getMaxWordsCount(n = 58) " << getMaxWordsCount(58) << "\n"; cout << "getMaxWordsCount(n = 57) " << getMaxWordsCount(57) << "\n"; */ /* getMaxWordsCount(n = 63) 9223372036854775805 <- przepelnienie getMaxWordsCount(n = 62) 13835058055282163709 getMaxWordsCount(n = 61) 6917529027641081853 getMaxWordsCount(n = 60) 3458764513820540925 getMaxWordsCount(n = 59) 1729382256910270461 getMaxWordsCount(n = 58) 864691128455135229 getMaxWordsCount(n = 57) 432345564227567613 --------------------------------------------- 10^18 1000000000000000000 (czyli maksymalne dane k) */ long long int maxWordsCount, oneThirdOfMax, halfOfMax; long long int tmp = 0; long long int powerLimit = 59; // should be 59 if (n < powerLimit) { // n powyzej 59 ma wiecej slow niz dla maksymalnego danego k (10^18) - patrz wyniki wyzej maxWordsCount = getMaxWordsCount(n); //cout << "maxWordsCount: " << maxWordsCount << "\n"; if (k > maxWordsCount) { cout << "NIE"; return 0; } oneThirdOfMax = maxWordsCount / 3; //cout << "ceil((float)k / oneThirdOfMax) -1 -> " << ceil((float)k / oneThirdOfMax) -1 << "\n"; //cout << "((int)ceil(k / oneThirdOfMax) - 1) -> " << ((int)ceil(k / oneThirdOfMax) - 1) << "\n"; //cout << (char)((int)'a' + ceil(k / oneThirdOfMax) - 1) << "\n"; previousLetter = getLetterByIndex((int)ceil((float)k / oneThirdOfMax) - 1); //cout << "\n"; cout << previousLetter; // print first letter currentK = (k-1) % oneThirdOfMax; } else { tmp = n; while (n >= powerLimit) { previousLetter = (previousLetter == 'a') ? 'b' : 'a'; //cout << "\n"; cout << previousLetter; n--; k--; //if (k == 0) { // break; //} } if (n < tmp) { n++; k++; } currentK = k-1; } while (n > 1) { //cout << "currentK: " << currentK << "\n"; if (currentK == 0) { return 0; } maxWordsCount = getMaxWordsCountWhenPrefixed(n); halfOfMax = (long long int)floor((maxWordsCount-1) / 2); currentHalf = (currentK <= halfOfMax) ? 0 : 1; /*cout << "previousLetter: " << previousLetter << "\n"; cout << "maxWordsCount: " << maxWordsCount << "\n"; cout << "halfOfMax: " << halfOfMax << "\n"; cout << "currentHalf: " << currentHalf << "\n"; cout << "getIndexByLetter(previousLetter): " << getIndexByLetter(previousLetter) << "\n"; */ currentLetter = letters[getIndexByLetter(previousLetter)][currentHalf]; //cout << "\n"; cout << currentLetter; previousLetter = currentLetter; //maxWordsCount = oneThirdOfMax; //oneThirdOfMax = (long long int)((maxWordsCount-1)/2); //currentK = (currentK-1) % oneThirdOfMax; currentK = (currentK-1) % halfOfMax; n--; } return 0; } int main() { long long int n; long long int k; cin >> n; cin >> k; long long int i = k; while (i<=k) { slo2(n, i); //cout << " " << i << "\n"; i++; } return 0; } |