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

Note: Your solution should be in logarithmic time complexity.

转自:http://blog.csdn.net/doc_sgl/article/details/42344441

考虑n!的质数因子。后缀0总是由质因子2和质因子5相乘得来的。如果我们可以计数2和5的个数,问题就解决了。考虑下面的例子:

n = 5: 5!的质因子中 (2 * 2 * 2 * 3 * 5)包含一个5和三个2。因而后缀0的个数是1。

n = 11: 11!的质因子中(2^8 * 3^4 * 5^2 * 7)包含两个5和三个2。于是后缀0的个数就是2。

我们很容易观察到质因子中2的个数总是大于等于5的个数。因此只要计数5的个数就可以了。那么怎样计算n!的质因子中所有5的个数呢?一个简单 的方法是计算floor(n/5)。例如,7!有一个5,10!有两个5。除此之外,还有一件事情要考虑。诸如25,125之类的数字有不止一个5。例 如,如果我们考虑28!,我们得到一个额外的5,并且0的总数变成了6。处理这个问题也很简单,首先对n÷5,移除所有的单个5,然后÷25,移除额外的 5,以此类推。

总结:

只有2和5相乘才会出现0,其中整十也可以看做是2和5相乘的结果,所以,可以在n之前看看有多少个2以及多少个5就行了,又发现2的数量一定多于5的个数,于是我们只看n前面有多少个5就行了,于是n/5就得到了5的个数,还有一点要注意的就是25这种,5和5相乘的结果,所以,还要看n/5里面有多少个5,也就相当于看n里面有多少个25,还有125,625.。。

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

Factorial Trailing Zeroes——数学类的更多相关文章

  1. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  2. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

  3. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  4. LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

    172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...

  5. LeetCode_172. Factorial Trailing Zeroes

    172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...

  6. LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  7. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  8. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  9. 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告

    原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...

随机推荐

  1. bzoj4518: [Sdoi2016]征途(DP+决策单调性分治优化)

    题目要求... 化简得... 显然m和sum^2是已知的,那么只要让sigma(si^2)最小,那就变成了求最小平方和的最小值,经典的决策单调性,用分治优化即可. 斜率优化忘得差不多就不写了 #inc ...

  2. 使用 Rational AppScan 保证 Web 应用的安全性,第 1 部分: Web 安全与 Rational AppScan 入门

    前言 当今世界,Internet(因特网)已经成为一个非常重要的基础平台,很多企业都将应用架设在该平台上,为客户提供更为方便.快捷的服务支持.这些应用 在功能和性能上,都在不断的完善和提高,然而在非常 ...

  3. linux内核挂载文件系统的两种方式

    1.nfs方式(挂载的为文件夹) bootargs=root=/dev/nfs nfsroot=192.168.1.105:/mnt/rootfs/rootfs ip=192.168.1.88:192 ...

  4. 网络编程:I/O模型

    I/O模型 Unix下可用的5种I/O模型有: 阻塞式I/O 非阻塞式I/O I/O复用(select和poll,epoll) 信号驱动式I/O 异步I/O(POSIX的aio_系列函数) 一个输入操 ...

  5. 数据结构&字符串:字典树

    前缀树里面可以存一堆字符串,也可以说是一堆单词,存完之后我们可以轻松判断一个指定的字符串是否出现过 下面我来详细解释一下实现细节 *+; //单词个数*每一个单词的字符数 ; struct Trie ...

  6. [洛谷P2023] [AHOI2009]维护序列

    洛谷题目链接:[AHOI2009]维护序列 题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,-,aN .有如下三种操作形式: (1)把数列 ...

  7. 发福利喽稀疏FFT

    附介绍: 四位来自麻省理工学院的研究人员蒂娜·卡塔比(Dina Katabi).海塞姆·哈桑(Haitham Hassanieh).比欧特·因迪克(Piotr Indyk)和埃里克·普里斯(Eric ...

  8. Unity下的ECS框架 Entitas简介

    最近随着守望先锋制作组在gdc上发布的一个关于ecs的talk,ecs这个架构算是得到了一定的曝光度. 在这之前,github上就一直有一个C#的ecs框架名为Entitas,截止现在已经有1300+ ...

  9. 【nginx】nginx的安装及测试

    nginx中文文档:http://www.nginx.cn/doc/index.html 1.到官网下载nginx的压缩包: https://nginx.org/   2.解压到相应的目录,比如我是e ...

  10. LCD实验学习笔记(五):MMU

    内存管理分别页表机制和内存分配机制两块. 页表机制就是管理设备真实物理地址与虚拟地址的动态或静态的映射,基于cpu内部的mmu(内存管理单元)进行. CP15(协处理器)的C0(缓存)是一级页表,含4 ...