HDU 1018.Big Number "July 25"

Source: Internet
Author: User
Tags integer numbers

Big number

This problem, there is no difficulty or anything, is to rise posture. Problem DescriptionIn Many applications very large integers numbers are required. Some of these applications is using keys for secure transmission of data, encryption, etc. In this problem you is given a number, you has to determine the number of digits in the factorial of the number.

Inputinput consists of several lines of integer numbers. The first line contains a integer n, which is the number of cases to being tested, followed by n lines, one integer 1≤n≤ 107 on all line.

Outputthe output contains the number of digits in the factorial of the integers appearing in the input.

Sample Input
21020

Sample Output
719

This is a more interesting topic. Give a number and then find the number of bits of the factorial of the number. The first idea must be to find the factorial of the number, and then count the number. And then the egg! Must not be found. Then find the following information:N The order of the number of bits equal to LOG10 (n! ) =LOG10 (1) + ..... LOG10 (N). OK, on the code:

#include <cstdio> #include <cmath>int main () {    int n;    scanf ("%d", &n);    for (int i=0;i<n;i++) {        int x;        Double sum=0;        scanf ("%d", &x);        for (int j=1;j<=x;j++)            sum+=log10 (j);        printf ("%d\n", int (sum) +1);    }    return 0;}
At the same time find a little information:Stirling formula: n! and √ (2πn) * n^n * e^ (-N) values very close
So log10 (n!) = log (n!)/log (Ten) = (N*log (n)-n + 0.5*log (2*π*n))/log (n);
Here's someone else's code:

#include <stdio.h> #include <math.h>const double PI = ACOs ( -1.0), const double LN_10 = log (10.0);d ouble Reback ( int N) {    return ceil ((N*log (double (N))-n+0.5*log (2.0*N*PI))/ln_10);}    int main () {int cas,n;scanf ("%d", &cas), while (cas--) {scanf ("%d", &n), if (n<=1) printf ("1\n"), else printf ("% .0lf\n ", Reback (n)); return 0;}

really up the pose!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1018.Big Number "July 25"

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.