768B - Code For 1 - CodeForces Solution


constructive algorithms dfs and similar divide and conquer *1600

Please click on ads to support us..

Python Code:

n, l, r= [int(item) for item in input().split()]

def _enumerate(n):
    if n <= 1:
        return 1
    else:
        return _enumerate(n//2)*2 + 1

def _list(n):
    if n<=1:
        return n
    else:
        return 2 * _list(n//2) + n%2

def solve(p, n, temp, prev):
    if p == 0:
        return 0
    elif p == temp:
        return _list(n)
    elif p==temp//2 + 1:
        return _list(n//2) + n%2
    elif p>temp:
        return _list(n) + prev%2 + solve(p - temp - 1, n//2, temp//2, n)
    else:
        return solve(p, n//2, temp//2, n)

def help_sam(l, r, n):
    lower = solve(l - 1, n, _enumerate(n), 0)
    higher = solve(r, n, _enumerate(n), 0)

    return higher - lower

output  = help_sam(l, r, n)

print(output)


Comments

Submit
0 Comments
More Questions

1729D - Friends and the Restaurant
1606C - Banknotes
580C - Kefa and Park
342A - Xenia and Divisors
1033A - King Escape
39D - Cubical Planet
1453A - Cancel the Trains
645A - Amity Assessment
1144A - Diverse Strings
1553B - Reverse String
1073A - Diverse Substring
630N - Forecast
312B - Archer
34D - Road Map
630I - Parking Lot
160B - Unlucky Ticket
371B - Fox Dividing Cheese
584B - Kolya and Tanya
137B - Permutation
550C - Divisibility by Eight
5A - Chat Servers Outgoing Traffic
615A - Bulbs
5B - Center Alignment
549A - Face Detection
535B - Tavas and SaDDas
722C - Destroying Array
366A - Dima and Guards
716B - Complete the Word
1461C - Random Events
1627A - Not Shading