AcWing 1085. 不要62(Python)
原题链接
中等
作者:
习学学
,
2021-04-07 16:42:06
,
所有人可见
,
阅读 366
Python 代码
import sys
f = [[0] * 10 for _ in range(11)]
for i in range(10):
if i != 4: f[1][i] = 1
for i in range(2, 11):
for j in range(10):
if j == 4: continue
for k in range(10):
if j == 6 and k == 2: continue
if k == 4: continue
f[i][j] += f[i-1][k]
def dp(x):
if x == 0: return 1
nums = []
while x:
nums.append(x % 10)
x //= 10
res = 0
last = -1
for i in range(len(nums)-1, -1, -1):
num = nums[i]
for j in range(num):
if last == 6 and j == 2: continue
elif j == 4: continue
res += f[i+1][j]
if last == 6 and num == 2: break
if num == 4: break
if i == 0: res += 1
last = num
return res
while True:
line = sys.stdin.readline()
L, R = map(int, line.split())
if L == 0 and R == 0: break
print(dp(R) - dp(L-1))