Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define fore(i, l, r) for(int i = int(l); i < int(r); i++)
- #define sz(a) int((a).size())
- #define x first
- #define y second
- typedef long long li;
- li n, k;
- inline bool read() {
- if(!(cin >> n >> k))
- return false;
- return true;
- }
- int getLen(li val) {
- int ans = 0;
- while(val > 0) {
- ans++;
- val >>= 1;
- }
- return ans;
- }
- li calcSize(li val, li mx) {
- if(val > mx)
- return 0;
- int diffLen = getLen(mx) - getLen(val);
- li ans = 0;
- fore(k, 0, diffLen)
- ans += 1ll << k;
- if(val > (mx >> diffLen))
- return ans;
- if(val < (mx >> diffLen))
- ans += 1ll << diffLen;
- else
- ans += mx - (val << diffLen) + 1;
- return ans;
- }
- li calcSubTree(li val, li mx) {
- if(val & 1ll)
- return calcSize(val, mx);
- return calcSize(val, mx) + calcSize(val + 1, mx);
- }
- inline void solve() {
- li lvl = 1;
- for(li curL = 1ll; curL <= n; curL <<= 1) {
- if(calcSubTree(curL, n) >= k)
- lvl = curL;
- }
- li ans = 0;
- li lf = lvl / 2, rg = lvl;
- while(rg - lf > 1) {
- li mid = (lf + rg) >> 1;
- if(calcSubTree(2 * mid, n) >= k)
- lf = mid;
- else
- rg = mid;
- }
- ans = max(ans, 2 * lf);
- lf = lvl / 2, rg = lvl;
- while(rg - lf > 1) {
- li mid = (lf + rg) >> 1;
- if(calcSubTree(2 * mid + 1, n) >= k)
- lf = mid;
- else
- rg = mid;
- }
- if(calcSubTree(2 * lf + 1, n) >= k)
- ans = max(ans, 2 * lf + 1);
- cout << ans << endl;
- }
- int main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- int tt = clock();
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(0), cout.tie(0);
- cout << fixed << setprecision(15);
- if(read()) {
- solve();
- #ifdef _DEBUG
- cerr << "TIME = " << clock() - tt << endl;
- tt = clock();
- #endif
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement