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

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的个数了。

该题的代码如下:

 1 import java.util.Scanner;
 2
 3 public class Main {
 4     private static int end_zeros(int num) {
 5         if(num <= 4)
 6             return 0;
 7         else
 8             return num/5 + end_zeros(num/5);
 9     }
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner scanner = new Scanner(System.in);
13         int t = scanner.nextInt();
14         while(t-- >0 )
15         {
16             int num = scanner.nextInt();
17             System.out.println(end_zeros(num));
18         }
19     }
20
21 }

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

时间: 2024-10-17 10:26:03

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

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 logarithmic time complexity. 分析 Note中提示让用对数的时间复杂度求解,那么如果粗暴的算出N的阶乘然后看末尾0的个数是不可能的. 所以仔细分析,N! = 1 * 2 * 3 * ... * N 而末尾0的个数只与这些乘数中5和2的个数有关,因为每出现一对5和2就会产生

算法-计算阶乘n!末尾0的个数

算法逻辑转载自计算阶乘n!末尾0的个数: 问题描述    给定参数n(n为正整数),请计算n的阶乘n!末尾所含有"0"的个数.    例如,5!=120,其末尾所含有的"0"的个数为1:10!= 3628800,其末尾所含有的"0"的个数为2:20!= 2432902008176640000,其末尾所含有的"0"的个数为4. 计算公式    这里先给出其计算公式,后面给出推导过程.    令f(x)表示正整数x末尾所含有的&q

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

有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N!=3628800,则N!的末尾有两个0: 直接上干货,算法思想如下: 对于任意一个正整数N!,都可以化为N!= (2^X)  * (3^Y)* (5^Z)......的形式,要求得末尾0的个数只需求得min(X, Z)即可, 由于是求N!,则X >= Z; 即公约数5出现的频率小于等于2出现的频率,即Z=min(X, Z),即出现0的个数等于公约数5出现的次数: 源码如下: 方法一: #include <

N!结果中末尾0的个数 2.2

主要是看N!结果中2的个数和5的个数,多的那个个数即是末尾0的个数 ? ? ? ? 计算Z有两种方法 ? ? 一种是对每个n都去看有多少个5的因子 ? ? 一种是隔5个增加一个5的因子,隔25个再在之前的基础上增加一个5的因子 ? ? 两种方法差不多 ? ? 第二种,循环少 ? ? package numOfZeroFactorialN_2_2; ? ? public class NumOfZeroFactorialN_2_2 { ? ? static int numOfZeros(int n)

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

问题一解法: 我们知道求N的阶乘结果末尾0的个数也就是说我们在从1做到N的乘法的时候里面产生了多少个10, 我们可以这样分解,也就是将从0到N的数分解成因式,再将这些因式相乘,那么里面有多少个10呢? 其实我们只要算里面有多少个5就可以了? 因为在这些分解后的因子中,能产生10的可只有5和2相乘了,由于2的个数是大于5的个数的,因此 我们只要算5的个数就可以了.那么这个题目就是算这些从1到N的数字分解成因子后,这些因子里面 5的个数. Python代码 def factorialnumberof

求一个数阶乘末尾0的个数

#include<iostream> using namespace std; //给定一个整数N,那么N的阶乘末尾有几个0?N=10,N!=3628800,末尾有2个0 //1.如果我们从"哪些数相乘能得到 10"这个角度来考虑,问题就变得简单了. //首先考虑,如果 N!= K×10M,且 K 不能被 10 整除,那么 N!末尾有 M 个 0.再考虑 //对 N!进行质因数分解,N!=(2x)×(3y)×(5z)-,由于 10 = 2×5,所以 M 只跟 X 和 Z /

poj1401--Factorial--阶乘末尾0的个数

Description 求出n!的末尾有多少个0(连续的). 每组测试点有t个测试数据,输入格式为第一行一个t,后面2~t+1行每行一个n,输出其结果. Sample Input 6 3 60 100 1024 23456 8735373 Sample Output 0 14 24 253 5861 2183837 题解: 求一个数阶乘的末尾0的个数. 10=2*5,显然2的个数总比5多, 即转化为,求阶乘分解以后有几个5. #include<iostream> #include<cma

阶乘末尾0的个数(证明)

先给出算法: 给定n,求n的阶乘末尾0的个数. int res = 0; while (n > 0) { res += n / 5; n /= 5; } 因为: 比方说求15的阶乘,也就是求 1 × 2 × 3 × 4 × 5 × 6 × 7 × 8 × 9 × 10 × 11 × 12 × 13 × 14 × 15 的末尾0的个数.现在我们把这15个数分解出来含有5的因子 1 × 2 × 3 × 4 × 5 × 6 × 7 × 8 × 9 × 2   × 11 × 12 × 13 × 14 ×

计算n的阶乘(n!)末尾0的个数

题目: 给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数. 举例: 5!=120,其末尾所含有的“0”的个数为1: 10!= 3628800,其末尾所含有的“0”的个数为2: 20!= 2432902008176640000,其末尾所含有的“0”的个数为4 解题思路: 两个大数字相乘,都可以拆分成多个质数相乘,而质数相乘结果尾数为0的,只可能是2*5.如果想到了这一点,那么就可以进一步想到:两个数相乘尾数0的个数其实就是依赖于2和5因子的个数.又因为每两个连续数字就会有一个因子2,个数