uva_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 bbased 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和b,求n!在b进制下有多少位数和尾部有多少个0。。。数论的东西没看过不会做。。。上网搜了下才懂,

   有多少位数就是求满足不等式 b^m <= n! < b^(m+1) 的m,位数就是m+1. 但是n!太大所以要用到cmath里的log函数

       求尾部多少个0,就是看 n!能表示成 k*b^x ( k为不能被b整除部分)的x值, 比如十进制下1200 = 12*10^2,10的指数为2,所以是2个0;但是由于n!太大不能直接求,所以用分解质因数的办法,比如10进制将10分解为2*5,也就是最后求n!里5的指数(因为2的指数一定比5的指数大)。

     求出b的各个质因数prime[i]和其对应的指数power[i], 然后求解n!里对应的prime[i]的个数cnt[i],最后答案应该为min{ cnt[i]/power[i]},

比如考虑24进制,分解24 = 2^3*3, 2的指数是3的指数的3倍,比如考虑9!,9!里面就有 2 4 6 8,总共包含8个2--2^8, 有3 6 9, 包含4个3--3^4.  8/3 = 2, 4/1 =4。这时候对应的最多凑出两个0--24^2。 


#include <iostream>
#include <cmath>
using namespace std;

int getZeros( int n, int b)
{
    int prime[b], power[b];
    int b1 = b, pCnt = 0;
    for ( int i = 2; i <= b; i++){
            if( b1 < i) break;
            if ( b1%i == 0){
                prime[pCnt] = i;
                power[pCnt] = 0;
                pCnt++;
            }

            while( b1%i == 0){
                power[pCnt-1]++;
                b1 /= i;
            }
    }



    int cnt[pCnt];
    fill( cnt, cnt+pCnt, 0);

    for( int i = 2; i <= n; i++){
        int t = i;
        for( int j = 0; j < pCnt; j++){
                if( t < prime[j]) break;
                while( t%prime[j] ==0){
                    cnt[j]++;
                    t /= prime[j];
                }
        }
    }

    int ans = 10000000;
    if( !pCnt) ans = 0;
    for( int i = 0; i<pCnt; i++)
    ans = min( cnt[i]/power[i], ans);
    return ans;
}


int getDigits( int n, int b)
{
    double digit = 0;
    for( int i = 2; i <=n; i++)
        digit +=log(i+0.0);

    return (int)( digit/(log(b)) + 1e-8+1);  //+1e-8--精度问题
}


int main()
{


    int n, b;
    while( cin >> n >> b){
        cout << getZeros( n, b) <<" " << getDigits( n, b) << endl;

    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值