#include <bits/stdc++.h> using namespace std; typedef long long ll; ll Ile[1100000]; const ll MAXLL = 1ll << 60; ll ileciagow(int len) { return Ile[len]; } ll ilepusty(int len) { return 1; } ll n, k; char md[] = "baa"; char ma[] = "ccb"; char min_d(char last) { return md[last - 'a']; } char max_d(char last) { return ma[last - 'a']; } int main() { scanf("%lld%lld", &n, &k); Ile[1] = 1; // ile ciągów z ustalonym pierwszym wyrazem for(int i = 2; i <= n; ++i) { Ile[i] = min(MAXLL, 2 * ileciagow(i-1) + 1); } char l = 'a'; //pierwsze if(Ile[n] + 1 <= k) { k -= Ile[n] + 1; if(Ile[n] <= k) { k -= Ile[n]; if(Ile[n] <= k) { puts("NIE"); return 0; } l = 'c'; } else { l = 'b'; } } else --k; printf("%c", l); for(int i = 2; i <= n; ++i) { if(Ile[n - i + 1] + 1 <= k) { k -= Ile[n-i+1] + 1; l = max_d(l); } else { if(k == 0) l = 0; else { l = min_d(l); --k; } } if(l != 0) printf("%c", l); } puts(""); }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; ll Ile[1100000]; const ll MAXLL = 1ll << 60; ll ileciagow(int len) { return Ile[len]; } ll ilepusty(int len) { return 1; } ll n, k; char md[] = "baa"; char ma[] = "ccb"; char min_d(char last) { return md[last - 'a']; } char max_d(char last) { return ma[last - 'a']; } int main() { scanf("%lld%lld", &n, &k); Ile[1] = 1; // ile ciągów z ustalonym pierwszym wyrazem for(int i = 2; i <= n; ++i) { Ile[i] = min(MAXLL, 2 * ileciagow(i-1) + 1); } char l = 'a'; //pierwsze if(Ile[n] + 1 <= k) { k -= Ile[n] + 1; if(Ile[n] <= k) { k -= Ile[n]; if(Ile[n] <= k) { puts("NIE"); return 0; } l = 'c'; } else { l = 'b'; } } else --k; printf("%c", l); for(int i = 2; i <= n; ++i) { if(Ile[n - i + 1] + 1 <= k) { k -= Ile[n-i+1] + 1; l = max_d(l); } else { if(k == 0) l = 0; else { l = min_d(l); --k; } } if(l != 0) printf("%c", l); } puts(""); } |