Project Euler 501
Project Euler 501
题目
Eight Divisors
The eight divisors of \(24\) are \(1, 2, 3, 4, 6, 8, 12\) and \(24\).
The ten numbers not exceeding \(100\) having exactly eight divisors are \(24, 30, 40, 42, 54, 56, 66, 70, 78\) and \(88\).
Let \(f(n)\) be the count of numbers not exceeding \(n\) with exactly eight divisors.
You are given \(f(100)=10, f(1000)=180\) and \(f(10^6)=224427\).
Find \(f(10^{12})\).
Lehmer公式
Lehmer公式给出了质数计数函数\(\pi\)的一个计算方式:
\[\pi(x)=\varphi(x,a)+\dfrac{(b+a-2)(b-a+1)}{2}-\sum_{i=a+1}^b\pi\left(\dfrac{x}{p_i}\right)-\sum_{i=a+1}^c\sum_{j=i}^{b_i}\left(\pi\left(\dfrac{x}{p_ip_j}\right)-(j-1)\right)\]
其中,\(p_i\)表示第\(i\)个质数,\(a=\pi(\sqrt[4]{x}),b=\pi(\sqrt{x}),c=\pi(\sqrt[3]{x}),b_i=\pi\left(\sqrt{\dfrac{x}{p_i}}\right)\) 其中\(\varphi(x,a)\)是\(n\)以内的数中,质因数只由第\(a+1\)个及以后的质数的个数,用容斥原理可以写成:
\[\varphi(x,a)=x-\sum_{i=1}^a\left\lfloor\dfrac{x}{p_i}\right\rfloor+\sum_{1\le i\le j\le a}\left\lfloor\dfrac{x}{p_ip_j}\right\rfloor-\dots\]
为了方便计算,页面说明\(\varphi(x,a)\)还可以写成以下递推式形式:
\[ \varphi(x,a)= \left \{\begin{aligned} &\left\lceil\dfrac{x}{2}\right\rceil & & \text{if}\quad x=0\lor a=1 \\ &\varphi(x,a-1)-\varphi\left(\left\lfloor\dfrac{x}{p_a}\right\rfloor,a-1\right) & & \text{else} \end{aligned}\right. \]
在计算函数\(\pi\)和\(\varphi\)时,需要使用记忆化方法记录函数值。
解决方案
令\(N=10^{12}\),有\(8\)个因子的数分别有以下三种(以下使用的\(p_i\)均为质数):
- \(p_1\)
- \(p_1^3p_2\),其中满足\(p_1\neq p_2\)
- \(p_1p_2p_3\),其中满足\(p_1<p_2<p_3\)
那么不难写出三种数对应的个数为:
- \(f_1(n)=\pi(\sqrt[7]{N})\)
- \(f_2(n)=\sum_{p_1\le \sqrt[3]{N}}\left(\pi\left(\dfrac{N}{p_1^3}\right)\right)-\pi(\sqrt[4]{N})\)
- \(f_3(n)=\sum_{p_1\le \sqrt[3]{N}}\sum_{p_1<p_2<\sqrt{\frac{N}{p_1}}}\left(\pi\left(\dfrac{N}{p_1\cdot p_2}\right)-\pi(p_2)\right)\)
最终得到\(f(n)=f_1(n)+f_2(n)+f_3(n).\)
那么现在问题就转化成了使用Lehmer
公式计算函数\(\pi.\)
代码
1 |
|