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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

//#define PA2016_TEST

#ifdef PA2016_TEST
#define ASSERT( condition ) if ( !( condition ) ) { __debugbreak(); }
#define PAUSE_CONSOLE system( "pause" );
#define DEBUG_ONLY( line ) line;
#else
#define ASSERT( condition )
#define PAUSE_CONSOLE 
#define DEBUG_ONLY( line )
#endif

typedef unsigned long long llu;

// Given node and direction, returns node's succesor in the direction.
char Transition( char node, int dir )
{
	// dir: 0 - left, 1 - right.
	switch( node )
	{
	case 'a': return dir ? 'c' : 'b';
	case 'b': return dir ? 'c' : 'a';
	case 'c': return dir ? 'b' : 'a';
	}

	return 0;
}

// Find wanted idx using segment tree approach, when traversing segment tree, fill output with wanted string.
const char* TraverseSegmentTreeDepthFirstAndFillOutput( llu wantedIdx, llu min, llu max, llu stepSoFar, char* output, char lastLetter )
{
	char prev = lastLetter;
	llu steps = stepSoFar;

	llu curr_min = min;
	llu curr_max = max;
	while( true )
	{
		++steps;

		// It is possible that wanted idx is current min, so search is ended.
		if( curr_min == wantedIdx )
			break;

		// Calculate left and right ranges...
		llu left_min = curr_min + 1;
		llu left_max = ( ( curr_max - curr_min ) / 2 ) + curr_min;
		llu right_min = left_max + 1;
		llu right_max = curr_max;
		
		// ...and check where wantedIdx fits in.
		if( ( wantedIdx >= left_min ) && ( wantedIdx <= left_max ) )
		{
			curr_min = left_min;
			curr_max = left_max;
			prev = Transition( prev, 0 );
			output[ steps ] = prev;
		}
		else
		{
			curr_min = right_min;
			curr_max = right_max;

			prev = Transition( prev, 1 );
			output[ steps ] = prev;
		}
	}

	output[ steps ] = 0;
	return output;
}

// Function return in what range ( of binary tree with DFS indexing ) wantedIdx fits in.
// Returns false if tree with given height cannot hold wantedidx.
bool FindRange( llu treeHeigh, llu wantedIdx, llu& out_min, llu& out_max )
{
	llu min = treeHeigh;
	llu max = treeHeigh;
	
	while( min > 0 )
	{
		if( ( wantedIdx >= min ) && ( wantedIdx <= max ) )
		{
			out_min = min;
			out_max = max;
			return true;
		}

		llu oldmin = min;
		llu oldmax = max;

		min = oldmin - 1;
		max = min + 2 * ( oldmax - oldmin + 1 );
	}

	out_min = min + 1;
	out_max = max / 2;

	return false;
}

const char* Solve( llu maxN, llu wantedIdx )
{
	llu min = maxN;
	llu max = maxN;
	char letter = 'a';
	bool founded = false;

	// 1. Finad what range wanted idx is in.
	// We have to do this way, because otherwise we can't represent range as llu ( like e.g. [ 0, 2^1000000 ] ).
	for( int i = 0; i < 3; ++i )
	{
		if( FindRange( maxN, wantedIdx, min, max ) )
		{
			founded = true;
			break;
		}

		++letter;
		wantedIdx = wantedIdx - max;
	}

	char* output = (char*)malloc( maxN + 1 );
	output[ maxN ] = 0; // cstring ending.

	if( !founded )
	{
		output[ 0 ] = 'N';
		output[ 1 ] = 'I';
		output[ 2 ] = 'E';
		output[ 3 ] = 0;
		return output;
	}

	// 2. Fill path to min node, cuz we know it in advance:
	for( llu i = 0; i < min; ++i )
	{
		output[ i ] = i % 2 == 0 ? letter : Transition( letter, 0 );
	}

	// 3. Perform segment tree search:
	TraverseSegmentTreeDepthFirstAndFillOutput( wantedIdx, min, max, min - 1, output, letter );

	return output;
}

void Test( llu maxN, llu wantedIdx, const char* wantet_result )
{
	const char* txt = Solve( maxN, wantedIdx );
	ASSERT( strcmp( wantet_result, txt ) == 0 );
	free( (void*)txt );
}

void Run( llu wantedIdx, llu maxN )
{
	const char* txt = Solve( maxN, wantedIdx );
	printf( "%s\n", txt );
	free( (void*)txt );
}

void TestAll()
{
	Test( 3, 7, "acb" );
	Test( 5, 1, "a" );
	Test( 5, 31, "acbcb" );
	Test( 5, 188888, "NIE" );
	Test( 3, 8, "b" );
	Test( 10, 678, "acacacaba" );
	Test( 21, 9999999999999, "NIE" );
	Test( 100, 6666, "ababababababababababababababababababababababababababababababababababababababababababababcbabcbacacbc" );
	Test( 666, 16121989, "abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababacbcbacacbcbcbcb" );
	Test( 66, 666666666666, "abababababababababababababacabcbacbabcbcababcbacbcababacacabcbcbca" );
	Test( 111, 999999999999999999, "ababababababababababababababababababababababababababcbacbcbababacacbacbacacbabcbcacabcbcacbabacbcbcbcbcbcacbacb" );
	Test( 999999, 999999999999999999, "" );
}

int main()
{
	DEBUG_ONLY( TestAll(); );

	llu wantedIdx = 0;
	llu maxN = 0;
	scanf( "%ld%ld", &maxN, &wantedIdx );

	Run( wantedIdx, maxN );

	PAUSE_CONSOLE;
	return 0;
}