uva 10061: How many zero's and how many digits?
来源:互联网 发布:php授权验证怎么做 编辑:程序博客网 时间:2024/05/22 03:07
对于这么大的数字,要想知道他有几位数,末尾有多少个零,很明显不能按常规的方法来做。
一、求末尾有几个零的代码:
int ZeroNum() { memset(factor_count,0,sizeof(factor_count)); for(int i=2; i<=N; i++) { int tmp=i; for(int j=2; j<=tmp && j<=B; j++) { while(tmp%j==0) { factor_count[j]++; tmp /= j; } } } int nZero=0; while(1) { int tmp=B; for(int i=2; i<=tmp; i++) { while(tmp%i==0 && factor_count[i]>0) { factor_count[i]--; tmp /= i; } } if(tmp==1) nZero++; else break; } return nZero; }
思路就是:先找出前面所乘数字中所有小于B的因子factor以及其个数factor[i];
然后,计数这所有的因子可以 将B整除到1 的次数。
————这就是末尾零的个数。
二、求总共有多少位的代码:
int DigitNum() { double sum=0; for(int i=2; i<=N; i++) sum += log10(double(i)); sum /= log10(double(B)); return floor(sum+1e-9) + 1; }
思路是这样的:
B进制下m位数所能表示的最大值在10进制下,为B^m-1;
若设总位数为m,则有公式:B^(m-1)<=N!<B^m;
我们会发现N!因为太大是表示不出来的。但是,公式是需要变形的!
就有(m-1)*log10(B)<=log10(N!)<=m*log10(B);
还有一个重要的关系式:log10(1*2*3*...*N)=log10(1)+log10(2)+log10(3)+..+log10(N);!!!
就有了求位数m的办法。
0 0
- uva 10061 - How many zero's and how many digits ?
- uva 10061 - How many zero's and how many digits ?
- UVa 10061 - How many zero's and how many digits ?
- UVA 10061 How many zero's and how many digits ?
- uva 10061 - How many zero's and how many digits ?
- UVa 10061: How many zero's and how many digits?
- uva 10061 - How many zero's and how many digits
- uva 10061 How many zero's and how many digits ?
- uva 10061: How many zero's and how many digits?
- UVa 10061 - How many zero's and how many digits ?
- UVa 10061 - How many zero's and how many digits ?
- UVA 10061 How many zero's and how many digits ?
- UVA - 10061 How many zero's and how many digits ?
- UVA - 10061 How many zero's and how many digits ?
- UVa 10061 How many zero's and how many digits
- 10061 - How many zero's and how many digits ?
- 10061 - How many zero's and how many digits ?
- 10061 - How many zero's and how many digits ?
- websphere6.1开发、部署、远程调用EJB2.0
- VMware中Linux无法全屏的问题
- 为程序添加版本自动更新功能(转+详细分析)
- Number of 1s
- 构建自定义组件
- uva 10061: How many zero's and how many digits?
- LINUX中oracle10g安装
- Android 核心分析 之六 -----IPC框架分析 Binder,Service,Service manager
- C#基于Linq和反射实现数据持久化框架Xml4DB
- java sax 解析 实例
- xcode4.2 MainWindow.xib
- cocos2d-x工具集
- PAT (Advanced) 1069. The Black Hole of Numbers (20)
- 产品开发这几年(5)编码规范