UVA - 10061 How many zero'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!=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's and how many digits ?的更多相关文章
- UVA - 10057 A mid-summer night's dream.
偶数时,中位数之间的数都是能够的(包含中位数) 奇数时,一定是中位数 推导请找初中老师 #include<iostream> #include<cstdio> #include ...
- UVA 12436 - Rip Van Winkle's Code(线段树)
UVA 12436 - Rip Van Winkle's Code option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- 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 ...
- UVA 1484 - Alice and Bob's Trip(树形DP)
题目链接:1484 - Alice and Bob's Trip 题意:BOB和ALICE这对狗男女在一颗树上走,BOB先走,BOB要尽量使得总路径权和大,ALICE要小,可是有个条件,就是路径权值总 ...
- uva 10061 How many zero's and how many digits ?
How many zeros and how many digits? Input: standard input Output: standard output Given a decimal in ...
- Uva 12436 Rip Van Winkle's Code
Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...
- 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 ...
- Uva 10061 进制问题
题目大意:让求n!在base进制下的位数以及末尾0的连续个数. 多少位 log_{10}256=log_{10}210^2+log_{10}510^1+log_{10}6*10^0 可以发现,只和最高 ...
- uva 10061(数学)
题解:题目要在b进制下输出的是一个数字阶乘后有多少个零,然后输出一共同拥有多少位.首先计算位数,log(n)/log(b) + 1就是n在b进制下有多少位,而log有个公式就是log(M×N) = l ...
随机推荐
- this引用逃逸问题
//this引用逃逸 // 1.构造器还未完成前,将自身this引用向外抛,使其他线程访问这个引用,进而访问到其未初始化的变量,造成问题 // 2.内部类访问外部类未初始化的成员变量 //3.多态继承 ...
- SQLServer2008 在where条件中使用CASE WHEN
create table #temp( id int identity(1,1), name varchar(20), startYear int, startMonth in ...
- C# 多线程系列(三)
线程池 创建线程需要时间,如果有不同的小任务要完成,就可以事先创建许多线程,在应完成这些任务时发出请求.这个线程数最好在需要更多线程时增加,在需要释放资源时减少. 不需要自己创建这样的一个列表.该列表 ...
- 【转载】【翻译】JavaScript Scoping and Hoisting--JS作用域和变量提升的探讨
原文链接:http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting 你知道下面的JavaScript代码执行后会aler ...
- 用 Django2.0 做 简单的BBS(前端用 Bootstrap)
实现目标: 开发首页显示BBS的标题和摘要,点击BBS的标题可跳转到BBS详细页面进行展示. 开发环境及开发工具: Python 3.6.3 Django 2.0 Pycharm 2017.3 实现过 ...
- 【MFC】在MFC中PreTranslateMessage()的使用方法
BOOL CSearchuserDlg::PreTranslateMessage(MSG* pMsg) { if (pMsg->message==WM_KEYDOWN) // 判断是否有按键按下 ...
- android开源新闻小程序、3D翻转公告效果、小说检索、Kotlin开发TODO清单等源码
Android精选源码 开源新闻小程序源码分享 android动态壁纸.锁屏动画.来电秀等源码 android笔记App效果源码 Android实现3D版翻页公告效果 android小说搜索阅读源码 ...
- c++ 中一个类或者一个对象所占的字节数
转载博客:转载地址https://www.cnblogs.com/JingHuanXiao/p/6080726.html 一个空的class在内存中多少字节?如果加入一个成员函数后是多大?这个成员函数 ...
- 【PL/SQL】匿名块、存储过程、函数、触发器
名词解释 子程序:PL/SQL的过程和函数统称为子程序. 匿名块:以DECLARE或BEGIN开始,每次提交都被编译.匿名块因为没有名称,所以不能在数据库中存储并且不能直接从其他PL/SQL块中调用. ...
- JavaOO小结二,及MySQL小结
流按照传输内容分有几种?各自的父类是什么? 流按照传输内容有 字节流.字符流.对象流.但其本质都是字节流.字符流和对象流是在字节流基础上作了一层封装,以便更好对字符和对象进行操作. 字节流的父类:In ...