#include <cstdio>
char decision[6] = {'b', 'c', 'a', 'c', 'a', 'b'};
int decision_aux(char letter) {
return (((int)letter - (int)'a') << 1);
}
char found_word[62];
int main() {
int n;
long long k;
scanf("%d %lld", &n, &k);
if (n == 1) {
if (k <= 3)
printf("%c\n", 'a' + (char)(k - 1));
else
printf("NIE\n");
return 0;
}
if ((long long)n >= k) {
for (int new_k = (int)k, i = 0; i < new_k; ++i) {
putchar('a');
if (++i < new_k)
putchar('b');
else
break;
}
putchar('\n');
return 0;
}
int start_pos = ((n < 61) ? n : 61), where = start_pos;
long long current = (long long)n, remainder = 1LL;
while (current + remainder < k) {
current += remainder;
remainder = (remainder << 1) + 1;
if (--where == 1) {
if (current + remainder >= k) {
found_word[1] = 'b';
break;
}
if (current + (remainder << 1) >= k) {
current += remainder;
found_word[1] = 'c';
break;
}
printf("NIE\n");
return 0;
}
}
int where_end = where - 1, where_to = n - (start_pos - where_end);
if (where > 1)
found_word[where] = 'c';
while (++current < k) {
++where;
remainder >>= 1;
if (current + remainder < k) {
current += remainder;
found_word[where] = decision[decision_aux(found_word[where - 1]) + 1];
}
else
found_word[where] = decision[decision_aux(found_word[where - 1])];
}
for (int i = 0; i < where_to; ++i) {
putchar('a');
if (++i < where_to)
putchar('b');
else
break;
}
while (found_word[++where_end])
putchar(found_word[where_end]);
putchar('\n');
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 | #include <cstdio> char decision[6] = {'b', 'c', 'a', 'c', 'a', 'b'}; int decision_aux(char letter) { return (((int)letter - (int)'a') << 1); } char found_word[62]; int main() { int n; long long k; scanf("%d %lld", &n, &k); if (n == 1) { if (k <= 3) printf("%c\n", 'a' + (char)(k - 1)); else printf("NIE\n"); return 0; } if ((long long)n >= k) { for (int new_k = (int)k, i = 0; i < new_k; ++i) { putchar('a'); if (++i < new_k) putchar('b'); else break; } putchar('\n'); return 0; } int start_pos = ((n < 61) ? n : 61), where = start_pos; long long current = (long long)n, remainder = 1LL; while (current + remainder < k) { current += remainder; remainder = (remainder << 1) + 1; if (--where == 1) { if (current + remainder >= k) { found_word[1] = 'b'; break; } if (current + (remainder << 1) >= k) { current += remainder; found_word[1] = 'c'; break; } printf("NIE\n"); return 0; } } int where_end = where - 1, where_to = n - (start_pos - where_end); if (where > 1) found_word[where] = 'c'; while (++current < k) { ++where; remainder >>= 1; if (current + remainder < k) { current += remainder; found_word[where] = decision[decision_aux(found_word[where - 1]) + 1]; } else found_word[where] = decision[decision_aux(found_word[where - 1])]; } for (int i = 0; i < where_to; ++i) { putchar('a'); if (++i < where_to) putchar('b'); else break; } while (found_word[++where_end]) putchar(found_word[where_end]); putchar('\n'); return 0; } |
English