本帖最后由 fan1993423 于 2020-3-17 17:41 编辑
这道题思路很容易想到,但考虑到是效率题,所以还是要从效率入手。在323055,41520421这数据,我测了下是14秒,比其他鱼油快点,部分鱼油解答不出来这个数据。估计又要被版主说超时了。已经想好了这一步了。如果有优化空间在发上来。
- from bisect import bisect,bisect_left
- from math import gcd
- from functools import reduce
- def get_lcm(lst):
- return reduce(lambda a,b:a*b//gcd(a,b),lst)
- def get_test(num):
- for i in str(num):
- if num% int(i):return False
- return True
- a=[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22, 24, 33, 36, 44, 48, 55, 66, 77, 88, 99, 111, 112, 115, 122, 124, 126, 128, 132, 135, 144, 155, 162, 168, 175, 184, 212, 216, 222, 224, 244, 248, 264, 288, 312, 315, 324, 333, 336, 366, 384, 396, 412, 424, 432, 444, 448, 488, 515, 555, 612, 624, 636, 648, 666, 672, 728, 735, 777, 784, 816, 824, 848, 864, 888, 936, 999]
- right_n=[]
- def fun354(left,right):
- if right<1000:
- return a[bisect_left(a,left):bisect(a,right)]
- elif left<1000 and right>1000:
- b=right//1000
- for i in range(1,b+1):
- if '0' not in str(i):
- right_lcm=get_lcm(list(map(int,str(i))))
- for j in range(i*1000+111,(i+1)*1000):
- if '0' not in str(j) and get_test(j):
- break
- for k in range(j,(i+1)*1000,right_lcm):
- if '0' not in str(k) and get_test(k) and k<=right:
- right_n.append(k)
- return a[bisect_left(a,left):]+right_n
- else:
- b=right//1000
- c=left//1000
- for i in range(c,b+1):
- if '0' not in str(i):
- right_lcm=get_lcm(list(map(int,str(i))))
- for j in range(i*1000+111,(i+1)*1000):
- if '0' not in str(j) and get_test(j):
- break
- for k in range(j,(i+1)*1000,right_lcm):
- if '0' not in str(k) and get_test(k) and k<=right and k>=left:
- right_n.append(k)
- return right_n
复制代码
|