n!=x*b^y,

当x为正整数时,最大的y就是n!末尾0的个数了,

把n,b分别拆成素因子相乘的形式:

比如,

n=5,b=16

n=5,b=2^4,

非常明显,末尾0的个数为0

10进制时,n!=a*10^x

b进制时,n!=c*b^y

非常明显,n!的位数就是最大的x+1

这里计算我用了log,精度设置为1e-9

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<cmath>
using namespace std;
const int inf=(1<<31)-1;
const double eps=1e-9;
vector<int>prime;
void maketable()
{int i,j,n=800;bool iscp[810];memset(iscp,0,sizeof(iscp));for(i=2;i<=n;i++){if(!iscp[i]){prime.push_back(i);for(j=i+i;j<=n;j+=i)iscp[j]=1;}}
}
map<int,int>fn;
map<int,int>fb;
map<int,int>::iterator it;
void debug()
{cout<<"***************"<<endl;for(it=fn.begin();it!=fn.end();it++)cout<<it->first<<"^"<<it->second<<endl;cout<<"***************"<<endl;for(it=fb.begin();it!=fb.end();it++)cout<<it->first<<"^"<<it->second<<endl;cout<<"***************"<<endl;
}
int main()
{//freopen("in","r",stdin);//freopen("out","w",stdout);maketable();int i,j,k,n,b,dg,m,num_zero;double x;while(cin>>n>>b){fn.clear();fb.clear();x=0;for(i=2;i<=n;i++)x+=log10(double(i));dg=int(x/log10(double(b))+eps)+1;m=prime.size();for(i=2;i<=n;i++){k=i;for(j=0;j<m&&k>=prime[j];j++){while(k%prime[j]==0&&k>=prime[j]){fn[prime[j]]++;k/=prime[j];}}}for(i=0;i<m&&b>=prime[i];i++){while(b%prime[i]==0&&b>=prime[i]){fb[prime[i]]++;b/=prime[i];}}//debug();num_zero=inf;for(it=fb.begin();it!=fb.end();it++)num_zero=min(num_zero,fn[it->first]/it->second);cout<<num_zero<<" "<<dg<<endl;}return 0;
}

Problem G

How many zeros and how many digits?

Input: standard input

Output: standard output

Given a decimal integer number you willhave to find out how many trailing zeros will be there in its factorial in a given number system and alsoyou will have to find how many digits will its factorial have in a given number system? You can assume that fora 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 linewill 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 ina single line how many trailing zeros will the factorial of that numberhave in the given number system and also how many digits will the factorial of thatnumber have in that given number system. Separate these two numbers with a single space. You can be surethat the number of trailing zeros or the number of digits will not be greaterthan 2^31-1

Sample Input:

2 10
5 16
5 10

 

Sample Output:

0 1
0 2
1 3
________________________________________________________________________________________
Shahriar Manzoor
16-12-2000

UVA - 10061 How many zero#39;s and how many digits ?相关推荐

  1. UVa 10061 How many zero's and how many digits?

    方法: factorial mod, logarithm 求trailing zeros,其实就是factorial mod 的应用, 求长度,利用log 函数.需要注意的是,答案为int(log(n ...

  2. uva 10061——How many zero\'s and how many digits ?

    题意:这道题开始是卡了很久的,题意是给定一个数n然后让你求B进制下n!有多少个零,和有多少位数,咋一看,是高精度,如果数论不是很熟系. 思路:开始是直接抛弃高精度的一来是存不下,二来没办法短时间计算出 ...

  3. Uva 10061 进制问题

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

  4. UVa 10112 - Myacm Triangles

    UVa第一卷最后一题. 求内部不含点并且面积最大的三角形. 暴力. 代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #inclu ...

  5. UVa 10180 - Rope Crisis in Ropeland!

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=41&pa ...

  6. Uva 10074【递推dp】

    UVa 10074 题意:求01矩阵的最大子0矩阵. http://www.csie.ntnu.edu.tw/~u91029/MaximumSubarray.html#2 这里说的很清楚.先求Larg ...

  7. UVA 10494 - If We Were a Child Again(高精度除法和取余)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. Uva 11997 多路归并

    题目链接:https://uva.onlinejudge.org/external/119/11997.pdf 题意: k*k的矩阵,从每一行中选一个元素加起来,可以得到 kk个和,求前 k 个最小值 ...

  9. Uva - 12050 Palindrome Numbers【数论】

    题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...

最新文章

  1. 等我搞研究发财了,我就......
  2. 无意中发现了一位清华大佬的代码模版
  3. 模块化数据中心有什么优势?
  4. Windows 动态链接库DLL浅解
  5. 60v5g无刷电机矢量控制器_完成一个最小FOC矢量控制系统所需的基本模块和功能配置...
  6. MHA+keepalived实现Mysql高可用及读写分离
  7. 比特斯拉酒瓶更贵!小鹏汽车推出“金属麻将”:1999元
  8. Windows 编程[6] - 学习窗体生成的过程六: 最终的代码!
  9. 使用Docker部署mongo后 使用Robo 3T、Studio 3T( MongoChef )在 create databse 创建数据库时的掉坑笔记
  10. 智能化施工(综合管线)
  11. 【工具】——远程协助(向日葵)
  12. wifi信道12为啥没人用_为什么我的无线路由器没有WIFI信道设置
  13. Android Design 与 Holo Theme
  14. 吴裕雄--天生自然 高等数学学习:斯托克斯公式、环流量与旋度
  15. Unity画面像素化shader
  16. MicroC实现Expr部分和for循环,无续算版本Comp.fs展示
  17. NAS 详细搭建方案 -添加磁盘
  18. 驾驭你的“职场布朗运动” .
  19. 什么样的公司可以申请高新技术企业?
  20. AT指令查看IMEI/IMEISV的值

热门文章

  1. 修改hosts 流畅使用coursera
  2. Unity3D Shader入门指南(二)
  3. Android启动initlogo.rle制作
  4. C# 中数据缓存总结
  5. 使用c#操作IBM WebSphere MQ
  6. 移远EC20 4G模块LTE开发板三网通模块 MQTT阿里云物联网
  7. 段错误产生原因及简单的调试方法
  8. ARM(IMX6U)裸机C语言蜂鸣器驱动实验(BSP+SDK)
  9. java面向对象语言_Java到底是不是一种纯面向对象语言?
  10. 代码 拉取_Git 利用 Webhooks 实现代码的自动拉取