#include <cstdio> #include <iostream> using namespace std; typedef long long LL; #define REP(i,n) for (LL i = 0; i < n; ++i) #define FOR(i,a,b) for (LL i = a; i <= b; ++i) //#define DEBUG // x = dlog(n) // 2^(x-1) <= n <= 2^x - 1 LL dlog(LL n) { LL r = 0; while (n > 0) { r++; n /= 2; } return r; } LL pwr(LL n) { if (n == 0) return 1; else if (n % 2 == 1) { return 2 * pwr(n-1); } else { LL tmp = pwr(n/2); return tmp * tmp; } } char smaller (char x) { return (x == 'a') ? 'b' : 'a'; } char bigger (char x) { return (x == 'c') ? 'b' : 'c'; } // a <= 2^b bool mniejsze(LL a, LL b) { return dlog(a-1) <= b; } void fun(LL n, LL k) { LL d = dlog(k); LL d2 = dlog((k+1)/2); LL d3 = dlog((k+2)/3); LL x = k; char last = ' '; #ifdef DEBUG printf("\nd = %lld, d2 = %lld, d3 = %lld\n", d, d2, d3); #endif if (d <= n) { printf("a"); x = k - 1; last = 'a'; } else if (d2 <= n) { printf("b"); x = k - 1 - (pwr(n) - 1); last = 'b'; } else if (d3 <= n) { printf("c"); x = k - 1 - 2 * (pwr(n) - 1); last = 'c'; } else { printf("NIE\n"); return; } LL krok = 1; while (x > 0) { #ifdef DEBUG printf("dd = %lld\n", d); #endif if (mniejsze(x+1, n - krok)) { char next = smaller(last); printf("%c", next); last = next; x--; } else { char next = bigger(last); printf("%c", next); last = next; x = x - 1 - (pwr(n-krok) - 1); } krok++; } printf("\n"); } int main() { LL n, k; #ifdef DEBUG FOR(k,1,50) { //printf("%lld %lld\n", k, dlog(k)); printf("%lld ", k); fun(4, k); } #endif #ifndef DEBUG scanf("%lld %lld", &n, &k); fun(n, k); #endif 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 | #include <cstdio> #include <iostream> using namespace std; typedef long long LL; #define REP(i,n) for (LL i = 0; i < n; ++i) #define FOR(i,a,b) for (LL i = a; i <= b; ++i) //#define DEBUG // x = dlog(n) // 2^(x-1) <= n <= 2^x - 1 LL dlog(LL n) { LL r = 0; while (n > 0) { r++; n /= 2; } return r; } LL pwr(LL n) { if (n == 0) return 1; else if (n % 2 == 1) { return 2 * pwr(n-1); } else { LL tmp = pwr(n/2); return tmp * tmp; } } char smaller (char x) { return (x == 'a') ? 'b' : 'a'; } char bigger (char x) { return (x == 'c') ? 'b' : 'c'; } // a <= 2^b bool mniejsze(LL a, LL b) { return dlog(a-1) <= b; } void fun(LL n, LL k) { LL d = dlog(k); LL d2 = dlog((k+1)/2); LL d3 = dlog((k+2)/3); LL x = k; char last = ' '; #ifdef DEBUG printf("\nd = %lld, d2 = %lld, d3 = %lld\n", d, d2, d3); #endif if (d <= n) { printf("a"); x = k - 1; last = 'a'; } else if (d2 <= n) { printf("b"); x = k - 1 - (pwr(n) - 1); last = 'b'; } else if (d3 <= n) { printf("c"); x = k - 1 - 2 * (pwr(n) - 1); last = 'c'; } else { printf("NIE\n"); return; } LL krok = 1; while (x > 0) { #ifdef DEBUG printf("dd = %lld\n", d); #endif if (mniejsze(x+1, n - krok)) { char next = smaller(last); printf("%c", next); last = next; x--; } else { char next = bigger(last); printf("%c", next); last = next; x = x - 1 - (pwr(n-krok) - 1); } krok++; } printf("\n"); } int main() { LL n, k; #ifdef DEBUG FOR(k,1,50) { //printf("%lld %lld\n", k, dlog(k)); printf("%lld ", k); fun(4, k); } #endif #ifndef DEBUG scanf("%lld %lld", &n, &k); fun(n, k); #endif return 0; } |