Project Euler 169
题目
Exploring
the number of different ways a number can be expressed as a sum of
powers of
Define and to be the number of different ways n
can be expressed as a sum of integer powers of using each power no more than
twice.
For example, since there
are five different ways to express :
What is ?
解决方案
如果 是一个奇数,那么就可以从 划分出一个 出来,然后再从 中继续进行。但注意这时 不能够再从 继续划分了,因为再用 就相当于用了 次 ,不合题意。因此只能从 开始寻找,而这等价于值为 的一个新问题。
如果 是一个偶数,那么可以选择不将 分出来,直接从 开始找,这等价于值为 的一个新问题;也可以选择将两个 分出来,再从剩下的 那一部分开始尝试将 划分出来,这等价于值为 的新问题。
因此,假设 为题目中要求的划分数,可以列出如下状态转移方程:
为避免子问题的重复计算,代码实现使用了记忆化搜索。
另外,用前几项查询 OEIS,结果为 A002487。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| N = 10 ** 25 mp = {}
def dfs(n: int): if n <= 1: return 1 if n in mp.keys(): return mp[n] m = n >> 1 if n & 1: mp[n] = dfs(m) else: mp[n] = dfs(m) + dfs(m - 1) return mp[n]
ans = dfs(N) print(ans)
|