uva10061 - How many zero's and how many digits ?
2013-11-18 19:50
274 查看
题目大意: 给出一个数num和一个进制base 求出num在该base下的末尾0进制和总位数
第一部分求末尾0进制:
先求出进制的素因数分解 例如 10=2*5
然后找出找出num的素因数分解 中有多少个 2或者5 由此找出多少个0
第二部分求出位数,就是求出 num = base^x 的最大X
利用两边去log计算
因为精读问题WA了N次。。最后也是看网上的代码解决的orz
Problem G
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 b based 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
代码:
第一部分求末尾0进制:
先求出进制的素因数分解 例如 10=2*5
然后找出找出num的素因数分解 中有多少个 2或者5 由此找出多少个0
第二部分求出位数,就是求出 num = base^x 的最大X
利用两边去log计算
因为精读问题WA了N次。。最后也是看网上的代码解决的orz
Problem G
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 b based 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
代码:
#include <iostream> #include <vector> #include <algorithm> #include <limits.h> #include <memory.h> #include <malloc.h> #include <math.h> #include <cstdio> using namespace std; const int maxn = 900; int f[maxn]; int fn[maxn]; int p=0; void div(long long num) { int many=0; memset(f,0,sizeof(f)); memset(fn,0,sizeof(fn)); p=-1; for(long long i=2;i<=num;i++) { while(num%i==0) { if(f[p]==i) { fn[p]++; } else { p++; f[p]=i; fn[p]++; } num/=i; } } } int main() { long long num,base; int numn[maxn]; while(cin>>num>>base) { div(base); memset(numn,0,sizeof(numn)); int pn=0; for(int i=1;i<=num;i++) { int temp=i; for(int j=0;j<=p;j++) { while(temp%f[j]==0) { numn[j]++; temp/=f[j]; } } } long long MIN=LONG_LONG_MAX; for(int i=0;i<=p;i++) { numn[i]/=fn[i]; if(numn[i]<MIN) MIN=numn[i]; } double digits=0; for(int i=1;i<=num;i++) { digits =digits+ log10(i); } digits/=log10(base); if (digits - floor(digits) < 1e-10) digits ++; else digits = floor(digits+ 1); printf("%lld %.0f\n",MIN,digits); } return 0; }
相关文章推荐
- 玩转Eclipse — 添加 JAR 文件
- 封装微软TTS实现web系统文本转语音
- Hadoop的MapFile, SetFile, ArrayFile 及 BloomMapFile文件格式<转>
- dbgrideh使用
- shell脚本
- 顺序表的应用举例
- eclipse ubuntu上快捷图标
- (Relax 贪心1.4)POJ 2325 Persistent Numbers(使用贪心策略解决这么一个问题: 给定一个数n,求一个最小的数m,使得m的各位的乘积==n)
- java内存模型(java 并发编程基础)
- centos 下安装jdk
- 异常积累:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in
- find用法例子
- virtualenv django
- HTTP web错误
- 命名空间的了解
- Please ensure that adb is correctly located at
- OGRE学习(5)
- 多线程的分离思考
- 写个批处理文件自动配置IP地址
- 顺序表与链表的比较