#include <iostream> #include <vector> using namespace std; typedef unsigned long long ll; const ll INF = 5000000000000000000; int main() { ios_base::sync_with_stdio(0); ll n, k; cin >> n >> k; vector<ll> pot(n + 1, INF), sumy(n + 1, INF); pot[0] = 1; sumy[0] = 1; for (int i = 1; i < n + 1; ++i) { pot[i] = pot[i - 1] * 2LL; sumy[i] = sumy[i - 1] + pot[i]; if (sumy[i] > INF) { sumy[i] = INF; break; } } string odp = ""; for (int i = 1; i <= n; ++i) { if (k == 1 && i > 1) break; if (i > 1) --k; if (i == 1 || (i > 1 && odp[i - 2] != 'a')) { if (sumy[n - i] >= k) { odp += "a"; continue; } else k -= sumy[n - i]; } if (i == 1 || (i > 1 && odp[i - 2] != 'b')) { if (sumy[n - i] >= k) { odp += "b"; continue; } else k -= sumy[n - i]; } if (i == 1 || (i > 1 && odp[i - 2] != 'c')) { if (sumy[n - i] >= k) { odp += "c"; continue; } else k -= sumy[n - i]; } if (odp == "") { cout << "NIE"; return 0; } } cout << odp; 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 | #include <iostream> #include <vector> using namespace std; typedef unsigned long long ll; const ll INF = 5000000000000000000; int main() { ios_base::sync_with_stdio(0); ll n, k; cin >> n >> k; vector<ll> pot(n + 1, INF), sumy(n + 1, INF); pot[0] = 1; sumy[0] = 1; for (int i = 1; i < n + 1; ++i) { pot[i] = pot[i - 1] * 2LL; sumy[i] = sumy[i - 1] + pot[i]; if (sumy[i] > INF) { sumy[i] = INF; break; } } string odp = ""; for (int i = 1; i <= n; ++i) { if (k == 1 && i > 1) break; if (i > 1) --k; if (i == 1 || (i > 1 && odp[i - 2] != 'a')) { if (sumy[n - i] >= k) { odp += "a"; continue; } else k -= sumy[n - i]; } if (i == 1 || (i > 1 && odp[i - 2] != 'b')) { if (sumy[n - i] >= k) { odp += "b"; continue; } else k -= sumy[n - i]; } if (i == 1 || (i > 1 && odp[i - 2] != 'c')) { if (sumy[n - i] >= k) { odp += "c"; continue; } else k -= sumy[n - i]; } if (odp == "") { cout << "NIE"; return 0; } } cout << odp; return 0; } |