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
//Patryk Kisielewski - PA 2016 - Slowo [SLO] - v2.0
#include <cstdio>
#define MAXN 60

using namespace std;

char findCharLeft(char);
char findCharRight(char);

int main(){
    int n;
    unsigned long long k;
    scanf("%d%llu", &n, &k);

    char current = 'n';
    unsigned long long current_index = 1;

    if(n > MAXN){
        current = 'a';
        current_index = n-MAXN+1;
        printf("%c", current);
        while(n-- > MAXN){
            current = findCharLeft(current);
            printf("%c", current);
        }
        n++;
    }

    unsigned long long sum = 1;
    unsigned long long tmp = 1;

    for(int i = 1; i < n; i++){
        tmp = tmp << 1;
        sum += tmp;
    }

    int first = (k-current_index+1)/sum;
    if((k-current_index+1)%sum == 0){
        first--;
    }

    if(first < 3){
        char arr[] = { 'a', 'b', 'c' };
        if(current == 'n'){
            current = arr[first];
            printf("%c", current);
            if(first > 0) k -= sum;
            if(first > 1) k -= sum;
        }
        while(sum > 1 && current_index != k){
            sum = sum >> 1;
            if(sum+current_index >= k){
                current = findCharLeft(current);
                current_index++;
            } else {
                current = findCharRight(current);
                current_index += sum+1;
            }
            printf("%c", current);
        }
        printf("\n");
    } else {
        printf("NIE\n");
    }
    return 0;
}

char findCharLeft(char current){
    switch(current){
    case 'a':
        return 'b';
    case 'b':
        return 'a';
    case 'c':
        return 'a';
    }
}

char findCharRight(char current){
    switch(current){
    case 'a':
        return 'c';
    case 'b':
        return 'c';
    case 'c':
        return 'b';
    }
}