poj1423---Ask for a large number of digits, I guess the algorithm that counts less than how many bits of input characters on the site

Source: Internet
Author: User

Law one: The logarithm of a number, +1 rounding is its number of digits

Problem converted to int (log10 (n!) +1), logarithmic property log10 (n!) =LOG10 (N) +log10 (N-1) +...+log10 (1)

/* Use LOG10 to find the number of digits */#include <stdio.h> #include <math.h>int main () {    int tim,n;    scanf ("%d", &tim);    while (tim--)    {        int i;        Double numofdigit=1;        scanf ("%d", &n);        for (i=n;i>=1;i--)        {            numofdigit+=log10 (i);        }        printf ("%d\n", (int) numofdigit);}    }

When n is large, time is long, TLE

Law II: Stirling formula

Log (n!) = log10 (sqrt (2*pi*n)) + N*LOG10 (n/e);

/* Use Stirling to find the number of positions */#include <stdio.h> #include <math.h> #define e 2.718281828459045#define pi 3.141592653589793239int Main () {    int t;    scanf ("%d", &t);    while (t--)    {        int n;        scanf ("%d", &n);        Double num_digit;        Num_digit=log10 (sqrt (2*pi*n)) + N*LOG10 (n/e);        printf ("%d\n", (int) num_digit+1);    }    return 0;}

WA two times reason:

Num_digit=log10 (sqrt (2*pi*n)) + n*log10 (n/e) +1;        printf ("%d\n", (int) num_digit);

When N=1,num_digit=0, because when N=1,

log10 (sqrt (2*pi*n)) + n*log10 (n/e) =-0.03

The last value is 0.

Summary: When using the Stirling formula to calculate the number of digits, taking into account the n=1, plus 1 placed after taking the whole

(int) (Log10 (sqrt (2*pi*n)) + N*LOG10 (n/e)) =0
Plus 1, after taking the whole place.

Int (3.1+1) =4
Int (3.1) +1=4
Int (3) +1=4
Int (3+1) =4
Int (-0.03) +1=1

poj1423---Ask for a large number of digits, I guess the algorithm that counts less than how many bits of input characters on the site

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.