The length of the factorial of 1058 N (51nod)

Posted by opencombatclan on Thu, 02 Jan 2020 13:37:25 +0100

Enter the length of the decimal representation of the factorial of N. For example, 6! = 720 and the length is 3.

Input

Enter N(1 < = N < = 10 ^ 6)

Output

Length of factorial of output N

Input example

6

Output example

3

Related issues

Factorial mod P of N

0

 Factorial of N

0

 The length of N's factorial

0

 Factorial V2 of N

320

The length of N's factorial V2 (Stirling approximation)

0

At the beginning of the problem, I used array processing, and the result was TLE every time
 

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
const int maxn=1e7;
#define ll long long
int a[maxn];

void cn(int n)
{
    int m=0;
    int c;
    a[0]=1;

    for(;n>=2;n--)
    {
        c=0;
        for(int i=0;i<=m;i++)
        {
            c+=a[i]*n;
            a[i]=c%10000;
            c=c/10000;
        }
        //if(c>0)
        // a[++m]=c;
        for(;c;c/=10000)
            a[++m]=c%10000;
    }
    int tem=a[m];
    int ans=0;
    while(tem)
    {
        ans++;
        tem/=10;
    }
    printf("%d\n",ans+m*4);
}

int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
       cn(n);
    }
    return 0;
}

Then I looked at the comment area: a few lines of other people's code were O ~ (@ ^ @)~

#include<cstdio>
#include<iostream>
#include<cmath>
//#define _USE_MATH_DEFINES
using namespace std; 
/*
sqrt(2*pi*n)*pow(e/n,n) 
*/
int main(){
	long long n;
	cin>>n;
	long long l=log10((long double)(sqrt(2*M_PI*n)))+n*log10((long double)n/M_E);
//	cout<<M_E<<endl;
	cout<<l+1<<endl;
	
}

It's from a mathematical formula

Some people also write as follows:
 

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int maxm = 210;
const double pi = acos(-1);
const double e = exp(1);

int main()
{
    int n;
    cin>>n;
    int len = log10(2.0*pi*n)*0.5 + n*log10(n/e) + 1;
    if(n==1)len=1;
    cout<<len<<endl;
    return 0;
}