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. kicad绿油开窗_GitHub - OS-Q/S05: KiCad EDA
  3. 关于IPv4设置一些常见问题的解答
  4. 【EOJ Monthly 2018.10 - A】oxx 的小姐姐们(模拟,水题,填充矩阵,输出格式有坑)
  5. linux /dev/null,Shell中 /dev/null和 /dev/null 21
  6. docker 容器 exited_docker常用命令整理
  7. 线程通信之handle用法
  8. MATLAB当中reshape函数以及转置的使用
  9. iOS简单人脸检测的实现
  10. 掌握旋风:SAP成功实施实地指南
  11. 仿时钟表盘自定义view
  12. TX-LCN分布式事务之TCC模式
  13. 微信小程序泛滥的今天,你知道如何测试吗?
  14. org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException
  15. 成长的力量--销售技巧
  16. 程序员为什么不单干、接私活、自己开公司
  17. Java实现网络聊天程序的设计与实现(基于UDP协议)
  18. js判断一个数独的解是否合法
  19. 2022-2028年中国医用级空气消毒机行业市场深度分析及前瞻研究报告
  20. 视频重建论文EDVR: Video Restoration with Enhanced Deformable Convolutional Networks阅读笔记

热门文章

  1. float浮动,定位
  2. 前后端交互中,返回前端的自定义响应数据结构
  3. UE4 集成讯飞听写插件
  4. 如何把两个查询语句合成一条 语句
  5. 高仿带感魔性病毒源码+成品(最近很火的)
  6. 计算机网络第2章(物理层)
  7. Java字符串简化_关于java查询语句 如何简化的问题
  8. c语言关于链表选择题看不懂,有关链表基本操作三题
  9. Linu查看系统内核版本命令 uname -r
  10. x299服务器芯片组,18核心炸裂!X299主板全集:为它真拼了