#include <cstdio> #include <cmath> #include <string> using namespace std; #define INF 18446744073709551615 typedef unsigned long long ull; char sections[] = { 'a', 'b', 'c' }; ull sectionSize(int section, int n, int k) { n -= section; if (n > 64) return INF; return pow(2, n) - 1; } int whichSection(int depth, int n, int k, bool *first, char prev = ' ') { ull size = sectionSize(depth, n, k); if (depth == 0) { if (k <= size) { return 0; } if (k <= 2 * size) { return 1; } if (k <= 3 * size) { return 2; } return -1; } *first = k <= size; if (prev == sections[0]) { return k <= size ? 1 : 2; } else if (prev == sections[1]) { return k <= size ? 0 : 2; } else { return k <= size ? 0 : 1; } } int isFirstn(int isFirst) { if (isFirst) return 0; else return 1; } int main() { ull n, k; scanf("%llu %llu", &n, &k); string s = ""; bool isFirst; int section = whichSection(0, n, k, &isFirst); if (section < 0) { printf("NIE\n"); return 0; } s += sections[section]; int id = 0; k -= section * (sectionSize(id, n, k));k--; id++; while (k > 0) { section = whichSection(id, n, k, &isFirst, sections[section]); if (section < 0) { printf("NIE\n"); return 0; } s += sections[section]; k--; k -= isFirstn(isFirst) * (sectionSize(id, n, k)); id++; } printf("%s\n", s.c_str()); 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 | #include <cstdio> #include <cmath> #include <string> using namespace std; #define INF 18446744073709551615 typedef unsigned long long ull; char sections[] = { 'a', 'b', 'c' }; ull sectionSize(int section, int n, int k) { n -= section; if (n > 64) return INF; return pow(2, n) - 1; } int whichSection(int depth, int n, int k, bool *first, char prev = ' ') { ull size = sectionSize(depth, n, k); if (depth == 0) { if (k <= size) { return 0; } if (k <= 2 * size) { return 1; } if (k <= 3 * size) { return 2; } return -1; } *first = k <= size; if (prev == sections[0]) { return k <= size ? 1 : 2; } else if (prev == sections[1]) { return k <= size ? 0 : 2; } else { return k <= size ? 0 : 1; } } int isFirstn(int isFirst) { if (isFirst) return 0; else return 1; } int main() { ull n, k; scanf("%llu %llu", &n, &k); string s = ""; bool isFirst; int section = whichSection(0, n, k, &isFirst); if (section < 0) { printf("NIE\n"); return 0; } s += sections[section]; int id = 0; k -= section * (sectionSize(id, n, k));k--; id++; while (k > 0) { section = whichSection(id, n, k, &isFirst, sections[section]); if (section < 0) { printf("NIE\n"); return 0; } s += sections[section]; k--; k -= isFirstn(isFirst) * (sectionSize(id, n, k)); id++; } printf("%s\n", s.c_str()); return 0; } |