题目链接
这个题就是bfs。不过我错误地估计了时间复杂度为O(2!+3!+…+19!)。其实不会有这么多枝,因为最后得到的位数满足要求的数一定可以被表示为
x
∗
x*
x∗
2
a
∗
2^a*
2a∗
3
b
∗
3^b*
3b∗
5
c
∗
5^c*
5c∗
7
d
7^d
7d的形式(因为x每次只乘10以内的数)。我们知道最后的数一定是不大于1e18的,因此 a is not greater than 63, b is not greater than 39, c is not greater than 27, and d is not greater than 22, and the number of reachable integers is about 1.5 million。也就是说时间复杂度在1e6附近。bfs维护一个HashMap,记录每个数对应的最小步数,重复出现的数跳过。
从原理上讲,从1到1e18之间的数,有很多素数,也有很多大于10的素因子的幂组成的数,是我们不会在bfs过程中遇到的。因此时间复杂度没有想的这么夸张。
1681D - Required Length(dp,bfs,素因子)
最新推荐文章于 2024-06-12 10:00:23 发布