【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的个数了。
该题的代码如下:
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的个数)的更多相关文章
- POJ 1401:Factorial 求一个数阶乘的末尾0的个数
Factorial Time Limit: 1500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 9349 Descri ...
- 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 ...
- N的阶乘末尾0的个数和其二进制表示中最后位1的位置
问题一解法: 我们知道求N的阶乘结果末尾0的个数也就是说我们在从1做到N的乘法的时候里面产生了多少个10, 我们可以这样分解,也就是将从0到N的数分解成因式,再将这些因式相乘,那么里面有多少个 ...
- LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数
题目链接:http://lightoj.com/volume_showproblem.php?problem=1090 题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾 ...
- 神秘常量复出!用0x077CB531计算末尾0的个数 -- De Bruijn 序列
http://www.matrix67.com/blog/archives/3985 神秘常量复出!用0x077CB531计算末尾0的个数 大家或许还记得 Quake III 里面的一段有如天书般的代 ...
- Algorithm --> 求阶乘末尾0的个数
求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...
- 求N的阶乘N!中末尾0的个数
求N的阶乘N!中末尾0的个数 有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N=3628800,则N!的末尾有两个0:直接上干货,算法思想如下:对于任意一个正 ...
- 牛客小白月赛6 水题 求n!在m进制下末尾0的个数 数论
链接:https://www.nowcoder.com/acm/contest/135/C来源:牛客网 题目描述 其中,f(1)=1;f(2)=1;Z皇后的方案数:即在Z×Z的棋盘上放置Z个皇后,使其 ...
- 求一个数的阶乘在 m 进制下末尾 0 的个数
题意 : 求一个数 n 的阶层在 m 进制下末尾 0 的个数 思路分析 : 如果是 10 进制地话我们是很容易知道怎么做的,数一下其对 5 约数地个数即可,但是换成 m 进制的话就需要先将 m 分解质 ...
随机推荐
- Git使用技巧(1)-- 配置【持续更新】
配置名字和邮箱 git config --global user.name "Your Name" git config --global user.email "ema ...
- ps -aux --sort -rss |head 列出进程拿物理内存占用排序 使用ps aux 查看系统进程时,第六列即 RSS列显示的就是进程使用的物理内存。
ps -a Select all processes -u userlist Select by effective user ID (EUID) or name. ...
- java 调用cmd命令
public class Port{ public static void main(String[] args) { Runtime runtime=Runtime.getRuntime(); tr ...
- samba在linux下的配置
Samba配置过程容易遇到的问题 samba的安装很简单,搜索一下都能找到.我主要是记录一上我碰到的问题及解决的办法 第一个问题 防火墙的问题,粗暴的办法是直接把防火墙关掉,合适的做法是把samba用 ...
- Eclipse 创建 Java 类
打开新建 Java 类向导 你可以使用新建 Java 类向导来创建 Java 类,可以通过以下途径打开 Java 类向导: 点击 "File" 菜单并选择 New > Cla ...
- linux oracle sqlplus中文乱码解决
在oracle用户的~/.bash_profile中添加 NLS_LANG="SIMPLIFIED CHINESE"_CHINA.ZHS16GBKexport NLS_LANG 然 ...
- android代码中自定义布局
转载地址:http://blog.csdn.net/luckyjda/article/details/8760214RelativeLayout rl = new RelativeLayout(thi ...
- WPF命令绑定 自定义命令
WPF的命令系统是wpf中新增加的内容,在以往的winfom中并没有.为什么要增加命令这一块内容.在winform里面的没有命令只使用事件的话也可以实现程序员希望实现的功能.这个问题在很多文章中都提到 ...
- 【BZOJ4821】[Sdoi2017]相关分析 线段树
[BZOJ4821][Sdoi2017]相关分析 Description Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. ...
- 【BZOJ2819】Nim 树状数组+LCA
[BZOJ2819]Nim Description 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可 ...