#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
#define REP(i, n) for (int i=0, ___=(n); i<___; ++i)
#define FOR(i, a, b) for (int i=(a), ___=(b); i<=___; ++i)
#define FORD(i, a, b) for (int i=(a), ___=(b); i>=___; --i)
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define mp make_pair
#define ST first
#define ND second
int read() { int n; scanf("%d", &n); return n; }
long long readl() { long long n; scanf("%lld", &n); return n; }
long long cnt(long long n) {
if (n > 61) n = 61;
return (3LL << n) - 3;
}
long long cnt2(long long n) {
if (n > 61) n = 61;
return (1LL << n) - 1;
}
void foo(long long n, long long k, char c) {
while (k > 1) {
--k;
long long all = cnt2(n);
if (k <= all) {
char cc = c == 'a' ? 'b' : 'a';
printf("%c", cc);
c = cc;
} else {
char cc = c == 'c' ? 'b' : 'c';
printf("%c", cc);
c = cc;
k -= all;
}
--n;
}
printf("\n");
}
void foo(long long n, long long k)
{
if (k <= n) {
while (k >= 2) { printf("ab"); k -= 2; }
if (k > 0) printf("a");
printf("\n");
return;
}
long long all = cnt(n);
if (k > all) {
printf("NIE\n");
return;
}
all /= 3;
if (k <= all) {
printf("a");
foo(n-1, k, 'a');
return;
}
if (k <= all*2) {
printf("b");
foo(n-1, k - all, 'b');
return;
}
printf("c");
foo(n-1, k - all - all, 'c');
}
void brut(long long n, long long k) {
vector<string> v;
v.push_back("");
v.push_back("a");
v.push_back("b");
v.push_back("c");
for (int i=2; i<=n; ++i) {
int n = v.size();
for (int j=0; j<n; ++j) if (v[j].size() == i-1) {
string s = v[j];
FOR(c, 'a', 'c') {
if (s[i-2] == c) continue;
string ss(s);
ss.push_back(c);
v.push_back(ss);
}
}
}
sort(v.begin(), v.end());
if (k >= v.size()) printf("NIE\n");
else printf("%s\n", v[k].c_str());
}
int main()
{
long long n = readl();
long long k = readl();
foo(n, k);
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 | #include <cstdio> #include <cstdlib> #include <cmath> #include <ctime> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <algorithm> using namespace std; #define REP(i, n) for (int i=0, ___=(n); i<___; ++i) #define FOR(i, a, b) for (int i=(a), ___=(b); i<=___; ++i) #define FORD(i, a, b) for (int i=(a), ___=(b); i>=___; --i) typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; #define mp make_pair #define ST first #define ND second int read() { int n; scanf("%d", &n); return n; } long long readl() { long long n; scanf("%lld", &n); return n; } long long cnt(long long n) { if (n > 61) n = 61; return (3LL << n) - 3; } long long cnt2(long long n) { if (n > 61) n = 61; return (1LL << n) - 1; } void foo(long long n, long long k, char c) { while (k > 1) { --k; long long all = cnt2(n); if (k <= all) { char cc = c == 'a' ? 'b' : 'a'; printf("%c", cc); c = cc; } else { char cc = c == 'c' ? 'b' : 'c'; printf("%c", cc); c = cc; k -= all; } --n; } printf("\n"); } void foo(long long n, long long k) { if (k <= n) { while (k >= 2) { printf("ab"); k -= 2; } if (k > 0) printf("a"); printf("\n"); return; } long long all = cnt(n); if (k > all) { printf("NIE\n"); return; } all /= 3; if (k <= all) { printf("a"); foo(n-1, k, 'a'); return; } if (k <= all*2) { printf("b"); foo(n-1, k - all, 'b'); return; } printf("c"); foo(n-1, k - all - all, 'c'); } void brut(long long n, long long k) { vector<string> v; v.push_back(""); v.push_back("a"); v.push_back("b"); v.push_back("c"); for (int i=2; i<=n; ++i) { int n = v.size(); for (int j=0; j<n; ++j) if (v[j].size() == i-1) { string s = v[j]; FOR(c, 'a', 'c') { if (s[i-2] == c) continue; string ss(s); ss.push_back(c); v.push_back(ss); } } } sort(v.begin(), v.end()); if (k >= v.size()) printf("NIE\n"); else printf("%s\n", v[k].c_str()); } int main() { long long n = readl(); long long k = readl(); foo(n, k); return 0; } |
English