How many zeros and how many digits?

Input: standard input

Output: standard output

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 digits will its factorial have in a given number system? You can assume that for a bbased number system there are b different symbols to denote values ranging from 0 ... b-1.

Input

There will be several lines of input. Each line makes a block. Each line will contain a decimal number N (a 20bit unsigned number) and a decimal number B (1<B<=800), which is the base of the number system you have to consider. As for example 5! = 120 (in decimal) but it is 78 in hexadecimal number system. So in Hexadecimal 5! has no trailing zeros

Output

For each line of input output in a single line how many trailing zeros will the factorial of that number have in the given number system and also how many digits will the factorial of that number have in that given number system. Separate these two numbers with a single space. You can be sure that the number of trailing zeros or the number of digits will not be greater than 2^31-1

Sample Input:

2 10
5 16
5 10

 

Sample Output:

0 1
0 2
1 3

题目大意:求n!的bas进制m的位数和后面0的个数。

解题思路:1,求位数:当base为10时,10^(m-1) < n < 10 ^m,两边同去log10,m - 1 < log10(n) < m,n 的位数为(m-1).

PS:<1>log10(a * b) = log10(a) + log10(b)        求n!的位数时。

<2>logb(a) = log c(a) / log c(b)转换进制位数。

<3>浮点数的精度问题,求位数需要用到log函数,log函数的计算精度有误差。所以 最后需要对和加一个1e-9再floor才能过。

2,将n!分解成质因子,储存在数组里面,在对bas做多次分解,直到数组中的元素小于0.

#include<stdio.h>
#include<string.h>
#include<math.h> #define N 10000
int num[N]; int count_digit(int n, int bas){
double sum = 0;
for (int i = 1; i <= n; i++)
sum += log10(i);
sum = sum / log10(bas);
return floor(sum + 1e-9) + 1;
} int count_zore(int n, int bas){
memset(num, 0, sizeof(num)); for (int i = 2; i <= n; i++){
int g = i;
for (int j = 2; j <= g && j <= bas; j++){
while (g % j == 0){
num[j]++;
g = g / j;
}
}
} int cnt = 0; while (1){
int g = bas; for (int j = 2; j <= bas; j++){
while (g % j == 0){
if (num[j] > 0)
num[j]--;
else
goto out;
g = g / j;
}
}
cnt++;
}
out:
return cnt;
} int main(){
int n, bas;
while (scanf("%d%d", &n, &bas) != EOF){
int ndigit = count_digit(n, bas);
int nzore = count_zore(n, bas);
printf("%d %d\n", nzore, ndigit);
}
return 0;
}

uva 10061 How many zero's and how many digits ?的更多相关文章

  1. UVA 10061 How many zero's and how many digits ? (m进制,阶乘位数,阶乘后缀0)

    题意: 给出两个数字a和b,求a的阶乘转换成b进制后,输出 (1)后缀中有多少个连续的0? (2)数a的b进制表示法中有多少位? 思路:逐个问题解决. 设a!=k.  k暂时不用直接转成b进制. (1 ...

  2. How many zero's and how many digits ? UVA - 10061

    Given a decimal integer number you will have to find out how many trailing zeros will be there in it ...

  3. Uva 10061 进制问题

    题目大意:让求n!在base进制下的位数以及末尾0的连续个数. 多少位 log_{10}256=log_{10}210^2+log_{10}510^1+log_{10}6*10^0 可以发现,只和最高 ...

  4. UVA - 10061 How many zero&#39;s and how many digits ?

    n!=x*b^y, 当x为正整数时,最大的y就是n!末尾0的个数了, 把n,b分别拆成素因子相乘的形式: 比如, n=5,b=16 n=5,b=2^4, 非常明显,末尾0的个数为0 10进制时,n!= ...

  5. uva 10061(数学)

    题解:题目要在b进制下输出的是一个数字阶乘后有多少个零,然后输出一共同拥有多少位.首先计算位数,log(n)/log(b) + 1就是n在b进制下有多少位,而log有个公式就是log(M×N) = l ...

  6. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

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

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

  8. 2003-Can't connect to mysql server on localhost (10061)

    mysql数据库出现2003-Can't connect to mysql server on localhost (10061)问题 解决办法:查看wampserver服务器是否启动,如果没有启动启 ...

  9. VNC connect:Connection refused(10061)

    在Windows机器上使用VNC Viewer访问Linux服务器,有时候会遇到"connect:Connection refused(10061)"这个错误,导致这个错误出现的原 ...

随机推荐

  1. DeDe调用指定栏目ID下的文章

    *注: row: 调用条数 titlelen: 字数 typeid: 调用的栏目ID orderby: 按照升序对记录进行排序 idlist: 提取特定文档为空 infolen='40' 内容简介长度 ...

  2. 转载自php 大牛的学习计划 人生规划

    2012年偶决定开始写博客了,不为别的,就希望可以通过博客记录我的成长历程同时也希望可以帮助一些刚毕业,刚入行业的兄弟姐们们.我们是一群充满浮躁.抱怨.迷茫的程序猿,想一想3年就这么过去了,社会变得更 ...

  3. C语言初学 使用while语句统计输入字符个数

    #include<stdio.h> main() { int n=0; printf("输入任意个数的字符:\n"); while(getchar()!='\n')n+ ...

  4. 第一个微信小程序(实现点击一个按钮弹出toast)

    今天根据网上的教程搭建了微信小程序的环境,然后看文档做了一个简单的小应用. 项目的目录是这个样子的: app.js.app.json.app.wxss是全局文件,必不可少的文件.定义在app.wxss ...

  5. NGINX小技巧--将所有目录和目录下所有文件分别给与不同的权限

    为了安全,有时要将文件的权限进行限制,但,目录如果没有755,则不能进入. 所以需要分别给权限 find ./ -type f -name "*" |xargs ls -l

  6. Cracking the coding interview--Q1.1

    原文: Implement an algorithm to determine if a string has all unique characters. What if you can not u ...

  7. MCS-51单片机实用子程序库

    目前已有若干版本的子程序库公开发表,它们各有特色.本程序库中的开平方算法为快速逼近算法,它能达到牛顿迭代法同样的精度,而速度加快二十倍左右,超过双字节定点除法的速度. 本子程序库对<单片机应用程 ...

  8. 自制单片机之十五……可串行驱动LCD12864的应用

    在网上搜了一下,ST7920控制器的LCD产品可以提供8位,4位并行和串行接口可选,并行的控制接口的LCD较多,前面的贴子也介绍过,我们在这儿不说了,这儿我们讲的是串口控制LCD12864. 买了块S ...

  9. Delphi - GetUserNameEx(学一下导出Windows API,以及Array Char充当缓冲区的用法,下标必须从零开始)

    (* * Author : http://www.michael-puff.de * Date : 2006-03-29 * License : PUBLIC DOMAIN *) function G ...

  10. struct2(六) 为表单添加验证

    简介 为表单添加验证 添加校验的方法: 1. first name 不能为null 2. Email address 不能为null 3. age 必须大于18岁 为了在用户提交的时候,能够校验这个表 ...