• 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;
    }
  • 相关阅读:
    ImportError: cannot import name 'render_to_response' 解决方法
    python 操作zookeeper详解
    Appium swipe实现屏幕滑动
    Appium 环境配置
    pytest 常用方法介绍
    Allure+pytest 生成测试报告
    Pytest 使用简介
    python 实现线程之间的通信
    python 使用 thrift 教程
    nginx 配置简单反向代理
  • 原文地址:https://www.cnblogs.com/jackge/p/3048395.html
Copyright © 2020-2023  润新知