Skip to content

Instantly share code, notes, and snippets.

@yjfvictor
Last active August 29, 2015 14:07
Show Gist options
  • Save yjfvictor/e12655ac3df4b4071def to your computer and use it in GitHub Desktop.
Save yjfvictor/e12655ac3df4b4071def to your computer and use it in GitHub Desktop.
1273: 大数的位数
/**
* @author 叶剑飞
* @file 1273.c
* @brief 1273: 大数的位数
* @note http://125.221.232.253/JudgeOnline/problem.php?id=1273
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/**
* @brief 圆周率
*/
#define PI 3.1415926535897932384626433832795028841971
/**
* @brief 自然对数的底数
*/
#define e 2.718281828459045235360287471352
/**
* @brief 得到指定数字阶乘的位数
* @param[in] n 被阶乘数
* @return 数字的阶乘的位数
*
* @section 描述
*
* Stirling公式: @f[ n! \approx \sqrt{2 \pi n } ( \frac{n}{e} )^n @f]
*
* 阶乘的位数:
* @f[ 1 + \log_{10} [\sqrt{2 \pi n } ( \frac{n}{e} )^n] @f]
*
* 化简,得
* @f[ 1 + \log_{10} \sqrt{2 \pi n } + n \log_{10} ( \frac{n}{e} ) @f]
*/
long long GetDigitCount(int n)
{
long long digits = 1 + (int) (log10(sqrt(2 * PI * n)) + n * log10(n / e));
return digits;
}
int main()
{
int test_cases;
int n;
scanf("%d", &test_cases);
while (test_cases--)
{
scanf("%d", &n);
printf("%lld\n", GetDigitCount(n));
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment