mirror of
https://github.com/iritmaximus/tira.git
synced 2026-10-12 19:13:47 +03:00
35 lines
658 B
Python
35 lines
658 B
Python
from math import floor, ceil, log
|
|
|
|
|
|
def count(n):
|
|
print()
|
|
result = 0
|
|
nodes = 0
|
|
levels = 0
|
|
|
|
while 2**levels <= n // 2:
|
|
levels += 1
|
|
levels -= 1
|
|
print("levels", levels)
|
|
|
|
bottom_leaves = n + 1 - 2 * 2**levels
|
|
if bottom_leaves == 2 ** (levels + 1):
|
|
levels += 1
|
|
bottom_leaves = 0
|
|
|
|
print("bottom leaves:", bottom_leaves)
|
|
|
|
for x in range(1, levels + 1):
|
|
result += x * 2**x
|
|
|
|
result += bottom_leaves * (levels + 1)
|
|
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(count(4)) # 4
|
|
print(count(7)) # 10
|
|
print(count(123)) # 618
|
|
# print(count(999999999)) # 27926258178
|