// AwA <DATETIME> #include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define fi first #define se second const int N = 2e5 + 5, M = 1e6 + 5; const int inf = 1e9, mod = 998244353; const ll INF = 1e18, wxhn = 2324814; mt19937 rd(time(0)); uniform_int_distribution<int> dist(0, wxhn); struct qwq{ vector <int> a; void init(string s){ reverse(s.begin(), s.end()); a.clear(); for (auto i : s) a.push_back(i - '0'); } void init(int x){ a.clear(); if (!x) a.push_back(0); while (x) a.push_back(x % 10), x /= 10; } void read(){ string awa; cin >> awa; init(awa); } const qwq operator += (const qwq &rr){ vector <int> x = rr.a; while (a.size() < x.size()) a.push_back(0); for (int i = 0; i < x.size(); i ++ ) a[i] += x[i]; for (int i = 0; i < a.size(); i ++ ){ if (a[i] > 9){ if (i + 1 >= a.size()) a.push_back(a[i] / 10); else a[i + 1] += a[i] / 10; a[i] %= 10; } } return (*this); } const qwq operator -= (const qwq &rr){ vector <int> x = rr.a; for (int i = 0; i < x.size(); i ++ ) a[i] -= x[i]; for (int i = 0; i < a.size(); i ++ ){ while (a[i] < 0){ a[i + 1] -= 1; a[i] += 10; } } while (a.size() && a.back() == 0) a.pop_back(); if (!a.size()) a.push_back(0); return (*this); } const qwq operator -= (const int &x){ // cout << a[0] << " " << x << " " << a[0] - x << "\n"; a[0] -= x; for (int i = 0; i < a.size(); i ++ ){ if (a[i] < 0){ a[i + 1] -= 1; a[i] += 10; } } while (a.size() && a.back() == 0) a.pop_back(); if (!a.size()) a.push_back(0); return (*this); } const qwq operator += (const int &x){ a[0] += x; for (int i = 0; i < a.size(); i ++ ){ if (a[i] > 9){ if (i + 1 >= a.size()) a.push_back(a[i] / 10); else a[i + 1] += a[i] / 10; a[i] %= 10; } } return (*this); } const qwq operator /= (const int &x){ int r = 0; vector <int> vec; for (int i = a.size() - 1; i >= 0; i -- ){ r *= 10, r += a[i]; vec.push_back(r / x); r %= x; } reverse(vec.begin(), vec.end()); a = vec; while (a.size() && a.back() == 0) a.pop_back(); if (!a.size()) a.push_back(0); return (*this); } const bool operator < (const qwq &rr){ vector <int> x = rr.a; if (a.size() < x.size()) return true; if (a.size() > x.size()) return false; for (int i = a.size() - 1; i >= 0; i -- ) if (a[i] != x[i]) return a[i] < x[i]; return false; } const bool operator <= (const qwq &rr){ vector <int> x = rr.a; if (a.size() < x.size()) return true; if (a.size() > x.size()) return false; for (int i = a.size() - 1; i >= 0; i -- ) if (a[i] != x[i]) return a[i] < x[i]; return true; } const bool operator == (const qwq &rr){ return a == rr.a; } } k, len, sum, sta, one, thr; void print(qwq x){ for (int i = x.a.size() - 1; i >= 0; i -- ) cout << x.a[i]; cout << "\n"; } signed main(){ ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); one.init(1), thr.init(3); int n; while (cin >> n){ k.read(); if (n == 1){ if (one < k) cout << "-1\n"; else cout << "1\n"; continue; } if (n == 2){ if (thr < k) cout << "-1\n"; else print(k); continue; } if (n == 3){ int md = 0; for (auto i : k.a) md = (md + i) % 3; k -= 1, k /= 3, k += 1; qwq res; res.init(1); while (k.a.size() > 1 || k.a[0] > 0){ res += res, k -= 1; } if (md == 0) res += 1; else if (md == 1) res -= 1; print(res); continue; } len.init(n); sum.init(n); sta.init(1); while (sum < k){ len += len, len += 3; sum += len; sta += sta, sta += 1; } sum -= len; qwq lst = k; lst -= sum, lst -= 1; sta += lst; print(sta); } }
Note.ms
/shit