#include <bits/stdc++.h> using namespace std; typedef unsigned long long LL; const int N = 1e6 + 7; int n; LL k, pot[N], x; char s[N]; inline char mn(char z) { return (z == 'a' ? 'b' : 'a'); } inline char mx(char z) { return (z == 'c' ? 'b' : 'c'); } int main() { scanf("%d%lld", &n, &k); pot[0] = 1; for(int i = 1; i <= n + 3; i++) pot[i] = min(2 * pot[i - 1], (LL)1e18 + 7); x = pot[n] - 1; if(k <= x) s[0] = 'a'; else if(k <= 2 * x) s[0] = 'b', k -= x; else if(k <= 3 * x) s[0] = 'c', k -= 2 * x; else { puts("NIE"); return 0; } for(int i = 1; i < n; i++) { if(k == 1) continue; if(k <= pot[n - i]) s[i] = mn(s[i - 1]), k--; else s[i] = mx(s[i - 1]), k -= pot[n - i]; } printf("%s\n", s); 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 | #include <bits/stdc++.h> using namespace std; typedef unsigned long long LL; const int N = 1e6 + 7; int n; LL k, pot[N], x; char s[N]; inline char mn(char z) { return (z == 'a' ? 'b' : 'a'); } inline char mx(char z) { return (z == 'c' ? 'b' : 'c'); } int main() { scanf("%d%lld", &n, &k); pot[0] = 1; for(int i = 1; i <= n + 3; i++) pot[i] = min(2 * pot[i - 1], (LL)1e18 + 7); x = pot[n] - 1; if(k <= x) s[0] = 'a'; else if(k <= 2 * x) s[0] = 'b', k -= x; else if(k <= 3 * x) s[0] = 'c', k -= 2 * x; else { puts("NIE"); return 0; } for(int i = 1; i < n; i++) { if(k == 1) continue; if(k <= pot[n - i]) s[i] = mn(s[i - 1]), k--; else s[i] = mx(s[i - 1]), k -= pot[n - i]; } printf("%s\n", s); return 0; } |