【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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了