#include <iostream>
#include <string>
typedef unsigned long long int ull;
ull count(int n, ull* tab){
for(int i = 3; i <= n; i++){
tab[i] = (tab[i-1] - tab[i-2])*2 + tab[i-1];
}
//if(tab[n-1] == 0) count(n-2, tab)
return tab[n];
}
char getNext(char prev, int half){
char res;
switch(prev){
case 'a':
res = half ? 'c' : 'b';
break;
case 'b':
res = half ? 'c' : 'a';
break;
case 'c':
res = half ? 'b' : 'a';
break;
}
return res;
}
std::string getAtK(ull k, int n, ull* tab){
ull current_position = 0;
std::string word ="";
ull first = tab[n] / 3;
if( k == 0) return "";
if( k < first ) {
word = "a";
}
else if (k < (first * 2)){
word = "b";
k -= first;
}
else{
word = "c";
k -= 2*first;
}
n -= 2;
char last = word[0];
int half = 0;
ull border = first;
while(k > 0){
border = (border) / 2;
if(k >= border){
half = 1;
k -= border;
}
else{
half = 0;
}
if(k > 0) k--;
last = getNext(last, half);
word += last;
}
return word;
}
int main(){
std::ios_base::sync_with_stdio(false);
int n = 0;
ull k = 0;
std::cin>>n>>k;
ull* tab = new ull[n+1];
tab[1] = 3;
tab[2] = 9;
tab[3] = 21;
std::string result;
if(count(n, tab) < k){
result = "NIE";
}else{
result = getAtK(k, n, tab);
}
std::cout<<result;
delete[] tab;
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 | #include <iostream> #include <string> typedef unsigned long long int ull; ull count(int n, ull* tab){ for(int i = 3; i <= n; i++){ tab[i] = (tab[i-1] - tab[i-2])*2 + tab[i-1]; } //if(tab[n-1] == 0) count(n-2, tab) return tab[n]; } char getNext(char prev, int half){ char res; switch(prev){ case 'a': res = half ? 'c' : 'b'; break; case 'b': res = half ? 'c' : 'a'; break; case 'c': res = half ? 'b' : 'a'; break; } return res; } std::string getAtK(ull k, int n, ull* tab){ ull current_position = 0; std::string word =""; ull first = tab[n] / 3; if( k == 0) return ""; if( k < first ) { word = "a"; } else if (k < (first * 2)){ word = "b"; k -= first; } else{ word = "c"; k -= 2*first; } n -= 2; char last = word[0]; int half = 0; ull border = first; while(k > 0){ border = (border) / 2; if(k >= border){ half = 1; k -= border; } else{ half = 0; } if(k > 0) k--; last = getNext(last, half); word += last; } return word; } int main(){ std::ios_base::sync_with_stdio(false); int n = 0; ull k = 0; std::cin>>n>>k; ull* tab = new ull[n+1]; tab[1] = 3; tab[2] = 9; tab[3] = 21; std::string result; if(count(n, tab) < k){ result = "NIE"; }else{ result = getAtK(k, n, tab); } std::cout<<result; delete[] tab; return 0; } |
English