题目
1 × 2 × 3 × ⋯ × 2007 × 2008 1\times 2 \times 3\times \dots \times 2007 \times 2008 1×2×3×⋯×2007×2008结果的末尾有几个连续的零?
简化表达
2008
!
2008!
2008!结果的末尾有几个连续的零?
公式推导
如果
K
=
1
×
2
×
3
×
⋯
×
(
N
−
1
)
×
N
=
M
0...0
⏞
L
K=1\times 2 \times 3\times \dots \times (N-1) \times N=M\overbrace{0...0}^{\text{L}}
K=1×2×3×⋯×(N−1)×N=M0...0
L。
每凑一对
2
×
5
2\times5
2×5 结果的末尾就多一个零。
K
K
K可以分解为
L
2
L_2
L2 个
2
2
2 相乘再乘以
X
X
X
K
=
X
×
2
L
2
K=X\times 2^{L_2}
K=X×2L2
{
L
2
1
=
2
4
⏟
2×2
…
8
⏟
2×2
…
L
2
2
=
4
8
⏟
2×2×2
…
16
⏟
2×2×2
…
L
2
3
=
8
16
⏟
2×2×2×2
…
32
⏟
2×2×2×2
…
…
\begin{cases} L_{2_1}=2\space \underbrace{4}_{\text{2×2}} \space \dots \space \underbrace{8}_{\text{2×2}} \space \dots\\ L_{2_2}=4\space \underbrace{8}_{\text{2×2×2}} \space \dots \space \underbrace{16}_{\text{2×2×2}} \space \dots\\ L_{2_3}=8\space \underbrace{16}_{\text{2×2×2×2}} \space \dots \space \underbrace{32}_{\text{2×2×2×2}} \space \dots\\\ \dots \end{cases}
⎩
⎨
⎧L21=2 2×2
4 … 2×2
8 …L22=4 2×2×2
8 … 2×2×2
16 …L23=8 2×2×2×2
16 … 2×2×2×2
32 … …
注:
①为什么
L
2
2
L_{2_2}
L22只取一次?
因为
L
2
1
L_{2_1}
L21中已经取过一次了,所以累计取了
2
2
2次。
②为什么
L
2
3
L_{2_3}
L23只取一次?
因为
L
2
2
L_{2_2}
L22、
L
2
1
L_{2_1}
L21中已经分别取过一次了,所以累计取了
3
3
3次。
…
\dots
…
同理,
K
K
K也可以分解
L
5
L_5
L5 个
5
5
5 相乘再乘以
Y
Y
Y。
K
=
Y
×
5
L
5
K=Y\times 5^{L_5}
K=Y×5L5
{
L
5
1
=
5
10
15
20
25
⏟
5×5
…
50
⏟
5×5
…
L
5
2
=
25
50
75
100
125
⏟
5×5×5
…
250
⏟
5×5×5
…
L
5
3
=
125
250
375
500
625
⏟
5×5×5×5
…
1250
⏟
5×5×5×5
…
…
\begin{cases} L_{5_1}=5\space 10\space 15\space 20\space \underbrace{25}_{\text{5×5}} \space \dots \space \underbrace{50}_{\text{5×5}} \space \dots\\ L_{5_2}=25\space 50\space 75\space 100\space \underbrace{125}_{\text{5×5×5}} \space \dots \space \underbrace{250}_{\text{5×5×5}} \space \dots\\ L_{5_3}=125\space 250\space 375\space 500\space \underbrace{625}_{\text{5×5×5×5}} \space \dots \space \underbrace{1250}_{\text{5×5×5×5}} \space \dots\\\ \dots \end{cases}
⎩
⎨
⎧L51=5 10 15 20 5×5
25 … 5×5
50 …L52=25 50 75 100 5×5×5
125 … 5×5×5
250 …L53=125 250 375 500 5×5×5×5
625 … 5×5×5×5
1250 … …
注:
①为什么
L
5
2
L_{5_2}
L52只取一次?
因为
L
5
1
L_{5_1}
L51中已经取过一次了,所以累计取了
2
2
2次。
②为什么
L
5
3
L_{5_3}
L53只取一次?
因为
L
5
2
L_{5_2}
L52、
L
5
1
L_{5_1}
L51中已经分别取过一次了,所以累计取了
3
3
3次。
…
\dots
…
整理得到如下公式:
L
=
{
L
5
=
⌊
N
5
1
⌋
+
⌊
N
5
2
⌋
+
⋯
+
⌊
N
5
n
⌋
=
∑
i
=
1
n
⌊
N
5
n
⌋
N
≥
0
,
5
n
≤
N
L
2
=
⌊
N
2
1
⌋
+
⌊
N
2
2
⌋
+
⋯
+
⌊
N
2
m
⌋
=
∑
j
=
1
m
⌊
N
2
m
⌋
N
≥
0
,
2
m
≤
n
L = \begin{cases} L_5=\lfloor\frac{N}{5^1}\rfloor+ \lfloor\frac{N}{5^2}\rfloor+\dots + \lfloor\frac{N}{5^n}\rfloor=\displaystyle\sum_{i=1}^n \lfloor\frac{N}{5^n}\rfloor&\text{ } N\geq0\text{\space ,\space}5^n\leq N \\ L_2=\lfloor\frac{N}{2^1}\rfloor+ \lfloor\frac{N}{2^2}\rfloor+\dots + \lfloor\frac{N}{2^m}\rfloor=\displaystyle\sum_{j=1}^m \lfloor\frac{N}{2^m}\rfloor &\text{ } N\geq0\text{\space ,\space}2^m\leq n \end{cases}
L=⎩
⎨
⎧L5=⌊51N⌋+⌊52N⌋+⋯+⌊5nN⌋=i=1∑n⌊5nN⌋L2=⌊21N⌋+⌊22N⌋+⋯+⌊2mN⌋=j=1∑m⌊2mN⌋ N≥0 , 5n≤N N≥0 , 2m≤n
∵
\because
∵ 当
N
≥
0
N\geq0
N≥0 时有
m
≥
n
m \geq n
m≥n 且
⌊
N
2
n
⌋
≥
⌊
N
5
n
⌋
\lfloor\frac{N}{2^n}\rfloor \geq\lfloor\frac{N}{5^n}\rfloor
⌊2nN⌋≥⌊5nN⌋
∴
L
2
≥
L
5
\therefore L_2 \geq L_5
∴L2≥L5 因此,
L
=
L
5
L= L_5
L=L5
注: ⌊ ⌋ \lfloor \space \rfloor ⌊ ⌋为向下取整符号
解
从 1 1 1 到 N N N 的连续正整数相乘(N!或N的阶乘),有如下计算公式
L = ∑ i = 1 n ⌊ N 5 n ⌋ 且 N ≥ 0 , 5 n ≤ N L=\displaystyle\sum_{i=1}^n \lfloor\frac{N}{5^n}\rfloor \text{ \space 且} N\geq0\text{\space ,\space}5^n\leq N L=i=1∑n⌊5nN⌋ 且N≥0 , 5n≤N
其中: L L L为末尾连续零的个数
∵
N
=
2008
\because N=2008
∵N=2008 且
5
n
≤
N
5^n\leq N
5n≤N,即
5
n
≤
2008
5^n\leq 2008
5n≤2008
{
5
1
=
5
5
2
=
25
5
3
=
125
5
4
=
625
5
5
=
3125
\begin{cases} 5^1=5 &\text{ } \\ 5^2=25 &\text{ } \\ 5^3=125 &\text{ } \\ 5^4=625 &\text{ } \\ 5^5=3125 &\text{} \end{cases}
⎩
⎨
⎧51=552=2553=12554=62555=3125
∴
n
=
4
\therefore n=4
∴n=4
L
=
∑
i
=
1
n
⌊
N
5
n
⌋
=
⌊
2008
5
1
⌋
+
⌊
2008
5
2
⌋
+
⌊
2008
5
3
⌋
+
⌊
2008
5
4
⌋
=
⌊
2008
5
⌋
+
⌊
2008
25
⌋
+
⌊
2008
125
⌋
+
⌊
2008
625
⌋
=
⌊
401.6
⌋
+
⌊
80.32
⌋
+
⌊
16.064
⌋
+
⌊
3.2128
⌋
=
401
+
80
+
16
+
3
=
500
\begin{equation} \begin{split} L&=\displaystyle\sum_{i=1}^n \lfloor\frac{N}{5^n}\rfloor \\ &=\lfloor\frac{2008}{5^1}\rfloor +\lfloor\frac{2008}{5^2}\rfloor +\lfloor\frac{2008}{5^3}\rfloor +\lfloor\frac{2008}{5^4}\rfloor \\ &=\lfloor\frac{2008}{5}\rfloor +\lfloor\frac{2008}{25}\rfloor +\lfloor\frac{2008}{125}\rfloor +\lfloor\frac{2008}{625}\rfloor \\ &=\lfloor 401.6\rfloor +\lfloor 80.32\rfloor +\lfloor 16.064\rfloor +\lfloor 3.2128\rfloor \\ &= 401 + 80 + 16 + 3 \\ &= 500 \end{split} \end{equation}
L=i=1∑n⌊5nN⌋=⌊512008⌋+⌊522008⌋+⌊532008⌋+⌊542008⌋=⌊52008⌋+⌊252008⌋+⌊1252008⌋+⌊6252008⌋=⌊401.6⌋+⌊80.32⌋+⌊16.064⌋+⌊3.2128⌋=401+80+16+3=500
扩展
使用代码编程输出结果
using System;
namespace ConsoleApp1
{
class Program
{
/// <summary>
/// 计算N!末尾连续0的个数
/// </summary>
/// <param name="N"></param>
/// <returns></returns>
static double CalculateZeroCount(double N)
{
double ZeroCount = 0;
int i = 1;
while(true)
{
var v = Math.Pow(5, i);
var cntTemp = Math.Floor(N / v);
if(cntTemp == 0)
{
break;
}
ZeroCount += Math.Floor(N/v);
i++;
}
return ZeroCount;
}
static void Main(string[] args)
{
var zeroCount = CalculateZeroCount(2008);
Console.WriteLine($"2008!结果的末尾有{zeroCount}个连续的零 ");
}
}
}
结果
2008!结果的末尾有500个连续的零