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

题意:求阶乘在不同进制下的位数。

用对数搞一搞就好啦,设阶乘n在k进制下位数为sum,sum=(int)logk(n!)+1=(int)(log10(n!)/log10(k))+1;然后以10为底打个表就好了。

注意n==0的时候特判一下。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define LL long long
using namespace std;
double sum[1000005];
void inin()
{
    sum[0]=0.0;
    for(int i=1; i<=1000000; i++)
        sum[i]=sum[i-1]+log10(i);
}
int main()
{
    inin();
    int T, t=1, n, k;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &k);
        if(n==0)printf("Case %d: 1\n", t++);
        else
        printf("Case %d: %d\n", t++, (int)(sum[n]/log10(k))+1);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zhulei2/p/8214088.html

时间: 2024-08-29 03:53:49

light oj 1045 - Digits of Factorial(求阶乘在不同进制下的位数)的相关文章

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 i

Light OJ 1028 Trailing Zeroes (I) 求n因子数

今天真机调试的时候莫名其妙遇到了这样的一个问题: This product type must be built using a provisioning profile, however no provisioning profile matching both the identity "iPhone Developer" and the bundle identifier..... 具体如下图所示: 十分蛋疼, 发现不管是从网上下的demo, 还是自己的过程.凡事真机测试的时候都

Light OJ 1054 Efficient Pseudo Code 求n^m的约数和

题目来源:Light OJ 1054 Efficient Pseudo Code 题意:求n的m次这个数的所有的约数和 思路:首先对于一个数n = p1^a1*p2^a2*p3^a3*-*pk^ak  约束和s = (p1^0+p1^1+p1^2+-p1^a1)(p2^0+p2^1+p2^2+-p2^a2)-(pk^0+pk^1+pk^2+-pk^ak) 然后就是先求素数表 分解因子 然后求p1^0+p1^1+p1^2+-p1^a1 这是一个以1开始的等比数列 就是(1-q^n)/(1-q) 最

SDUT 3503 有两个正整数,求N!的K进制的位数

有两个正整数,求N!的K进制的位数 题目链接:action=showproblem&problemid=3503">http://sdutacm.org/sdutoj/problem.php? action=showproblem&problemid=3503 #include <bits/stdc++.h> using namespace std; const double eps = 1e-6; const double PI = acos(-1.0); c

PAT 1015 Reversible Primes[求d进制下的逆][简单]

1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now

[算法]求满足要求的进制(辗转相除(欧几里得算法),求最大公约数gcd)

题目 3在十进制下满足若各位和能被3整除,则该数能被3整除. 5在十六进制下也满足此规律. 给定数字k,求多少进制(1e18进制范围内)下能满足此规律,找出一个即可,无则输出-1. 题解 写写画画能找到规律,即是求与k互质的数x,x进制下即能满足上述规律. 相关 求最大公约数:辗转相除法(又叫欧几里得算法) 欧几里德定理:gcd(a, b) = gcd(b , a mod b) ,对于正整数a.b. 其中a.b大小无所谓.当a值小于b值时,算法的下一次递归调用就能够将a和b的值交换过来. 代码

假设数n在b进制下为回文数,求最小的b

题目链接:here 假设n=b0+b1+b2....+bk 如果b2<=n,那么那么n在b进制下有多个数组成,可以直接暴力算 如果暴力没有正确的结果,即: 如果b2>n,那么n在b进制下只有两个数组成 要组成回文树,则xb+x=n  b=n/x-1 ,(n%x==0,x<b),要求b最小,则使x最大, 从sqrt(n+1)开始枚举x即可 #include<iostream> #include<cstdio> #include<cmath> using

【算法题5】求不同进制表示的位数之和的均值

尽管是一个CS专业的学生,小B的数学基础很好并对数值计算有着特别的兴趣,喜欢用计算机程序来解决数学问题,现在,她正在玩一个数值变换的游戏.她发现计算机中经常用不同的进制表示一个数,如十进制数123表达为16进制时只包含两位数7.11(B),用八进制表示为三位数1.7.3,按不同进制表达时,各个位数的和也不同,如上述例子中十六进制和八进制中各位数的和分别是18和11,. 小B感兴趣的是,一个数A如果按2到A-1进制表达时,各个位数之和的均值是多少?她希望你能帮她解决这个问题? 所有的计算均基于十进

light oj Beginners Problems

很多以前写的丑代码 1000 - Greetings from LightOJ #include<math.h> #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { int a,b,n,Case; scanf("%d",&n); Case=0; while(Case++,n--) { scanf("%d%d",&