HDU1124 Factorial

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

Code:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>

using namespace std;

/*N!末尾的0一定是由2*5产生的。

而且2因子的个数一定比5因子的个数多。

所以只需要求N!的5因子的个数。

用到了一个数论知识:

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

x=[n/p]+[n/p^2]+[n/p^3]+............;
int a[50000];*/

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long n;
        scanf("%ld",&n);
        int cnt=0,k=n/5;
        while(k>0)
        {
            cnt+=k;
            k/=5;
        }
        printf("%d\n",cnt);
    }
    return 0;
}
时间: 2024-10-06 00:28:09

HDU1124 Factorial的相关文章

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 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. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

Project Euler:Problem 74 Digit factorial chains

The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145: 1! + 4! + 5! = 1 + 24 + 120 = 145 Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it tu

[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. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数

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,

SPOJ FCTRL - Factorial

题目链接:http://www.spoj.com/problems/FCTRL/ 题目大意:询问N的阶乘的结果尾数有几个0. 解题思路:考虑问题:N的阶乘的结果能被2m整除,这个m最大为多少. 我们对前N个数除以2,忽略奇数,会得到N/2个数字.那么相当于我们得到了2N/2 对之后的N/2个数字继续除以2,同样忽略奇数.我们会再得到2N/4 ... 所以m=N/2 + N/4 + N/8 +...+0. 那么对于这个问题,我们计算出N的记成被2m1整除的最大m1以及被5m2整除的最大m2,由于2

Factorial Trailing Zeroes

Factorial Trailing Zeroes Total Accepted: 44612 Total Submissions: 144778 Difficulty: Easy Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (M) Number of Digit One n!的结果中0的个

Codeforces 515C. Drazil and Factorial

//codeforces 515C. Drazil and Factorial #include <iostream> #include <cstring> #include <cstdio> using namespace std; /** 4!=2!*2!*3! 6!=1*2*3*4*5*6=5!*3! 8!=1*2*3*4*5*6*7*8=7!*2!*2!*2! 9!=1*2*3*4*5*6*7*8*9=7!*2!*3!*3! */ int main() { ch

Hex Factorial 高精度

Hex Factorial 求n的阶乘结果的十六进制表示中,用多少个0.       java秒之! 1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 5 public class Main 6 { 7 public static void main(String[] args) 8 { 9 Scanner cin=new Scanner(new BufferedInputStream(System.in)); 1

how many does the factorial of n have zero?

how many does the n factorial end have zero? 个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:how many does the factorial of n have zero? 博客时间:2014-5-5; 编程语言:Java ; 编程坏境:Windows 7 专业版 x64; 编程工具:jdk,ec