Factorial

Problem Description

The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.

ACM technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called “Travelling Salesman Problem” and it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product 1.2.3.4….N. The number is very high even for a relatively small N.

The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behaviour of the factorial function.

For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1

Input

There is a single positive integer T on the first line of input. It stands for the number of numbers to follow. Then there is T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.

Output

For every number N, output a single line containing the single non-negative integer Z(N).

Sample Input

6

3

60

100

1024

23456

8735373

Sample Output

0

14

24

253

5861

2183837


解题心得:

  1. 题意就是叫你算N!的答案末尾有多少个0,很水的题,别人的分析很好我就引用一下就不写分析了。

分析:题目要求解的是N阶乘的结果有多少个0?(1<=N<=1000000000)

注意一下几个方面:


1、任何一个自然数都可分解质因数。N!=1*2*3*(2*2)5(2*3)*…*N=2^a*3^b*5^c*7^d……=(2*5)^c*2^(a-c)*3^b*7^d……=10^c*2^(a-c)*3^b*7^d……

2、两数相乘产生0,是因为2和5相乘。又由于在分解质因数时小的质数的幂次一定>=大的质数的幂次,在N!中2的个数显然大于5的个数,故解决该题转化成找出N!中5的幂次。

3、如何找出5的幂次呢?其实就是 N!中:是5的倍数的数+是5^2的倍数的数+5^3的倍数的数+…..

如50!中:

含有10个5的倍数的数:5,15,20,25,30,35,40,45,50 [50/5=10]

含有2个5^2的倍数的数:25,50 [50/(5^2)=2]

可见N!中一共有12个5相乘,那么N!结果中的0也必有12个。


#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n;
int t;
cin>>t;
while(t--)
{
cin>>n;
long long sum = 0;
while(n)
{
sum += n / 5;
n /= 5;
}
printf("%lld\n",sum);
}
return 0;
}

数学基础:HUD1124-Factorial(N!末尾0的个数)的更多相关文章

  1. 【CodeChef】Factorial(n!末尾0的个数)

    The most important part of a GSM network is so called Base Transceiver Station (BTS). These transcei ...

  2. POJ 1401:Factorial 求一个数阶乘的末尾0的个数

    Factorial Time Limit: 1500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 9349 Descri ...

  3. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

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

  4. N的阶乘末尾0的个数和其二进制表示中最后位1的位置

    问题一解法:     我们知道求N的阶乘结果末尾0的个数也就是说我们在从1做到N的乘法的时候里面产生了多少个10, 我们可以这样分解,也就是将从0到N的数分解成因式,再将这些因式相乘,那么里面有多少个 ...

  5. LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1090 题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾 ...

  6. 神秘常量复出!用0x077CB531计算末尾0的个数 -- De Bruijn 序列

    http://www.matrix67.com/blog/archives/3985 神秘常量复出!用0x077CB531计算末尾0的个数 大家或许还记得 Quake III 里面的一段有如天书般的代 ...

  7. Algorithm --> 求阶乘末尾0的个数

    求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...

  8. 求N的阶乘N!中末尾0的个数

    求N的阶乘N!中末尾0的个数 有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N=3628800,则N!的末尾有两个0:直接上干货,算法思想如下:对于任意一个正 ...

  9. 牛客小白月赛6 水题 求n!在m进制下末尾0的个数 数论

    链接:https://www.nowcoder.com/acm/contest/135/C来源:牛客网 题目描述 其中,f(1)=1;f(2)=1;Z皇后的方案数:即在Z×Z的棋盘上放置Z个皇后,使其 ...

随机推荐

  1. kali linux安装搜狗输入法

    1. 更新软件源 vi /etc/apt/sources.list 2. 安装fcitx apt-get install fcitx 3. 下载deb包到指定目录, dpkg -i sogou**** ...

  2. 安卓H5软键盘遮挡输入框

    由于安卓app内嵌入H5页面,webview对于软键盘没有处理(如果不是产品强烈要求建议不要处理这种拆东墙补西墙的问题,因为其他的手机上可能会出现已经优化软键盘的情况) 1.input下方还有多余空位 ...

  3. [转]使用 HTML5 WebSocket 构建实时 Web 应用

    HTML5 WebSocket 简介和实战演练 本文主要介绍了 HTML5 WebSocket 的原理以及它给实时 Web 开发带来的革命性的创新,并通过一个 WebSocket 服务器和客户端的案例 ...

  4. Java获取服务器系统默认编码格式

    大佬教的,做个笔记方法一(推荐):新建一个jsp页面在webapp下然后添加 <% out.print(System.getProperties().getProperty("file ...

  5. bug {was not declared in this scope}

    使用自己定义的结构体作为返回值的时候,出现了 ...was not declared in this scope 检查了各种头文件,把缓存也都删掉了还是不行. 结果,发现,应该这样用vector< ...

  6. 解决ajax 遇到session失效后自动跳转的问题

    在项目中,经常会遇到session失效后,点击任何链接无反应的情况!这样给客户的体验就不是很好,以为是系统出了故障!所以在项目中我们会处理session失效后的跳转问题(一般给用户提示,并跳转后登录页 ...

  7. 深入理解Java流机制(一)

    一.前言 C语言本身没有输入输出语句,而是调用"stdio.h"库中的输入输出函数来实现.同样,C++语言本身也没有输入输出,不过有别于C语言,C++有一个面向对象的I/O流类库& ...

  8. img标签src资源无法加载,报net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION错

    html代码: chrome和360浏览器均报错,系统自带IE.Firefox浏览器没有问题 原因:加载的资源名含有半角逗号(,)或者别的特殊符号 解决办法:后台给资源名加上双引号("&qu ...

  9. C#去掉字符串最后面的一个标点符号的写法

    keywordHtml = keywordHtml.Remove(keywordHtml.LastIndexOf(','),1);

  10. UVA 11992 Fast Matrix Operations (降维)

    题意:对一个矩阵进行子矩阵操作. 元素最多有1e6个,树套树不好开(我不会),把二维坐标化成一维的,一个子矩阵操作分解成多条线段的操作. 一次操作的复杂度是RlogC,很容易找到极端的数据(OJ上实测 ...