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. 微信js sdk上传多张图片

    微信js sdk上传多张图片,微信上传多张图片 该案例已tp3.2商城为例 直接上代码: php代码: public function ind(){ $appid="111111111111 ...

  2. ReverseEngineerCodeFirst 自定义模板

    1.在你要生成的项目里面在根目录下面添加CodeTemplates文件夹,并在该文件夹下面创建子文件夹ReverseEngineerCodeFirst 2.在ReverseEngineerCodeFi ...

  3. php中curl的详细解说 【转载】

    这几天在帮一些同学处理问题的时候,突然发现这些同学是使用file_get_contents()函数来采集页面内容的,貌似都没有curl的概念亦或是对这种工具特别不敏感, 本文我来给大家详细介绍下cUR ...

  4. python--1、入门

    python的创始人为吉多·范罗苏姆(Guido van Rossum). python在2017年统计的所有语言排名中处于第四名,稳步上升状态. python应用领域: WEB开发(Django框架 ...

  5. 使用T-sql建库建表建约束

    为什么要使用sql语句建库建表? 现在假设这样一个场景,公司的项目经过测试没问题后需要在客户的实际环境中进行演示,那就需要对数据进行移植,现在问题来了:客户的数据库版本和公司开发阶段使用的数据库不兼容 ...

  6. python 上手

    1.安装模块 cmd---“pip install [模块名]” 2.爬虫常用模块 requests beautifulsoup4 3.检查已安装的模块 cmd ---"pip list&q ...

  7. nvcc fatal : Unsupported gpu architecture 'compute_11'

    使用VS编译OpenCV编译源代码时候,对Cmake生成的工程文件编译,会出现 nvcc fatal : Unsupported gpu architecture 'compute_11'  问题.原 ...

  8. button提交表单 a标签提交表单

    <form name="searchForm" id="searchForm" method="get" action="/ ...

  9. Type inference

    Type inference refers to the automatic detection of the data type of an expression in a programming ...

  10. taglib遍历foreach循环list集合

    第一部导入jstl.jar 第二步进行list传输: package com.aaa.servlet; import com.aaa.dao.IUserDAO; import com.aaa.dao. ...