Wednesday, March 9, 2016

UVa 1185 - Big Numbers

#include <bits/stdc++.h>
#define LL  long long
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
#define priii(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
#define hlw printf("Hello World\n");
#define pcs printf("CASE# %d:\n", ++cs);
#define WRITE(fn) freopen(fn, "w", stdout);
using namespace std;

/****/ int cs = 0;
const int INF = 1<<29;
const int MX  = 1e7+10;

/*
Algorithm:
We know that log(a*b) = log a + log b, so log n! = log 1 + log 2 + ... + log n.
Further more, we can get how many digits a number has by taking logarithm operation with a base of 10.
Also, there's a recursive function that N! = (N-1)! * N.
*/

double ans[MX];

void precal()
{
    ans[1] = log(1);
    FOR(i,2,MX)
    {
        ans[i] = ans[i-1] + log10(i);
    }
    return;
}

int main()
{
    precal();

    int T,x;
    cin>>T;
    while(T--)
    {
        cin>>x;
        printf("%d\n", (int)ans[x] + 1);
    }

    return 0;
}

No comments:

Post a Comment