1950F - 0 1 2 Tree - CodeForces Solution


greedy math trees

Please click on ads to support us..

Python Code:

import sys
from os import path
import math

FILE = False 
if FILE:
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')

def get_int():
    return int(sys.stdin.readline())

def get_string():
    return sys.stdin.readline().strip()


def find_height(a,b,c):
    if 1+a!=c:
        return -1
    
    a_length = a.bit_length()
    if a_length == 0:
        return b+c-1
    
    openings = (((1 << a_length) - 1) ^ a)
    remaining_ones = max(0,b-openings)
    
        add_routes =  ((1 << a_length-1) - openings) * 2 + openings
    new_height = math.ceil( remaining_ones / add_routes ) if remaining_ones!=0 else 0
    
    height_2 = (a_length-1) 
        min_height = height_2 + new_height + 1
    
    return min_height
    

t = get_int()

final_result = []
for i in range(t):
    numbers = get_string()
    a,b,c = list(map(int,numbers.split()))
    ans = find_height(a,b,c)
    final_result.append(str(ans))

for item in final_result:
    sys.stdout.write(item)
    sys.stdout.write('\n')


Comments

Submit
0 Comments
More Questions

892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words
1681A - Game with Cards
151C - Win or Freeze
1585A - Life of a Flower
1662A - Organizing SWERC