10061 - How many zero's and how many digits ?

Problem G

How many zeros and how many digits?

Input: standard input

Output: standard output


Given a decimal integer number you will have to find out how many trailing zeros will be there in its factorial in a given number system and also you will have to find how many digits will its factorial have in a given number system? You can assume that for a 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 line will 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 in a single line how many trailing zeros will the factorial of that number have in the given number system and also how many digits will the factorial of that number have in that given number system. Separate these two numbers with a single space. You can be sure that the number of trailing zeros or the number of digits will not be greater than 2^31-1


Sample Input:

2 10
5 16
5 10

 

Sample Output:

0 1
0 2

1 3


给你两个数,N,base。其中要把N!表示成 base进制,之后再求该进制下的数为几位数,末尾有几个0.

比如 5 10 这是5!为120,表示为十进制就是3位数,末尾1个零

而5 16 。5!为120,表示成16进制就是78,有2位数,末尾没有0.


这道题我一开始只想到了用高精度来计算,但是又要表示成不同的进制,写起来必然很麻烦。看了别人的结题报告,学到的几个方法。这里先简单介绍一下吧

1. 求一个数 n 的阶乘在 b 进制下有多少位,必有 b^(m-1)<=n!<=b^m。把b换成十就很好理解。所以 m = log b(n!)=log b(1)+log b(2) +…… + log b(n)+1 要注意一下精度问题。

2. 求n!的末尾有几个零。我们知道10=2*5 而出现一个5,必然会有2.所以就转换成为求5的个数。

大家具体可以看这个: http://baobaolimei.blog.163.com/blog/static/4528707420103204858969/

3. 求n!按照b进制转换后,末尾有几个零。同样的想法,把b分解成质因数相乘,比如16=2*2*2*2,然后记录n!当中b的质因数的个数,最后再反过来处理。

具体见代码吧。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,base,ans,digit;
int prime[1000];
void get_digit()
{
    int i;
    double fs=0.0;
    for (i=2; i<=n; i++)
        fs+=log10(i);
    fs/=log10(base);
    digit=floor(fs+1e-9)+1;//之前没有加1e-9wa了。
}
int get_ans()
{
    ans=0;
    int i,j;
    for (i=2; i<=n; i++)
    {
        int temp=i;
        for (j=2; j<=base && j<=temp; j++)
        {
            while(temp%j==0)
            {
                prime[j]++;
                temp/=j;
            }
        }
    }
    while(1)
    {
        int temp=base;
        for (i=2; i<=base; i++)
        {
            while(temp%i==0)
            {
                if (prime[i]>0) prime[i]--;
                else return ans;
                temp/=i;
            }
        }
        ans++;
    }
    return ans;
}
int main ()
{
    while(cin>>n>>base)
    {
        memset(prime,0,sizeof(prime));
        get_digit();
        ans=get_ans();
        cout<<ans<<" "<<digit<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值