P - Big Number

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 ≤ 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 
给你一个数n,要你求他的阶乘有几位数字组成;一看到这题就条件反射的觉得要用大数才能解决,结果折腾了老久,竟然不对。然后去看大神的代码,要不是最近天气热,没吃啥东西,差点一口老血喷将出来;尼玛我敲了好长的代码,他给我就简简单单的几行,就用了一个函数log10()来自变量的位数,而这题给的是阶乘,比如说log10(3!)=log(1*2*3)=log(1)+log(2)+log(3);一个函数,一个for循环,丫的就解决的干干精简。这就是和大神的差距啊。不过也算赚到了。利用log10()的特点求位数 。   
 
 
 
 
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int a[100][100000];
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n;cin>>n;double sum=0;//在这里sum由于要进行相加求和,只能是double,而在求单独一个
        for(int i=1;i<=n;i++)//数的位数时可以用int来盛装
        {
            sum+=log10(i);
        }
        cout<<(int)sum+1<<endl;
    }
}
除此之外,这道题还可以用斯特林公式来求
 
 
//此题必须用公式才能AC,数据规模太大了
//Stirling公式:    n! = ((2*pi*n)^(1/2))*((n/e)^n); 前提是n > 3
//由此可以导出lg(n!) = (lg(2*pi)+lg(n))/2 + n*(lg(n)-lg(e)); 
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
	int test,n;
	long long int s;
	const long double c1 = 0.798179868358; //lg(2*pi)
	const long double c2 = 0.434294481903; //lg(e)
	long double c3;
	cin >> test;
	while(--test+1)
	{
		cin >> n;
		c3 = log10((double)n);
		s = 1;
		if(n > 3)
		{
			s = (c3 + c1)/2 + n * (c3 - c2) + 1;
			cout << s << endl;
		}
		else
			cout << 1 << endl; 
	}
	return 0;
}

据说这个公式可以解决和阶乘相关的一切问题,我表示太强了		



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值