LightOj 1045 Digits Of Factorial



Number Of Factorial Digits = log(n!)+1 . As n is very large for factorial  so we can write as below

log(5!)=log(5*4*3*2*1)=log1+log2+log3+log4+log5




#include <bits/stdc++.h>
using namespace std;
long double v[1000005];
int main()
{
    long long int  t,n,b,i,j,d;
    
    //freopen("1045in.txt","r",stdin);
    //freopen("1045out.txt","w",stdout);
    cin>>t;

    for(j=1;j<=1000000;j++)
    {
       v[j]=log10(j)+v[j-1];

    }
    for(i=1;i<=t;i++)
    {
        
        cin>>n>>b;
       
        d=floor(v[n]/log10(b))+1;

        printf("Case %lld: %lld\n",i,d);

    }
    return 0;
}

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment