HDU1018 POJ1423 UVALive2697 UVA1185 ZOJ1526 Big Number【阶乘位数】

708 篇文章 19 订阅
509 篇文章 9 订阅

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 28524 Accepted: 9075

Description

In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.

Input

Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 <= m <= 10^7 on each line.

Output

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

Sample Input

2
10
20

Sample Output

7
19

Source


Regionals 2002 >> Asia - Dhaka


问题链接HDU1018 POJ1423 UVALive2697 UVA1185 ZOJ1526 Big Number

问题简述:(略)

问题分析

  这是一个数论题,可以根据题意用暴力法来求解,也可以使用来Stirling公式求解。

  暴力法:N的阶乖的位数等于LOG10(N!)=LOG10(1)+.....LOG10(N)。

程序说明

  暴力法在POJ中出现TLE。

题记:(略)

 

AC的C++语言程序如下

/* HDU1018 POJ1423 UVALive2697 UVA1185 ZOJ1526 Big Number(根据Stirling公式计算) */

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

const double PI = acos(-1.0);
const double LN10 = log(10.0);

double stirling(int n)
{
    return ceil((n * log(double(n)) - n + 0.5 * log(2.0 * n * PI)) / LN10);
}

int main()
{
    int t, n;

    scanf("%d", &t);
    while(t--) {
        scanf("%d",&n);

        printf("%d\n", n <= 1 ? 1 : (int)stirling(n));
    }

    return 0;
}


AC的C语言程序如下

/* HDU1018 POJ1423 UVALive2697 UVA1185 ZOJ1526 Big Number(暴力法) */

#include <stdio.h>
#include <math.h>

int main(void)
{
    int t, n, i;
    double ans;

    scanf("%d", &t);
    while(t--) {
        scanf("%d",&n);

        ans = 0.0;
        for(i=1; i<=n; i++)
            ans += log10(i);

        printf("%d\n", (int)ans + 1);
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值