light oj 1045 - Digits of Factorial K进制下N!的位数

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)

代码:

1045 - Digits of Factorial

   PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

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



PROBLEM SETTER: JANE ALAM JAN



PROBLEM SETTER: JANE ALAM JAN

时间: 2024-10-21 23:18:20

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

light oj 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

n!在k进制下的后缀0

问n! 转化成k进制后的位数和尾数的0的个数.[UVA 10061 How many zeros and how many digits?] Given a decimal integer number you will have to find out how many trailing zeros will be there in its factorial in a given number system and also you will have to find how many di

51nod 1116:K进制下的大数

51nod 1116:K进制下的大数 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1116 题目大意:给定一个大数,该数在$K$进制下是$K-1$的倍数,问最小的$K$($2 \leqslant K \leqslant 36$)是多少,若无解输出No Solution. 二项式定理 这题虽然可以暴力枚举,但还有更优雅的做法. 考虑一个$K$进制的大数$A$可以被表示为$\sum_{x=0}a_xK^x$,

51NOD 1116 K进制下的大数(字符串取模 + 枚举)

传送门 1116 K进制下的大数 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 有一个字符串S,记录了一个大数,但不知这个大数是多少进制的,只知道这个数在K进制下是K - 1的倍数.现在由你来求出这个最小的进制K. 例如:给出的数是A1A,有A则最少也是11进制,然后发现A1A在22进制下等于4872,4872 mod 21 = 0,并且22是最小的,因此输出k = 22(大数的表示中A对应10,Z对应35). Input 输入大数对应的字符串S

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

题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; const int MAXS = 1e5 + 10; const int MAXK = 36; char num[MAXS]; int main(int argc, const char * argv[]) { while (cin >> num) { int sum = 0; int len = (int)

快速沃尔什变换与k进制FWT

这是一篇用来卖萌的文章QAQ 考虑以下三类卷积 \(C_k = \sum \limits_{i \;or\;j = k} A_i * B_j\) \(C_k = \sum \limits_{i\;and\;j = k} A_i * B_j\) \(C_k = \sum \limits_{i\;xor\;j = k}A_i * B_j\) 由于前两种可以用FMT(高维前缀和)解决,那我们就谈谈第三种吧 下文中的\(n\)都是形如\(2^i - 1\)的数 下标的开与闭是根据好不好写来定的,但是还是

k进制正整数的对k-1取余与按位取余

华电北风吹 天津大学认知计算与应用重点实验室 日期:2015/8/24 先说一下结论 有k进制数abcd,有abcd%(k?1)=(a+b+c+d)%(k?1) 这是由于kn=((k?1)+1)n=∑ni=0Cin(k?1)i 因此kn 对(k-1)取余的话为1 比如10进制1425%9=3,(1+4+2+5)=12%9=3. 这个性质眼下我在两个地方见到了 (一)算法导论第11章讲散列表的时候,除法散列的时候 h(k)=kmod m 对于m的选取,若m取2p或者2p?1 均是不合适的选择,前者

2^k进制数

[题目描述] 设R是个2^k进制数,并满足以下条件: (1)R至少是个2位的2^k进制数: (2)作为2^k进制数,除最后一位外,R的每一位严格小于它右边相邻的那一位: (3)将R转换为2进制数q后,则q的总位数不超过w: 在这里,正整数k(1 ≤ k ≤ 9)和w(k < w ≤30000)是事先给定的. 询问满足上述条件的不同的r共有多少个. 我们再从另一角度作些解释: 设S是长度为w的01字符串(即字符串S由w个“0”或“1”组成),S对应于上述条件(3)中的q.将S从右起划分为若干个长度

[NOIP2006] 提高组 洛谷P1066 2^k进制数

题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2进制数q后,则q的总位数不超过w. 在这里,正整数k(1≤k≤9)和w(k<W< span>≤30000)是事先给定的. 问:满足上述条件的不同的r共有多少个? 我们再从另一角度作些解释:设S是长度为w 的01字符串(即字符串S由w个“0”或“1”组成),S对应于上述条件(3)中的q.将S从右起划分为