HDU 1124 Factorial(简单数论)

Factorial

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4237    Accepted Submission(s): 2805

Problem Description

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.
ACM 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 "Travelling 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 behaviour 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. It stands for the number of numbers to follow. Then there is 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).

Sample Input

6
3
60
100
1024
23456
8735373

Sample Output

0
14
24
253
5861
2183837

Source

Central Europe 2000

Recommend

Eddy   |   We have carefully selected several similar problems for you:  1128 1113 1032 1115 1319

题目问 N!的末尾有多少个零,我们可以把所有的0都看成 2*5产生的 ,10也看成2*5,那么2的因子远远大于5的

因子,我们只需要找到5的因子的个数

这里引用一下:

若p是质数,p<=n,则n!是p的倍数,设p^x是p在n!内的最高幂,则

x=[n/p]+[n/p^2]+[n/p^3]+............;

时间: 2024-07-28 13:48:33

HDU 1124 Factorial(简单数论)的相关文章

HDU 1124 Factorial (数论)

http://acm.hdu.edu.cn/showproblem.php? pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長.我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙下~~~哈哈,所以我就猥瑣的叫 旁邊的小師妹幫我翻譯了,我是不是非常禽獸,嘻嘻~~~ 題目大意呢,就是給一個數,要你求出它的階乘的得到的結果後面有幾個0. 解析: 一看就是簡單數論啦.跟數因子有關.最小素因子并且相乘能得到10的(就是後面有0的)就是2*5啦.因為一個數的階乘2的因子明顯比5的因子要

hdu 1124 Factorial 数论,就是求一个数的阶乘的结果末尾有多少0.

Factorial Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2990    Accepted Submission(s): 1921 Problem Description The most important part of a GSM network is so called Base Transceiver Station

HDU 1124 Factorial (數論)

http://acm.hdu.edu.cn/showproblem.php?pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長,我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙下~~~哈哈,所以我就猥瑣的叫 旁邊的小師妹幫我翻譯了,我是不是很禽獸,嘻嘻~~~ 題目大意呢,就是給一個數,要你求出它的階乘的得到的結果後面有幾個0: 解析: 一看就是簡單數論啦,跟數因子有關,最小素因子而且相乘能得到10的(就是後面有0的)就是2*5啦,因為一個數的階乘2的因子明顯比5的因子要多得

hdu 1214 圆桌会议 简单数论

圆桌会议 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3328    Accepted Submission(s): 2328 Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家的讨论后一般没有

HDU 1124 Factorial (阶乘后缀0)

题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将十进制质因数分解得2*5=10.将n!质因数分解,那么分解后,其中应含有min(2个数,5个数)个后缀0. 为何这么说?例如n=15,那么{1 2 3 4 5 6 7 8 9  10 11 12 13 14 15},那么可以产生2的数字有{2,4,6,8,10,12,14},可以产生5的只有{5,10,15},质数中只有2乘以5才能形成10,因为素数只有2是偶数!!!那么min(2个数,5个数)就决定了可以产生10的个数,也就决定了

hdu 1220 Cube 简单数论

Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1516    Accepted Submission(s): 1206 Problem Description Cowl is good at solving math problems. One day a friend asked him such a question:

hdu 1395 2^x mod n = 1 (简单数论)

题目大意: 求出一个最小的x 使得 2的x次方对n取模为1 思路分析: 若要 a*b%p=1  要使得b存在 则 gcd (a,p)=1. 那么我们应用到这个题目上来. 当n为偶数 2^x 也是偶数,那么gcd 肯定不是1.故这个是不存在的. 那么n为奇数的时候,也就一定是1了. 所以直接暴力找. #include <iostream> #include <cstdio> using namespace std; int main() { int n; while(scanf(&q

hdu 1124 数论

题意:求n!中末尾连续0的个数  其实就是2*5的个数 30! 中有5 10 15 20 25 30  是5的倍数有6个   6=30/5; 6/5=1; 这个1 为25 5  10 15 20  25  30 35 40 45 50 55 60  65 70 75 80  85 90 95 100      100/5=20; 25                     50                    75                     100       20/5=4

POJ 1775 sum of Factorial (数论)

链接:http://poj.org/problem?id=1775 Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics,meteorology, science,