UVa 10061 How many is zero ' s and how many digits?

Source: Internet
Author: User
Tags time limit

The factorial length and the number of tail 0 under arbitrary system

10061-how many zero ' s and how many digits?
Time limit:20.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_ problem&problem=1002

Given a decimal integer number you'll have to find out how many trailing zeros'll be there in it factorial in a Given Number system and also you'll have to find how many digits'll its factorial have in a given number system? You can assume this for a B based number system There are b different symbols to denote values ranging from 0 ... b-1.

Input

There would be several lines of input. Each line makes a block. Each line would 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! = (in decimal) but it are in hexadecimal number system. So in hexadecimal 5! has no trailing zeros

Output

For the, input output in a, how many trailing zeros would the factorial of this number have in the given Number system and also how many digits would the factorial of this number have in that given number system. Separate these two numbers with a. You can is sure that number of trailing zeros or the number of digits would not be greater than

Sample Input:

2 10
5 16
5 10

Sample Output:
0 1
0 2
1 3

Ideas

What about the number of the tail 0?

In the ordinary 10, calculate the number of n! after the 0, it is necessary to consider the n! of the element decomposition in the power of 5 (because 10=2*5 and 5 power-constant <=2 Power)

In other systems, it is considered that the power of the power of the maximum prime number MAXP in the decomposition of the base mass factor and the Maxp of the N-mass factorization are taken into account.

Complete code:

/*0.075s*/#include <cstdio> #include <cmath> const int MAXN = 1 << 20;  
    
Double DIGIT[MAXN]; When there are too many repetitive operations, you may wish to play table inline void Lenn () {for (int i = 1; i < MAXN; i++) digit[i] = Digit[i-1] + log  
(i);  
    int main () {Lenn ();  
    int N, N2, base, Base2, Maxp, RN, Rbase;  
        while (~SCANF ("%d%d", &n, &base)) {N2 = N, base2 = base;  
        MAXP = 1;  
            for (int i = 2; I <= base; ++i) {rbase = 0;  
                While (base% i = = 0) {Maxp = i;///maximum prime number the power of the largest prime number in the decomposition of ++rbase;///base mass factor  
            Base/= i;  
        } rn = 0;  
            while (n) {n/= maxp; The power of MAXP in the decomposition of Rn + n;///n mass factor}///What if I figure out a xxx.9999999999?  
        Precision fine-tuning is essential!  
    printf ("%d%d\n", rn/rbase, (int) (DIGIT[N2]/log (BASE2) + 1e-9) + 1);  }
    return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.