Leetcode 之 Factorial Trailing Zeroes

来源:互联网 发布:淘宝网时间校准 编辑:程序博客网 时间:2024/05/02 03:08

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.


对n!做质因数分解n!=2x*3y*5z*...

显然0的个数等于min(x,z),并且min(x,z)==z

证明:

对于阶乘而言,也就是1*2*3*...*n
[n/k]代表1~n中能被k整除的个数
那么很显然
[n/2] > [n/5] (左边是逢2增1,右边是逢5增1)
[n/2^2] > [n/5^2](左边是逢4增1,右边是逢25增1)
……
[n/2^p] > [n/5^p](左边是逢2^p增1,右边是逢5^p增1)
随着幂次p的上升,出现2^p的概率会远大于出现5^p的概率。
因此左边的加和一定大于右边的加和,也就是n!质因数分解中,2的次幂一定大于5的次幂

解法一:

从1到n中提取所有的5


class Solution {public:    int trailingZeroes(int n) {        int ret = 0;        for(int i = 1; i <= n; i ++)        {            int tmp = i;            while(tmp%5 == 0)            {                ret ++;                tmp /= 5;            }        }        return ret;    }};

解法二:

由上述分析可以看出,起作用的只有被5整除的那些数。能不能只对这些数进行计数呢?

存在这样的规律:[n/k]代表1~n中能被k整除的个数。

因此解法一可以转化为解法二


class Solution {public:    int trailingZeroes(int n) {        int ret = 0;        while(n)        {            ret += n/5;            n /= 5;        }        return ret;    }};



0 0
原创粉丝点击