#include<bits/stdc++.h> #include<cstdio> using namespace std; #define R(p, k) for(int i = p; i <= k; i++) #define RI(i, p, k) for(int i = p; i <= k; i++) #define IR(k, p) for(int i = k; i >= p; i--) #define IRI(i, p, k) for(int i = k; i >= p; i--) #define DEB 0 #if DEB #define EPRLL(a) fprintf(stderr, "%s %lld ", #a, a); #define EPRN fprintf(stderr, "\n"); #else #define EPRLL(a) #define EPRN #endif const int MN = 1e6+6, al=3; long long dp_len[MN], dp_up_to[MN]; long long n, k; vector<char> result; int main(){ long long i, j, a, b; scanf("%lld%lld", &n, &k); dp_len[0]=1; dp_len[1]=al-1; dp_up_to[0]=1; dp_up_to[1]=al; R(2, n){ dp_len[i]=dp_len[i-1]*(al-1); dp_len[i]=min(dp_len[i], (long long)2e18); dp_up_to[i]=dp_up_to[i-1]+dp_len[i]; dp_up_to[i]=min(dp_up_to[i], (long long)2e18); } R(1, n){ EPRLL(dp_len[i]); } EPRN R(1, n){ EPRLL(dp_up_to[i]); } EPRN k++; char last = 'a'+al; IR(n, 1){ if(k==1){ result.push_back(' '); break; } k--; RI(j, 0, (al - 1)) { if(last != 'a'+j){ if(dp_up_to[i-1] < k) k-=dp_up_to[i-1]; else{ result.push_back('a'+j); last='a'+j; break; } } } if((signed)result.size() != n-i+1){ printf("NIE\n"); return 0; } } /*RI(j, 0, 5) printf("%d ", j);*/ R(0, (signed)result.size()-1) printf("%c", result[i]); printf("\n"); 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 | #include<bits/stdc++.h> #include<cstdio> using namespace std; #define R(p, k) for(int i = p; i <= k; i++) #define RI(i, p, k) for(int i = p; i <= k; i++) #define IR(k, p) for(int i = k; i >= p; i--) #define IRI(i, p, k) for(int i = k; i >= p; i--) #define DEB 0 #if DEB #define EPRLL(a) fprintf(stderr, "%s %lld ", #a, a); #define EPRN fprintf(stderr, "\n"); #else #define EPRLL(a) #define EPRN #endif const int MN = 1e6+6, al=3; long long dp_len[MN], dp_up_to[MN]; long long n, k; vector<char> result; int main(){ long long i, j, a, b; scanf("%lld%lld", &n, &k); dp_len[0]=1; dp_len[1]=al-1; dp_up_to[0]=1; dp_up_to[1]=al; R(2, n){ dp_len[i]=dp_len[i-1]*(al-1); dp_len[i]=min(dp_len[i], (long long)2e18); dp_up_to[i]=dp_up_to[i-1]+dp_len[i]; dp_up_to[i]=min(dp_up_to[i], (long long)2e18); } R(1, n){ EPRLL(dp_len[i]); } EPRN R(1, n){ EPRLL(dp_up_to[i]); } EPRN k++; char last = 'a'+al; IR(n, 1){ if(k==1){ result.push_back(' '); break; } k--; RI(j, 0, (al - 1)) { if(last != 'a'+j){ if(dp_up_to[i-1] < k) k-=dp_up_to[i-1]; else{ result.push_back('a'+j); last='a'+j; break; } } } if((signed)result.size() != n-i+1){ printf("NIE\n"); return 0; } } /*RI(j, 0, 5) printf("%d ", j);*/ R(0, (signed)result.size()-1) printf("%c", result[i]); printf("\n"); return 0; } |