Project Euler 374
Project Euler 374
题目
Maximum Integer Partition Product
An integer partition of a number n is a way of writing \(n\) as a sum of positive integers.
Partitions that differ only in the order of their summands are considered the same. A partition of \(n\) into distinct parts is a partition of \(n\) in which every part occurs at most once.
The partitions of \(5\) into distinct parts are:
\(5, 4+1\) and \(3+2\).
Let \(f(n)\) be the maximum product of the parts of any such partition of \(n\) into distinct parts and let \(m(n)\) be the number of elements of any such partition of \(n\) with that product.
So \(f(5)=6\) and \(m(5)=2\).
For \(n=10\) the partition with the largest product is \(10=2+3+5\), which gives \(f(10)=30\) and \(m(10)=3\).
And their product, \(f(10)\cdot m(10) = 30\cdot3 = 90\)
It can be verified that
\(\sum f(n)\cdot m(n)\) for \(1 \le n \le 100 = 1683550844462\).
Find \(\sum f(n)\cdot m(n)\) for \(1 \le n \le 10^{14}\).
Give your answer modulo \(982451653\), the \(50\) millionth prime.
解决方案
下面用“局部替换增益”的思路刻画最优结构,这些结构在论文中有所介绍:
- 不含 1:若拆分里有 \(1\) 和 \(x\) 且 \(x+1\) 不在拆分中,则用 \((x+1)\) 替换 \((1,x)\),总和不变而乘积变大,因为 \(1\cdot x < x+1\)。因此最大乘积拆分不会包含 \(1\)。
- 尽量均匀:若 \(x<y\) 在拆分中,且 \(x+1,y-1\) 都不在拆分中,则用 \((x+1,y-1)\) 替换 \((x,y)\),乘积增大:\((x+1)(y-1)-xy=y-x-1>0.\)因此最优拆分的数会尽量靠近,呈现“几乎连续”的形态:最多只有一个缺口且缺口大小至多为 \(1\)。
- 大数倾向拆成 2 与剩余:若 \(x>4\) 在拆分中且 \(x-2\) 不在拆分中,则用 \((2,x-2)\) 替换 \((x)\),总和不变而乘积增大,因为 \(2(x-2)=2x-4>x\)。这迫使最优拆分的起点在 \(2\) 或 \(3\) 附近。
综合结构2和3,最优拆分应当是形如 \(2,\dots,k\) 或 \(3,\dots,k\) 的连续段,允许“恰好少一个数”的缺口(且只会发生在非常受限的位置)。
论文的引理2.2指出,令三角数\(T_m=\dbinom{m+1}{2}=\dfrac{m(m+1)}{2}.\)对任意 \(n>1\),存在唯一的正整数 \(m\) 与 \(l\) 使得\(n=T_m+l, 0\le l\le m,\)且\(m=\left\lfloor\dfrac{\sqrt{8n+1}-1}{2}\right\rfloor, l=n-T_m.\)
论文的定理3.1给出了最大乘积 \(a_n\)(即本题的 \(f(n)\))的分段公式:若 \(n=T_m+l\),则 \[ f(n)= \begin{cases} \dfrac{(m+1)!}{m-l}, & 0\le l\le m-2,\\ \dfrac{m+2}{2}m!, & l=m-1,\\ (m+1)!, & l=m. \end{cases} \]
而且证明过程同时给出了达到最大值的拆分形态:
- 当 \(0\le l\le m-2\) 时,最优拆分为从 \(2\) 到 \((m+1)\) 的连续段缺一个数 \((m-l)\):\(n=2+3+\cdots+(m-l-1)+(m-l+1)+\cdots+m+(m+1),\)其乘积正是 \(\dfrac{(m+1)!}{m-l}\)。
- 当 \(l=m-1\) 时,最优拆分为\(n=3+4+\cdots+m+(m+2),\)乘积为 \(\dfrac{m+2}{2}m!\)。
- 当 \(l=m\) 时,最优拆分为\(n=2+3+\cdots+m+(m+1),\)乘积为 \((m+1)!\)。
由上面三种最优拆分直接数项即可得到:
- 若 \(l=m\),拆分为 \(2,3,\dots,m+1\),长度为 \(m\);
- 若 \(0\le l\le m-1\),拆分长度均为 \(m-1\)(要么缺一个数,要么从 \(3\) 开始但最后加了 \((m+2)\))。
因此对 \(n\ge 2\),
\[ m(n)= \begin{cases} m-1, & 0\le l\le m-1,\\ m, & l=m. \end{cases} \] 另外 \(n=1\) 时只有拆分 \(1\),所以 \(f(1)=1,m(1)=1\)。
接下里正式开始进行计算。固定 \(m\ge 2\),考虑这一组\(n=T_m+l, l=0,1,\dots,m,\)即从 \(T_m\) 到 \(T_{m+1}-1\) 共 \(m+1\) 个数。
- 对 \(l=0,\dots,m-2\):\(f(n)m(n)=(m-1)\cdot \dfrac{(m+1)!}{m-l}.\)令 \(d=m-l\),则 \(d\) 从 \(m\) 递减到 \(2\),于是这部分和为\(\displaystyle{(m-1)(m+1)!\sum_{d=2}^{m}\frac{1}{d}.}\)
- 对 \(l=m-1\):\(f(n)m(n)=(m-1)\cdot \dfrac{m+2}{2}m!.\)
- 对 \(l=m\):\(f(n)m(n)=m\cdot (m+1)!.\)
需要注意的是,最后一组可能不完整。令\(M=\max\{m:T_m\le N\}=\left\lfloor\dfrac{\sqrt{8N+1}-1}{2}\right\rfloor, L=N-T_M.\)
则对 \(m<M\) 的组都是完整的;对 \(m=M\) 只取 \(l=0,\dots,L\)。
当 \(L\le m-2\) 时,只需要计算\(\displaystyle{(m-1)(m+1)!\sum_{d=m-L}^{m}\frac1d,}\) 可见,\(\displaystyle{\sum_{d=m-L}^{m}\frac1d}\)可用前缀和差分实现。
6. Python 参考实现
1 | # Project Euler 374 |
7. 本题答案
\[ \sum_{n=1}^{10^{14}} f(n),m(n)\equiv \boxed{334420941}\pmod{982451653}. \]