1045 - Digits of Factorial

Factorial of an integer is defined by the following function

f(0) = 1

f(n) = f(n - 1) * n, if(n > 0)

So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 is 170.

In this problem, you have to find the number of digit(s) of the factorial of an integer in a certain base.

Input

Input starts with an integer T (≤ 50000), denoting the number of test cases.

Each case begins with two integers n (0 ≤ n ≤ 106) and base (2 ≤ base ≤ 1000). Both of these integers will be given in decimal.

Output

For each case of input you have to print the case number and the digit(s) of factorial n in the given base.

Sample Input

Output for Sample Input

5

5 10

8 10

22 3

1000000 2

0 100

Case 1: 3

Case 2: 5

Case 3: 45

Case 4: 18488885

Case 5: 1

分析:换底公式log a b = log c b / log c a; 所以logk(fn) = log10(fn)/ log10k;    logq0(fn) = log10(N) = log10(1 * 2 *...*n) = log10(1) + 1og10(2) .....+ 1og10(n)

代码:

#include<iostream>

#include<cstdio>

#include<cmath>

#include<cstring>

#include<algorithm>

#define N 1000009

using namespace std;

double a[N];

void init()

{

a[0] = log10(1);

for(int i = 1; i <= N; i++)

a[i] = a[i-1] + log10(i*1.0);

}

int main(void)

{

int T, cas;

int n, k;

init();

scanf("%d", &T);

cas = 0;

while(T--)

{

cas++;

scanf("%d%d", &n, &k);

if(n == 0)

printf("Case %d: 1\n", cas);

else

{

double ans = ceil(a[n]/log10(k*1.0));

printf("Case %d: %d\n", cas, (int)ans);

}

}

light oj 1045 - Digits of Factorial K进制下N!的位数的更多相关文章

  1. n!在k进制下的后缀0

    问n! 转化成k进制后的位数和尾数的0的个数.[UVA 10061 How many zeros and how many digits?] Given a decimal integer numbe ...

  2. 陕西师范大学第七届程序设计竞赛网络同步赛 F WWX的礼物【数学/k进制下x^n的位数/log】

    链接:https://www.nowcoder.com/acm/contest/121/F来源:牛客网 题目描述 WWX的女朋友送给了他一个礼物,可是礼物却被一把K进制密码锁锁住了.在礼物盒上还有一张 ...

  3. bzoj 3000 Big Number 估算n!在k进制下的位数 斯特林公式

    题目大意 求n!在k进制下的位数 2≤N≤2^31, 2≤K≤200 分析 作为数学没学好的傻嗨,我们先回顾一下log函数 \(\log_a(b)=\frac 1 {log_b(a)}\) \(\lo ...

  4. 51 Nod 1116 K进制下的大数

    1116 K进制下的大数  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 有一个字符串S,记录了一个大数,但不知这个大数是多少进制的,只知道这个数 ...

  5. 求x!在k进制下后缀零的个数(洛谷月赛T1)

    求x!在k进制下后缀和的个数 20分:     求十进制下的x!后缀和的个数 40分: 高精求阶乘,直接模拟过程 (我不管反正我不打,本蒟蒻最讨厌高精了) 60分     利用一个定理(网上有求x!在 ...

  6. [51nod1116]K进制下的大数

    解题关键:$A\% (k - 1) = (A[0] + A[1]*k + A[2]*{k^2} + ...A[n]*{k^n})\% (k - 1) = (A[0] + A[1] + ...A[n]) ...

  7. 数位DP 求K进制下0~N的每个数每位上出现的数的总和

    好久没写博客了,因为感觉时间比较紧,另一方面没有心思,做的题目比较浅也是另一方面. 热身赛第二场被血虐了好不好,于是决定看看数位DP吧. 进入正题: 如题是一道经(简)典(单)的数位dp. 第一步,对 ...

  8. 51nod 1116 K进制下的大数

    你万万想不到,Long Long 就能存下的数据 #include <iostream> #include <cstdio> #include <cstdlib> ...

  9. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

随机推荐

  1. 【python系统学习05】input函数——实现人机交互

    input函数 目录 input用途 语法 示例 参数 返回值 返回值数据类型转换 容错处理 动手吧 input用途 实现人机交互: 1.你提出问题打印在屏幕上, 2.运行程序的人再输入回答. 3.按 ...

  2. 初入python,与同学者的第一次见面(小激动)

    自2017来,接触python其实已经算是蛮久了,最苦的时光还是刚开始的时候,真的,我感觉编程就是一种感觉,有的时候就像找对象一样,感觉对了,怎么学都是带劲哈哈哈.在这个周围都在学习PHP的环境下,我 ...

  3. importlib 根据字符串导入模块

    应用: Django中间件,rest framework 组件的全局配置文件 import importlib path = "abc.def.foo" module_path,c ...

  4. idea命令行、撤销commit

    原文地址:https://blog.csdn.net/chzphoenix/article/details/38090349 近期在使用git,最开始在idea界面操作,后来要求用命令行.刚开始还不是 ...

  5. 以其他控制器为目标 在视图中生成输出URL

    结果呢:<a href="/Admin">This targets another controller</a>

  6. 异想家IDEA的偏好配置

    最好将配置文件位置改为软件安装目录下,因为只有自己用,易于便携. 修改bin目录下的idea.properties,注释#去掉修改idea.config.path.idea.system.path配置 ...

  7. redis--->微博小项目

    redis 微博小项目 centos6.9+lnmp+redis 写的微博小项目,梳理了redis在项目中kes的设计,redis各种数据结构在不同业务场景下的应用等知识点. 这里用的php框架是自己 ...

  8. Kafka网络模型和通信流程剖析

    1.概述 最近有同学在学习Kafka的网络通信这块内容时遇到一些疑问,关于网络模型和通信流程的相关内容,这里笔者将通过这篇博客为大家来剖析一下这部分内容. 2.内容 Kafka系统作为一个Messag ...

  9. 1759: 学生信息插入(武汉科技大学结构体oj)(已AC)

    #include<stdio.h>struct student { long no; char name[9]; int score;} t;void input(struct stude ...

  10. DOCKER 学习笔记7 Docker Machine 建立虚拟机实战,以及错误总结

    前言 通过以上6小节的学习,已经可以使用DOCKER 熟练的部署应用程序了.大家都可以发现使用 DOCKER 带来的方便之处,因为现在的话,只是在一台服务器上部署,这样部署,我们只需要一条命令,需要的 ...