您的位置:首页 > 其它

lightOJ 1045 Digits of Factorial (数位计数)

2017-01-15 16:40 411 查看

题目分析

首先我们知道log(a∗b)=log(a)+log(b),那么很明显如果是在k进制下n的阶乘,那么位数为lognk!+1=log1k+log2k+log3k+......+lognk+1,那么结果已经很明显了。我们可以先处理处10进制下的n个阶乘所得的数,然后如果是其他阶乘直接乘以log(10)/log(b)即可。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6+100;

double ans[maxn];   //计算10进制所得的位数

void init(){
ans[0] = 0;
for(int i = 1; i < maxn; i++)
ans[i] = log10(i) + ans[i-1];
}

int main(){
init();
int T;
scanf("%d", &T);
for(int kase = 1; kase <= T; kase++){
int n, b;
scanf("%d%d", &n, &b);
double temp = log(10)/log(b)*ans
;
printf("Case %d: %d\n", kase, (int)temp+1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: