zoukankan      html  css  js  c++  java
  • HDOJ(1018)

    Big Number

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


    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 ≤ n ≤ 107 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
     

    数学公式推导

    方法一:

    ①:10^M < n!   <10^(M+1)  若求得M,则M+1即为答案。

    对公式①两边以10为底取对数

    M < log10(n!) < M+1

    因为 log10(n!)=log10(1)+log10(2)+……+log10(n)

    可用循环求得M+1的值

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            int n;
            cin>>n;
            double ans=0;
            for(int i=1;i<=n;i++)
            {
                ans+=log(i)/log(10);
            }
            cout<<(int)ans+1<<endl;
            
        }
        return 0;
    }

    方法二:斯特林公式
    n! ≈ sqrt(2*n*pi)*(n/e)^n

    则 M+1=(int)(0.5*log(2.0*n*PI)+n*log(n)-n)/(log(10.0)) )+1;

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const double PI=3.1415926;
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            int n;
            cin>>n;
            double ans;
            ans=(0.5*log(2.0*n*PI)+n*log(n)-n)/(log(10.0));
            cout<<(long)ans+1<<endl; 
        }
        return 0;
    }

     Java:

    import java.util.Scanner;
    public class Main{
        static Scanner cin=new Scanner(System.in);
        static final int MAXN=10000005;
        static int[] res=new int[MAXN];
        public static void main(String[] args){
            double pre=Math.log(1.0);
            res[1]=(int)pre+1;
            for(int i=2;i<=10000000;i++)
            {
                pre+=Math.log10((double)i);
                res[i]=(int)pre+1;
            }
            int T=cin.nextInt();
            while(T--!=0)
            {
                int n=cin.nextInt();
                System.out.println(res[n]);
            }
        }
    }
  • 相关阅读:
    springboot2.X动态修改log4j2日志级别
    iframe嵌套PMM2.0
    grafana配置告警
    prometheus+grafana配置流程
    kubernetes拉取私有镜像仓库的镜像
    Windows Server 2016离线安装.NET Framework 3.5
    Office批量授权(VL)版本和激活方法
    华为USG防火墙配置NAT映射回流解决内网通过公网映射访问内部服务器
    IRF配置
    CENTOS7安装各种桌面系统 CENTOS安装桌面图形化GUI GNOME/KDE/Cinnamon/MATE/Xfce
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4690303.html
Copyright © 2011-2022 走看看