// Krzysztof Małysa #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (int i = (a), i##__ = (n); i <= i##__; ++i) #define REP(i,n) FOR(i,0,n-1) #define FORD(i,a,n) for (int i = (a), i##__ = (n); i >= i##__; --i) #define ALL(x) x.begin(), x.end() #define EB emplace_back #define ST first #define ND second #define OO(A) template<class... T> ostream& operator<<(ostream& os, const A<T...>& x) { return __o(os, ALL(x)); } #define SZ(x) ((int)x.size()) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; template<class A, class B> ostream& operator<<(ostream&, const pair<A, B>&); template<class I> ostream& __o(ostream&, I, I); template<class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& x) { return __o(os, ALL(x)); } OO(vector) OO(deque) OO(set) OO(multiset) OO(map) OO(multimap) template<class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << "(" << p.ST << ", " << p.ND << ")"; } template<class I> ostream& __o(ostream& os, I a, I b) { os << "{"; for (; a != b;) os << *a++, cerr << (a == b ? "" : " "); return os << "}"; } template<class I> ostream& __d(ostream& os, I a, I b) { os << "{\n"; for (I c = a; a != b; ++a) os << " " << distance(c, a) << ": " << *a << endl; return os << "}"; } template<class... T> void __e(T&&... a) { int t[] = {(cerr << forward<T>(a), 0)...}; (void)t; cerr << endl; } template<class A, class B> void mini(A& a, B&& b) { if (b < a) a = b; } template<class A, class B> void maxi(A& a, B&& b) { if (b > a) a = b; } int ceil2(int x) { return 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); } #ifdef DEBUG # define D(...) __VA_ARGS__ #else # define D(...) #endif #define LOG(x) D(cerr << #x ": " << x) #define LOGN(x) D(LOG(x) << endl) #define DUMP(x) D(cerr << #x ": ", __d(cerr, ALL(x)) << endl) #define E(...) D(__e(__VA_ARGS__)) #define endl '\n' constexpr char nl = '\n'; // End of templates char lower(char last) { return (last == 'a' ? 'b' : 'a'); } char upper(char last) { return (last == 'c' ? 'b' : 'c'); } LL pow2[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976 }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; LL k; cin >> n >> k; // w[i] = 2**i string res; constexpr int MAX_POT2 = 60; if (n > MAX_POT2) { res = 'a'; while (--k, --n > MAX_POT2 && k) res += lower(res.back()); } if (res.empty()) { if (k < pow2[n]) { res = 'a'; --k; } else if (k < 2 * pow2[n] - 1) { res = 'b'; k -= pow2[n]; } else if (k < 3 * pow2[n] - 2) { k -= 2 * pow2[n] - 1; res = 'c'; } else return puts("NIE"), 0; --n; } LOGN(res); LOGN(n); while (n && k) { if (k < pow2[n]) { res += lower(res.back()); --k; } else { k -= pow2[n]; res += upper(res.back()); } --n; } LOGN(k); LOGN(res); assert(k == 0); cout << res << nl; 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 | // Krzysztof Małysa #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (int i = (a), i##__ = (n); i <= i##__; ++i) #define REP(i,n) FOR(i,0,n-1) #define FORD(i,a,n) for (int i = (a), i##__ = (n); i >= i##__; --i) #define ALL(x) x.begin(), x.end() #define EB emplace_back #define ST first #define ND second #define OO(A) template<class... T> ostream& operator<<(ostream& os, const A<T...>& x) { return __o(os, ALL(x)); } #define SZ(x) ((int)x.size()) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; template<class A, class B> ostream& operator<<(ostream&, const pair<A, B>&); template<class I> ostream& __o(ostream&, I, I); template<class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& x) { return __o(os, ALL(x)); } OO(vector) OO(deque) OO(set) OO(multiset) OO(map) OO(multimap) template<class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << "(" << p.ST << ", " << p.ND << ")"; } template<class I> ostream& __o(ostream& os, I a, I b) { os << "{"; for (; a != b;) os << *a++, cerr << (a == b ? "" : " "); return os << "}"; } template<class I> ostream& __d(ostream& os, I a, I b) { os << "{\n"; for (I c = a; a != b; ++a) os << " " << distance(c, a) << ": " << *a << endl; return os << "}"; } template<class... T> void __e(T&&... a) { int t[] = {(cerr << forward<T>(a), 0)...}; (void)t; cerr << endl; } template<class A, class B> void mini(A& a, B&& b) { if (b < a) a = b; } template<class A, class B> void maxi(A& a, B&& b) { if (b > a) a = b; } int ceil2(int x) { return 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); } #ifdef DEBUG # define D(...) __VA_ARGS__ #else # define D(...) #endif #define LOG(x) D(cerr << #x ": " << x) #define LOGN(x) D(LOG(x) << endl) #define DUMP(x) D(cerr << #x ": ", __d(cerr, ALL(x)) << endl) #define E(...) D(__e(__VA_ARGS__)) #define endl '\n' constexpr char nl = '\n'; // End of templates char lower(char last) { return (last == 'a' ? 'b' : 'a'); } char upper(char last) { return (last == 'c' ? 'b' : 'c'); } LL pow2[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976 }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; LL k; cin >> n >> k; // w[i] = 2**i string res; constexpr int MAX_POT2 = 60; if (n > MAX_POT2) { res = 'a'; while (--k, --n > MAX_POT2 && k) res += lower(res.back()); } if (res.empty()) { if (k < pow2[n]) { res = 'a'; --k; } else if (k < 2 * pow2[n] - 1) { res = 'b'; k -= pow2[n]; } else if (k < 3 * pow2[n] - 2) { k -= 2 * pow2[n] - 1; res = 'c'; } else return puts("NIE"), 0; --n; } LOGN(res); LOGN(n); while (n && k) { if (k < pow2[n]) { res += lower(res.back()); --k; } else { k -= pow2[n]; res += upper(res.back()); } --n; } LOGN(k); LOGN(res); assert(k == 0); cout << res << nl; return 0; } |