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 ...
随机推荐
- 【区间DP】释放囚犯
貌似和石子合并差不多 可能是我见的题太少了,所以都差不多 OK 算法分析 首先不难看出这是一道区间DP,那么,按照本蒟蒻的意思 区间DP==三个循环 for(int len=2;len<=n;l ...
- java 实现yaml 数据转json与map
首先引入snakeyaml-1.16.jar的包. 直接上代码: package com.ming.yaml; import java.util.Map; import org.yaml.snakey ...
- .Net Core添加分布式Session
一.Session HTTP是一个无状态协议,Web服务器将每一个请求都视为独立请求.并且不保存之前请求中用户的值. Session 状态是ASP.NET Core提供的一个功能,它可以在用户通应用访 ...
- es优化收藏
Elasticsearch常用优化 https://www.cnblogs.com/zlslch/p/6478773.html Elasticsearch 基础理论 & 配置调优 http:/ ...
- objc_setAssociatedObject获取cell上button对应所在的行
#import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOu ...
- PostgreSQL的HA解决方案-1主从和备份(master/slave and backup)
一.部署说明 1.1 实施环境 本文档实验环境如下: PGSQL主机: 192.168.1.45 PGSQL备机: 192.168.1.50 软件和系统版本 Pgsql 版本: pgsql 9.2.4 ...
- react基础篇一
jsx简介 const element = <h1>Hello, world!</h1>; 这种看起来可能有些奇怪的标签语法既不是字符串也不是 HTML. 它被称为 JSX, ...
- POJ_3020_最小路径覆盖
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8721 Accepted: 4330 ...
- Jenkins构建项目
创建项目 Jenkins版本:Jenkins ver.2.150.1 在Jenkins首页点击‘New 任务’进入创建任务页面,在‘Enter an item name’输入框内输入项目名称,选择Je ...
- 团体程序设计天梯赛-练习集-L1-048. 矩阵A乘以B
L1-048. 矩阵A乘以B 给定两个矩阵A和B,要求你计算它们的乘积矩阵AB.需要注意的是,只有规模匹配的矩阵才可以相乘.即若A有Ra行.Ca列,B有Rb行.Cb列,则只有Ca与Rb相等时,两个矩阵 ...