zoukankan      html  css  js  c++  java
  • poj1401 Factorial

    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

    这道题的题意就是输入N,然后判断N!有几个零。

    两数相乘产生零那就只有偶数和5相乘了,每乘一次5,就会在后面多加个零,这样的话,想要求N!中零的个数,就要用的N!中5的次幂。

    N!中零的个数和就是 N/5+N/25+N/125 ……+N/(5^K)

    N < 5^K

    这里面还有一个优化问题,我一开始写的时候时间超限了,下面第一个代码就是我时间超限的代码。

    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    #include <cstring>
    #include <math.h>
     
    using namespace std;
     
    int main(void)
    {
        long long k,z,n,m,p,f;
        scanf("%lld",&n);
        while(n--)
        {
            scanf("%lld",&m);
            z = 0;
            for(long long i = 5,j; i <= m; i += 5)
            {
                j = 1;
                p = 0;
                f = 0;
                while(j <= m)
                {
                    j *= 5;
                    p++;
                    if(i % j == 0)
                    {
                        f = p;
                    }
                }
                z += f;
            }
            printf("%lld
    ",z);
        }
        return 0;
    }

    然后第二个代码就是优化之后的了,不管是空间复杂度还是时间复杂度都减小了

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <stdio.h>
     4 #include <cstring>
     5 #include <math.h>
     6  
     7 using namespace std;
     8  
     9 int main(void)
    10 {
    11     long long k,z,n,m,p,f;
    12     scanf("%lld",&n);
    13     while(n--)
    14     {
    15         scanf("%lld",&m);
    16         z = m/5;
    17         for(long long i = 25,j; i <= m; i *= 5)
    18         {
    19             z += m/i;
    20         }
    21         printf("%lld
    ",z);
    22     }
    23     return 0;
    24 }
  • 相关阅读:
    获取地址栏数据
    tag标签添加删除并把值存入到一个input的value内
    美化 input type=file控件
    高效的数组去重(js)
    原生js动态改变dom高度
    html5 postMessage解决跨域、跨窗口消息传递
    移动前端制作篇之javascript篇
    js中的事件委托
    图片轮播(定时播放)
    潭州课堂25班:Ph201805201 django 项目 第四十一课 后台 轮播图管理功能讲解,文档管理功能 实现 (课堂笔记)
  • 原文地址:https://www.cnblogs.com/iambupu/p/4514679.html
Copyright © 2011-2022 走看看