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

转载于:https://www.cnblogs.com/wzjhoutai/p/7098387.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值