二分查找变形

在这里插入图片描述

arr=list(map(int,input().split()))
n=arr[0]
m=arr[1]
def get_sum(s):
    total=0
    for i in range(n):
        total+=s
        s=(s+1)//2
    return total
low=1
high=m
while(low<=high):
    mid=low+(high-low)//2
    print(mid)
    if get_sum(mid)==m:
        print(mid)
        break
    elif get_sum(mid)<m:
        low=mid+1
    else:
        high=mid-1
if low>high:
    print(low-1)

猜你喜欢

转载自blog.csdn.net/haoshan4783/article/details/89055990