Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define f first
- #define s second
- #define ll long long
- #define pb push_back
- #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
- #define mk make_pair
- using namespace std;
- const int MAXN = 1e5 + 5;
- const ll INF = 1e18 + 7;
- int a[MAXN];
- map <ll, ll> ma, count_one;
- ll cnt(ll x){
- if (ma[x] != 0){
- return ma[x];
- }
- if (x <= 1){
- return 1;
- }
- ma[x] = cnt(x / 2) * 2 + 1;
- return ma[x];
- }
- ll count_digit_one(ll x){
- if (count_one[x] != 0){
- return count_one[x];
- }
- if (x <= 1){
- return x;
- }
- count_one[x] = count_digit_one(x / 2) * 2 + x % 2;
- return count_one[x];
- }
- ll posk(ll x, ll k){
- if (k == 0){
- return 0;
- }
- // cout << cnt(x / 2) << ' ' << x << ' ' << k << "\n";
- if (x <= 1){
- return x;
- }
- if (cnt(x / 2) >= k){
- return posk(x / 2, k);
- }
- ll ans = count_digit_one(x / 2);
- k -= cnt(x / 2);
- if (k == 1){
- return ans + (x % 2);
- }
- ans += x % 2;
- k--;
- return ans + posk(x / 2, k);
- }
- int main() {
- IOS;
- ll n, l, r;
- cin >> n >> l >> r;
- //cout << count_digit_one(2) << endl;
- cout << posk(n, r) - posk(n, l - 1);
- }
- /*
- 10
- 5 0 5
- 2 1 2 0 2 1 2
- 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1
- */
Advertisement
Add Comment
Please, Sign In to add comment