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. POJ 1111(数字很吉利嘛) 简单BFS

    Image Perimeters Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8594 Accepted: 5145 Desc ...

  2. HTML5 postMessage 和 localStorage 实现窗口间通信

    LocalStorage(不能跨域) 基本思想:通过localStorage的标准事件storage来实现跨页面通信,即页面A通过写入特定数据触发页面B的storage事件,页面B响应之后再写入数据通 ...

  3. W3C标准冒泡、捕获机制

    (一) 捕获和冒泡如何相互影响 我们来做几个任务吧! 有一个前提,#parent为标签,#child为子标签,他们是嵌套关系支线任务1 //捕获模式 document.getElementById(' ...

  4. Eclipse安装配置——For Java

    1.下载安装JRE 2.下载Eclipse,解压到相应文件夹 3.配置Eclipse 3.1 配置字体大小  -12号 3.2配置workspace默认编码,utf-8,默认系统windows 3.3 ...

  5. 用Python+selenium打开IE浏览器和Chrome浏览器的问题

    这几天在学Python+selenium自动化,对三大浏览器Firefox,Chrome和IE都做了尝试,也都分别下载了对应的webdriver,如:geckodriver.chromedriver. ...

  6. AI:IPPR的模式生成-学习/训练方式(基本结构)

    前言:        一个完备的模式识别系统,必然包含一个模式识别模型,此外还要附加一个评价模型,以及为了构建识别模型而构建的学习模型,并选择在学习模型中使用的学习方法. 否则  w=w 这样,)那样 ...

  7. 《计算机图形学基础(OpenGL版)》使用院校(更新)

    从清华大学出版社责任编辑处获悉,很多高等院校选用了我们这本教材,读者反应不错! 另外,编辑提供了一份详细的使用院校名单如下: 河南科技学院 中原工学院 河北工程大学 防空兵学院 伊犁师院电信学院 吉林 ...

  8. python tips:迭代器与可迭代对象

    for循环 for i in s: print(i) 在上述for循环中,不断地将s中的值赋值给i,然后打印出来.这种只针对s中元素的循环称为对s的迭代,能够迭代的s称为可迭代的. python为了实 ...

  9. BZOJ 1740: [Usaco2005 mar]Yogurt factory 奶酪工厂 贪心 + 问题转化

    Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the ...

  10. Python笔记(29)----进程

    目录: 一.进程 多任务的概念 创建子进程----fork[Linux] 全局变量在多个进程中不共享 多次fork() 创建进程----multiprocessing[windows] 二.线程 一. ...