1 条题解

  • 0
    @ 2025-5-8 16:26:42

    打表+二分查找

    简单的两层循环构造符合条件的数字列表相信童鞋们应该都会了hhh

    import sys
    import bisect
    input = lambda: sys.stdin.readline().strip() 
    a, b = map(int, input().split())
    
    # 符合条件的数字列表(有序)
    special_numbers = [
        1100, 1111, 1122, 1133, 1144, 1155, 1166, 1177, 1188, 1199,
        2200, 2211, 2222, 2233, 2244, 2255, 2266, 2277, 2288, 2299,
        3300, 3311, 3322, 3333, 3344, 3355, 3366, 3377, 3388, 3399,
        4400, 4411, 4422, 4433, 4444, 4455, 4466, 4477, 4488, 4499,
        5500, 5511, 5522, 5533, 5544, 5555, 5566, 5577, 5588, 5599,
        6600, 6611, 6622, 6633, 6644, 6655, 6666, 6677, 6688, 6699,
        7700, 7711, 7722, 7733, 7744, 7755, 7766, 7777, 7788, 7799,
        8800, 8811, 8822, 8833, 8844, 8855, 8866, 8877, 8888, 8899,
        9900, 9911, 9922, 9933, 9944, 9955, 9966, 9977, 9988, 9999
    ]
    
    def count_special_numbers(a, b):
        # 如果a比b大,交换它们的值
        if a > b:
            a, b = b, a
        
        # 使用二分查找找到第一个 >=a 的元素的索引
        left = bisect.bisect_left(special_numbers, a)
        # 使用二分查找找到第一个 >b 的元素的索引
        right = bisect.bisect_right(special_numbers, b)
        
        # 符合条件的数量是 right - left
        return right - left
    
    print(count_special_numbers(a, b))

    信息

    ID
    10
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    23
    已通过
    17
    上传者