HDU 1018 大数(求N!的位数/相加)

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35382    Accepted Submission(s): 16888

Problem 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 ≤ n ≤ 107 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

Asia 2002, Dhaka (Bengal)

题意:

求N!的位数。

代码:

 1 /*
 2 1:log10(12345)=log10(1.2345*10^4)=4+log(1.2345);n的位数就是log10(n)+1;可以暴力。
 3 2:斯特林公式:一个数的阶乘近似等于sqrt(2*PI*n)*(n/e)^n;
 4 */
 5 #include<iostream>
 6 #include<cmath>
 7 #include<cstdio>
 8 using namespace std;
 9 int main()
10 {
11     int n,m;
12     scanf("%d",&n);
13     while(n--)
14     {
15         scanf("%d",&m);
16         double ans=0;
17         for(int i=2;i<=m;i++)
18         ans+=log10(i);
19         printf("%d\n",(int)ans+1);
20     }
21     return 0;
22 }
23
24 #include<iostream>
25 #include<cmath>
26 #include<cstdio>
27 using namespace std;
28 const double e=2.718281828459;
29 const double PI=3.14159265;
30 int main()
31 {
32     int n,m;
33     scanf("%d",&n);
34     while(n--)
35     {
36         double ans;
37         scanf("%d",&m);
38         if(m!=1)
39         ans=0.5*log10(2*PI*m)+m*log10(m)-m*log10(e)+1;
40         else ans=1.0;
41         printf("%d\n",(int)ans);
42     }
43     return 0;
44 }
//两个大数相加用字符串处理。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int main()
{
    int t;
    char s1[1005],s2[1005];
    scanf("%d",&t);
    for(int k=1;k<=t;k++)
    {
        scanf("%s%s",s1,s2);
        int ks1=strlen(s1);
        int ks2=strlen(s2);
        ks1--;ks2--;
        int sav=0,h=0,a1,a2;
        char s[1005];
        while(1)
        {
            if(ks1<0&&ks2<0)
            break;
            if(ks1>=0&&ks2>=0)
            {
                a1=s1[ks1]-‘0‘;
                a2=s2[ks2]-‘0‘;
            }
            if(ks1>=0&&ks2<0)
            {
                a1=s1[ks1]-‘0‘;
                a2=0;
            }
            if(ks1<0&&ks2>=0)
            {
                a1=0;
                a2=s2[ks2]-‘0‘;
            }
            ks1--;ks2--;
            int tem=a1+a2+sav;
            sav=tem/10;
            tem%=10;
            s[h++]=tem+‘0‘;
        }
        if(sav!=0)
        s[h++]=sav+‘0‘;
        printf("Case %d:\n",k);
        printf("%s + %s = ",s1,s2);
        for(int i=h-1;i>=0;i--)
        cout<<s[i];
        printf("\n");
        if(k!=t)
        printf("\n");
    }
    return 0;
}
时间: 2024-10-07 04:01:28

HDU 1018 大数(求N!的位数/相加)的相关文章

HDU 1018 Big Number (阶乘位数)

题意:给一个数n,返回该数的阶乘结果是一个多少位(十进制位)的整数. 思路:用log来实现. 举个例子 一个三位数n 满足102 <= n < 103: 那么它的位数w 满足 w = lg103 = 3. 因此只要求lgn 向下取整 +1就是位数.然后因为阶乘比如5阶乘的话是5 * 4 * 3 * 2 * 1.位数就满足lg 5 * 4 * 3 * 2 * 1 = lg5 + lg4 + lg3 + lg2 + lg1.用加法就不会超过数字上限. 当然这是十进制下得.如果是m进制下 ,就把lg

HDU 1018 Big Number (简单数学)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25649    Accepted Submission(s): 11635 Problem Description In many applications very large integers numbers are required. Some of these

HDU 4927 大数运算

模板很重要 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define MAXN 9999 #define MAXSIZE 10 #define DLEN 4 class BigInt { private: int a[500]; //可以控制大数的位数 i

HDU 1018 Big Number (数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x) + 1; 所以n!的位数 = (int)log10(1*2*3*.......n) + 1; = (int)(log10(1) + log10(2) + log10(3) + ........ log10(n)) + 1; 1 #include<cstdio> 2 #include<cs

HDU 1018 Big Number 数学题解

Problem 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

大数求余

1 /**2016中国大学生程序设计网络赛赛HDU 2 1001大数求余 3 */ 4 5 #include "iostream" 6 #include "cstdio" 7 #include "cstring" 8 using namespace std; 9 char s[100000002]; 10 int main() { 11 12 int t=1; 13 while(~scanf("%s", &s)) {

HDU 4417 类似求逆序数的树状数组

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2250    Accepted Submission(s): 1092 Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability

求不相邻金币相加和的最大值--动态规划1

求不相邻金币相加和的最大值. 输入n个金币的金币面值(正数自定义),求这些金币不相邻和的最大值. 动态规划问题1 设f(n)为第n个金币数的最大值,f(0)=0,f(1)=a[1],输入的数组从下标为1开始. f(n)=max{a[n]+f(n-2),f(n-1)}. 代码如下: import java.util.Scanner; public class Jin_bi_zui_da_zhi { public static void main(String[] args) { Scanner s

HDU 4911 Inversion 求逆序数对

点击打开链接 Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1106    Accepted Submission(s): 474 Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent num