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
/**
 * @brief Potyczki algorytmiczne 2016 runda pierwsza - Slowo.
 *
 * @author Michal Kawecki
 */

#include <stdio.h>
#include <string.h>

typedef unsigned long long int uint64;

bool incrementText(char* inText, int maxLength) {
	for(int c = 0; c < maxLength; c++) {
		if(inText[c] == 0) {
			inText[c] = 'a';
			return true;
		} else if(inText[c] == 'a') {
			inText[c] = 'b';
			return true;
		} else if(inText[c] == 'b') {
			inText[c] = 'c';
			return true;
		} else if(inText[c] == 'c') {
			inText[c] = 'a';
		}
    }
    return false;
}

bool isProperText(char* inText) {
	for(int c = 1; inText[c] != 0; c++) {
		if(inText[c-1] == inText[c]) {
			return false;
		}
    }
    return (inText[1] != 0);
}

int main()
{
    int n = 3;
    uint64 k = 7;

    scanf("%llu %llu", &n, &k);

    if(n < 2) {
    	printf("%s", "NIE");
		return 0;
    }

    char* longText = new char[n];
    memset(longText, 0, n);

    uint64 currentIndex = 0;

    for(; currentIndex <= k;) {
    	if(incrementText(longText, n) == true) {
    		//printf("%s\n", longText);
			if(isProperText(longText) == true) {
				currentIndex++;

				//printf("%llu - %s\n", currentIndex, longText);
			}
    	} else {
    		break;
    	}
    }

    if(currentIndex > k) {
		for(int t = strlen(longText) - 1; t >= 0; t--) {
			printf("%c", longText[t]);
		}
    } else {
    	printf("%s", "NIE");
    }

    delete [] longText;

    return 0;
}