• Loading...

Problem 3

points - 15

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,
5! = 1 x 2 x 3 x 4 x 5 = 120
BUt this is in decimal number system. 10! can be written as 3628800 in decimal number system while it is equal to "15657400" in octal number system and "375F00" in hexadecimal number system.
Let NOZ(N,B) be a function which calculates the number of trailing zeroes that are in N factorial in a Base B number system. Therefore, NOZ(10,8) = NOZ(10,10) = NOZ(10,16) = 2.
Find .

Answer

23054122

Solution

We can always represent B as a product of primes as
B = a^p1 * b^p2 * c^p3 * ...
Then the number of trailing zeroes in n factorial in Base B is given by the formulae
min{1/p1(n/a + n/(a*a) + ....), 1/p2(n/b + n/(b*b) + ..), ....}.

Here is the C++ code:


#include<iostream>
using namespace std;
#include<math.h>
int main()
{
    int N,B,i,j,p,c,noz,k,sum=0;
    for (N=2;N<=1000;N++)
        for (B=2;B<=1000;B++)
        {
            noz=N;
            j=B;
            for (i=2;i<=B;i++)
            {
                if (j%i==0)
                {   
                    p=0;
                    while (j%i==0)
                    {
                          p++;
                          j/=i;
                    }
                    c=0;
                    k=N;
                    while (k/i>0)
                    {
                          c+=k/i;
                          k/=i;
                    }
                    noz=min(noz,c/p);
                }
          }
          sum+=noz;
    }
    cout<<sum;
}


Submit your solution

You need to be logged in to submit a solution.

discussions


There is no discussion yet. Feel free to ask an question you might have regarding this problem


post a question

Loading...

Top Scorers
KoenR 195
DrKorbin 185
Silence 150
A_A_Lunyov 145
cedricl 145
kozima 145
cgy4ever 135
ProjectYoung 130
pankdm 130
dzetkulic 130
Submissions
Problem1
15 pt
You are not logged in
79
submissions

23
correct (29 %)


Problem2
20 pt
You are not logged in
35
submissions

18
correct (51 %)


Problem3
25 pt
You are not logged in
29
submissions

6
correct (20 %)


Problem4
15 pt
You are not logged in
80
submissions

54
correct (67 %)


Problem5
25 pt
You are not logged in
35
submissions

13
correct (37 %)


Problem6
25 pt
You are not logged in
39
submissions

17
correct (43 %)


Problem7
25 pt
You are not logged in
26
submissions

13
correct (50 %)


Problem8
10 pt
You are not logged in
60
submissions

39
correct (65 %)


Problem9
20 pt
You are not logged in
32
submissions

25
correct (78 %)


Problem10
15 pt
You are not logged in
57
submissions

34
correct (59 %)