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 - 10057 A mid-summer night&#39;s dream.

    偶数时,中位数之间的数都是能够的(包含中位数) 奇数时,一定是中位数 推导请找初中老师 #include<iostream> #include<cstdio> #include ...

  2. UVA 12436 - Rip Van Winkle&#39;s Code(线段树)

    UVA 12436 - Rip Van Winkle's Code option=com_onlinejudge&Itemid=8&page=show_problem&cate ...

  3. 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 ...

  4. UVA 1484 - Alice and Bob&#39;s Trip(树形DP)

    题目链接:1484 - Alice and Bob's Trip 题意:BOB和ALICE这对狗男女在一颗树上走,BOB先走,BOB要尽量使得总路径权和大,ALICE要小,可是有个条件,就是路径权值总 ...

  5. 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 ...

  6. Uva 12436 Rip Van Winkle&#39;s Code

    Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...

  7. 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 ...

  8. Uva 10061 进制问题

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

  9. uva 10061(数学)

    题解:题目要在b进制下输出的是一个数字阶乘后有多少个零,然后输出一共同拥有多少位.首先计算位数,log(n)/log(b) + 1就是n在b进制下有多少位,而log有个公式就是log(M×N) = l ...

随机推荐

  1. Cookie、Token与Session介绍(非原创)

    文章大纲 一.Cookie介绍二.Token介绍三.Session介绍四.Token.Cookie与Session比较五.参考文章   一.Cookie介绍 1. Cookie是什么 cookie机制 ...

  2. BZOJ 4332 FFT+快速幂

    思路: 最裸的方程:f[i][j]=Σf[i-1][j-k]*F[k] 诶呦 这不是卷积嘛 f[i]就可以用f[i-1]卷F 求到 但是这样还是很慢 设p[i] 为Σ f[j](1<=j< ...

  3. Codeforces 609D 被二分教做人

    传送门:http://codeforces.com/problemset/problem/609/D (如需转载,请注明出处,谢谢O(∩_∩)O) 题意: Nura想买k个小玩意,她手上有 s 个bu ...

  4. 2013 ACM/ICPC Asia Regional Changsha Online - J

    原题戳这里. 题意: 有一未知列数a1,a2,a3.....an, 已知s[i]=a[i-1]+a[i]+a[i]  (1<i<n) s[1]=a[1]+a[2]; s[n]=a[n-1] ...

  5. indeed 5.13 第二次网测

    题目描述,我找不见了,大概写一下想法和代码吧. 1. 没有看 2. 由于数据范围很小,就是简单的枚举,求全排列,然后更新答案. #include<bits/stdc++.h> #defin ...

  6. 关于基础的Set 和Get

    先附上一篇文章,讲的很清楚 在Core中,我们要是先这样设置了.在我们对这个上下文做查询工作的时候,例如: var head = _OMSECDatabase.OmsEcorderHead.Where ...

  7. ★Java面向对象(一)——————————基本概念

    package boll; /* 用Java语言对现实生活中的事物进行描述. 通过类的形式来体现, 怎么描述呢? 对于事物的描述通常只有两个方面,一个是属性,一个是行为. 只要明确该事物的行为和属性并 ...

  8. PHP执行Mysql数据库的备份和还原

    使用mysqldump命令备份 mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中. mysqldump命令的工作原理很简单.它先查出需要备份的表的 ...

  9. 【sqli-labs】 less43 POST -Error based -String -Stacked with tiwst(POST型基于错误的堆叠变形字符型注入)

    和less42一样 login_user=&login_password=1');insert into users(id,username,password) value(15,'root' ...

  10. layer自定义弹窗样式

    1.下载并引用js, 官网http://layer.layui.com/ 文档http://www.layui.com/doc/modules/layer.html <link href=&qu ...