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.

The 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 "Traveling 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 behavior 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<N2, then Z(N1) <= Z(N2). It is because we can never "lose" any
trailing zero by multiplying by any positive number. We can only get new
and new zeros. The function Z is very interesting, so we need a
computer program that can determine its value efficiently.

Input

There is a single positive integer T on the first line of input
(equal to about 100000). It stands for the number of numbers to follow.
Then there are 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).


题解:题目很长,其实就一句话:计算n!末尾0的个数。

一个主要的思想就是n!中有多少个5,末尾就有多少个0。

首先通过将n分解因式,我们知道上述充分性是成立的,因为所有末尾的0都可以看成因子10,而10可以分解为2*5,所以末尾的一个0必然对应这一个5。而每个0也必然来自一个因子5。那么有个问题,就是n!中是否有足够的2把所有的5都变成10呢?答案是肯定的:对于任意n>=5,有n = (5*10*15*......*5(k-1)*5k)*a,其中a是不能被5整除的整数。那么对于上述序列5,10,15,......,5(k-1),5k中的每一个5,在区间(5(i-1),5i]中必存在一个偶数,这个偶数中的2就可以把这个5变成结尾的0了。

所以,对于n!中任意一个因子5,对应着一个末尾0,那么我们只要求出n!中有多少个因子5,就知道它末尾有多少个0了。

假设n!中有f(n!)个5,那么有f(n!) = (n!/5) + f(n!/5);所以就可以用递归的方法求解n!中5的个数了。

该题的代码如下:

 import java.util.Scanner;

 public class Main {
private static int end_zeros(int num) {
if(num <= 4)
return 0;
else
return num/5 + end_zeros(num/5);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while(t-- >0 )
{
int num = scanner.nextInt();
System.out.println(end_zeros(num));
}
} }

【CodeChef】Factorial(n!末尾0的个数)的更多相关文章

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

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

  2. 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 ...

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

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

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

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

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

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

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

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

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

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

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

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

  9. 求一个数的阶乘在 m 进制下末尾 0 的个数

    题意 : 求一个数 n 的阶层在 m 进制下末尾 0 的个数 思路分析 : 如果是 10 进制地话我们是很容易知道怎么做的,数一下其对 5 约数地个数即可,但是换成 m 进制的话就需要先将 m 分解质 ...

随机推荐

  1. 什么时候触发MinorGC?什么时候触发FullGC?

    触发MinorGC(Young GC) 虚拟机在进行minorGC之前会判断老年代最大的可用连续空间是否大于新生代的所有对象总空间 1.如果大于的话,直接执行minorGC 2.如果小于,判断是否开启 ...

  2. Spring MVC资源绑定视图解析器

    ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称. 以下示例显示如何使用Spring Web MVC框架中的ResourceBundleViewRes ...

  3. Unity3D学习笔记——IDE工作视图

    Unity3D中五个界面的使用: Project视图:存放游戏资源,比如贴图,音频,JS脚本等 Project中可创建的文件如下: Hierarchy视图:主要存放游戏场景中的对象,如摄像机,精灵,箱 ...

  4. SlidingMenu官方实例分析6——ResponsiveUIActivity

    ResponsiveUIActivity 这个类实现的是一个响应适UI设计重点是布局的设计: layout布局如下: layout-large-land布局如下: layout-xlarge布局如下: ...

  5. OpenCV学习笔记廿一:opencv_contrib模块

    一,简介: 该库为新加入代码库的算法.

  6. OpenCV学习笔记十七:opencv_bioinspired模块

    一,简介: 该库是基于仿生学的图像/视频处理库,目前包含模拟人类视网膜的算法.

  7. Handler classes should be static or leaks might occur

    http://droidyue.com/blog/2014/12/28/in-android-handler-classes-should-be-static-or-leaks-might-occur ...

  8. Highway

    Highway Accepted : 78   Submit : 275 Time Limit : 4000 MS   Memory Limit : 65536 KB Highway In ICPCC ...

  9. oracle clob字段去除html标签

    通过正则表达式的方式去除html标签 select regexp_replace(content,'</?[^>]*>|nbsp;|&','') content from T ...

  10. 解决asp.net中HTML中talbe的行高被内容撑的变高的问题

    将asp.net页面中的如下语句: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...