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
#include <iostream>
#include <cstdlib>
#include <vector>

typedef  unsigned long long ull_t;
using namespace std;

struct TreeNode {
	char key;
	ull_t depth;
	ull_t nodeNumber;
	TreeNode* left;
	TreeNode* right;
};

class Tree {
public:
	Tree(ull_t maxDepth, ull_t k);
	void print();

private:
	ull_t maxDepth;
	ull_t nodeNumber;
	ull_t currentNodeNumber;
	bool finished;
	vector<char> word;
	TreeNode* root;
	void insert (TreeNode* node);
};

Tree::Tree(ull_t maxDepth, ull_t nodeNumber) {
	this->maxDepth = maxDepth;
	this->nodeNumber = nodeNumber;
	this->finished = false;

	currentNodeNumber = 1;

	root = new TreeNode;
	root->key = 'a';
	root->depth = 1;
	root->nodeNumber = currentNodeNumber;
	root->left = NULL;
	root->right = NULL;

	if (maxDepth > 1)
		insert(root);
	else
		if (this->nodeNumber==1)
			this->finished=true;
}

void Tree::print() {

	if (this->finished) {
		word.push_back('a');
		for (vector<char>::iterator it=word.end()-1;it >= word.begin();it--)
			cout << *it;
	} else {
		cout << "NIE";
	}
}

void Tree::insert(TreeNode* node) {

	node->left = new TreeNode;
	node->left->depth = node->depth + 1;
	node->left->nodeNumber = ++currentNodeNumber;
	if (node->key == 'a') {
		node->left->key ='b';
	} else {
		node->left->key ='a';
	}

	if (node->left->nodeNumber==this->nodeNumber) {
		this->finished = true;

	} else {
		if (node->left->depth < maxDepth) {
			insert(node->left);
		}
	}

	if(this->finished) {
		this->word.push_back(node->left->key);
	} else {
		node->right = new TreeNode;
		node->right->depth = node->depth + 1;
		node->right->nodeNumber = ++currentNodeNumber;
		if (node->key == 'c') {
			node->right->key ='b';
		} else {
			node->right->key ='c';
		}

		if (node->right->nodeNumber==this->nodeNumber) {
			this->finished = true;
		} else {
			if (node->right->depth < maxDepth) {
				insert(node->right);
			}
		}
		if (this->finished)	this->word.push_back(node->right->key);
	}
}

int main(int argc, char *argv[]) {

	ull_t n = strtoll(argv[1],NULL, 10);
	ull_t k = strtoll(argv[2],NULL, 10);
	Tree tree = Tree(n,k);
	tree.print();
	return 0;
}