【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 分解质 ...
随机推荐
- Colossal Fibonacci Numbers! UVA 11582 寻找循环节
/** 题目:Colossal Fibonacci Numbers! UVA 11582 链接:https://vjudge.net/problem/UVA-11582 题意:f[0] = 1, f[ ...
- Android开发教程:shape和selector的结合使用(转载)
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...
- SQLServer -- 竟然默认不区分大小写
SELECT * FROM USER_INFO WHERE USERNAME = :username; 这样的写法,:username的值竟然不区分大小写 原因:数据库的排序规则设置的是Chinese ...
- [浪风推荐]php的memcache应用入门教程
所需环境: php 5.3.3 apache 2.2.7 mysql 5.5.8 解压Memcached_1.2.5文档,cmd下执行memcached.exe -d -install 将php5.3 ...
- visualvm远程监控jvm两种配置方法
参考:http://blog.itpub.net/17203031/viewspace-765810 一.Jstatd RMI远程监控方法 VisualVM在监控本地JVM的时候是很方便的.只要应用程 ...
- PYTHON -创建 表 和 插入 数据
import sqlite3 conn = sqlite3.connect('y_user_data2.db') cursor = conn.cursor() #create tablecursor. ...
- [转]libevent 环境配置
libevent 据说是IO复用的好东西.所以今天来耍耍. 1. 从官网下载源代码:http://libevent.org/,最新的版本已达到2.0. 2. 先把ubuntu系统自带的libevent ...
- Coursera课程《Machine Learning》学习笔记(week1)
这是Coursera上比较火的一门机器学习课程,主讲教师为Andrew Ng.在自己看神经网络的过程中也的确发现自己有基础不牢.一些基本概念没搞清楚的问题,因此想借这门课程来个查漏补缺.目前的计划是先 ...
- shell脚本中格式化日期
date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] ...
- Arcgis server服务启动后停止
---恢复内容开始--- 重新按装server10.1,10sp1和10.2 易出现服务无法启动.在管理中手动启动服务,出现如图1所示的错误. 图1 错误信息 本人多次遇到用户反馈这问题,最初以为是因 ...