#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
stringstream out;
char characters[] = { 'a', 'b', 'c' };
void getNextCharacter(int &previous, int number)
{
if (previous == 0)
{
if (number == 0)
{
out << 'b';
previous = 1;
}
else
{
out << 'c';
previous = 2;
}
}
else if (previous == 1)
{
if (number == 0)
{
out << 'a';
previous = 0;
}
else
{
out << 'c';
previous = 2;
}
}
else if (previous == 2)
{
if (number == 0)
{
out << 'a';
previous = 0;
}
else
{
out << 'b';
previous = 1;
}
}
else
{
if (number == 0)
out << 'a';
else if (number == 1)
out << 'b';
else
out << 'c';
previous = number;
}
}
int main()
{
ios_base::sync_with_stdio(false);
long long n, k;
int previous = -1;
cin >> n >> k;
k--;
for (int i = n; i > 0; i--)
{
if (i > 60)
{
getNextCharacter(previous, 0);
k--;
if (k < 0)
break;
}
else
{
long long divisor = (1LL << i) - 1;
long long modulo = k % divisor;
k /= divisor;
if (k > 2)
{
out.clear();
out << "NIE";
break;
}
getNextCharacter(previous, k);
k = modulo - 1;
if (k < 0)
break;
}
}
cout << out.str() << endl;
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 | #include <iostream> #include <cmath> #include <sstream> using namespace std; stringstream out; char characters[] = { 'a', 'b', 'c' }; void getNextCharacter(int &previous, int number) { if (previous == 0) { if (number == 0) { out << 'b'; previous = 1; } else { out << 'c'; previous = 2; } } else if (previous == 1) { if (number == 0) { out << 'a'; previous = 0; } else { out << 'c'; previous = 2; } } else if (previous == 2) { if (number == 0) { out << 'a'; previous = 0; } else { out << 'b'; previous = 1; } } else { if (number == 0) out << 'a'; else if (number == 1) out << 'b'; else out << 'c'; previous = number; } } int main() { ios_base::sync_with_stdio(false); long long n, k; int previous = -1; cin >> n >> k; k--; for (int i = n; i > 0; i--) { if (i > 60) { getNextCharacter(previous, 0); k--; if (k < 0) break; } else { long long divisor = (1LL << i) - 1; long long modulo = k % divisor; k /= divisor; if (k > 2) { out.clear(); out << "NIE"; break; } getNextCharacter(previous, k); k = modulo - 1; if (k < 0) break; } } cout << out.str() << endl; return 0; } |
English