zoukankan      html  css  js  c++  java
  • POJ 1423 Big Number

    Big Number

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 1   Accepted Submission(s) : 1
    Problem Description
    In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
     
    Input
    Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 <= m <= 10^7 on each line.
     
    Output
    The output contains the number of digits in the factorial of the integers appearing in the input.
     
    Sample Input
    2 10 20
     
    Sample Output
    7 19
     
    Source
    PKU
     
     

    给一个数n,求n的阶乘的位数;

    直接高精度计算显然很复杂,换个思路,对于一个数m,m的位数就是(int)log10(m)+1。如果m=n*(n-1)*(n-2)*......*1,那么log10(m)=log10(n)+

    log10(n-1)+.....+log10(2)+log10(1)。即m的位数是( int )( log10(n)+log10(n-1)+.....+log10(2)+log10(1) )+1。由于题目要求的m很大,独立计算会造成重复计算某些值,所以采用预处理出,所有的结果。

    //先将所以要求的数都存起来,由于最后要按输入顺序输出结果,所以还要存入id
    //按值从小到大顺序排列,先计算最小值的对数,后面的数都建立在他的基础上,
    //和上述代码思路相同。最后按id从小到大排列输出结果即可,这样可以避免对内存的消耗

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    
    using namespace std;
    
    struct node{
        int id,num;
        double result;
    }a[1010];
    
    int flag;
    
    int cmp(node c,node d){
        if(flag)
            return c.num<d.num;
        return c.id<d.id;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n;
        while(~scanf("%d",&n)){
            for(int i=0;i<n;i++){
                scanf("%d",&a[i].num);
                a[i].id=i;
                a[i].result=0;
            }
            flag=1;
            sort(a,a+n,cmp);
            for(int i=1;i<=a[0].num;i++)
                a[0].result+=log10(i);
            for(int i=1;i<n;i++){
                a[i].result=a[i-1].result;
                for(int j=a[i-1].num+1;j<=a[i].num;j++)
                    a[i].result+=log10(j);
            }
            flag=0;
            sort(a,a+n,cmp);
            for(int i=0;i<n;i++)
                printf("%d\n",(int)a[i].result+1);
        }
        return 0;
    }
  • 相关阅读:
    静态页转换平台(StaticPol)-静态页生成终极解决方案
    用DataSetSurrogate加速WebService
    压缩和解压缩数据流
    Stream 和 byte[] 之间的转换(转)
    优化网站性能 提高网站速度访问速度的14条实践(转)
    常用WebServices返回数据的4种方法比较
    .Net 中的序列化与反序列化
    对象序列化反序列化
    ASP.Net中自定义Http处理及应用之HttpModule篇
    利用UrlRewrite,asp.net动态生成htm页面
  • 原文地址:https://www.cnblogs.com/jackge/p/3048395.html
Copyright © 2011-2022 走看看