#include<stdio.h> #include<string.h> #include<limits> void printMin(int * last) { for (int i=0;i<3;i++) { if(!last[i]) { memset(last,0,3*sizeof(int)); last[i]=1; printf("%c",i+'a'); return; } } } void printMax(int * last) { for (int i=2;i>=0;i--) { if(!last[i]) { memset(last,0,3*sizeof(int)); last[i]=1; printf("%c",i+'a'); return; } } } int main() { unsigned long long n,k,max=1; scanf("%llu%llu",&n,&k); for (int i=0;i<n;i++) { max*=2; if(3*(max-1)>k)break; } max=3*(max-1); if(k>max) {printf("NIE\n"); return 0;} unsigned long long pot = 1,pot2; for (int i =0;i<n;i++) { pot*=2ULL; if(pot-1>=k) { break; } } pot2=pot; pot--; int last[3]={}; if(k<=pot) {printf("a"); last[0]=1;} else { pot*=2ULL; if(k<=pot) {printf("b"); last[1]=1; k-=(pot/2);} else { printf("c"); last[2]=1; k-=pot;} } k--; if(k==0) return 0; pot=1; for (int i =0;i<n;i++) { pot*=2; if(pot>=k) { pot=i+1; break; } } unsigned long long curPot = 0, curVal=0, pietro =n-1; const unsigned long long inf = std::numeric_limits<unsigned long long>::max(); while(curVal !=k) { unsigned long long leftVal = curVal+1,rightVal= inf; if(pietro<= pot) { rightVal = leftVal + (1ULL<<pietro)-1ULL; } else { curVal = leftVal; printMin(last); pietro--; continue; } if(k>=rightVal) { printMax(last); curVal = rightVal; } else { printMin(last); curVal = leftVal; } pietro--; } // printf("\n"); }
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 | #include<stdio.h> #include<string.h> #include<limits> void printMin(int * last) { for (int i=0;i<3;i++) { if(!last[i]) { memset(last,0,3*sizeof(int)); last[i]=1; printf("%c",i+'a'); return; } } } void printMax(int * last) { for (int i=2;i>=0;i--) { if(!last[i]) { memset(last,0,3*sizeof(int)); last[i]=1; printf("%c",i+'a'); return; } } } int main() { unsigned long long n,k,max=1; scanf("%llu%llu",&n,&k); for (int i=0;i<n;i++) { max*=2; if(3*(max-1)>k)break; } max=3*(max-1); if(k>max) {printf("NIE\n"); return 0;} unsigned long long pot = 1,pot2; for (int i =0;i<n;i++) { pot*=2ULL; if(pot-1>=k) { break; } } pot2=pot; pot--; int last[3]={}; if(k<=pot) {printf("a"); last[0]=1;} else { pot*=2ULL; if(k<=pot) {printf("b"); last[1]=1; k-=(pot/2);} else { printf("c"); last[2]=1; k-=pot;} } k--; if(k==0) return 0; pot=1; for (int i =0;i<n;i++) { pot*=2; if(pot>=k) { pot=i+1; break; } } unsigned long long curPot = 0, curVal=0, pietro =n-1; const unsigned long long inf = std::numeric_limits<unsigned long long>::max(); while(curVal !=k) { unsigned long long leftVal = curVal+1,rightVal= inf; if(pietro<= pot) { rightVal = leftVal + (1ULL<<pietro)-1ULL; } else { curVal = leftVal; printMin(last); pietro--; continue; } if(k>=rightVal) { printMax(last); curVal = rightVal; } else { printMin(last); curVal = leftVal; } pietro--; } // printf("\n"); } |