Problem:
Problem C: !

You are one of ICPC participants and in charge of developing a library for multiprecision numbers
and radix conversion. You have just finished writing the code, so next you have to test if it
works correctly. You decided to write a simple, well-known factorial function for this purpose:

Your task is to write a program that shows the number of trailing zeros when you compute M!
in base N, given N and M.

Input

The input contains multiple data sets. Each data set is described by one line in the format
below:

N M

where N is a decimal number between 8 and 36 inclusive, and M is given in the string repre-
sentation in base N. Exactly one white space character appears between them.

The string representation of M contains up to 12 characters in base N. In case N is greater
than 10, capital letters A, B, C, ... may appear in the string representation, and they represent
10, 11, 12, ..., respectively.

The input is terminated by a line containing two zeros. You should not process this line.

Output

For each data set, output a line containing a decimal integer, which is equal to the number of
trailing zeros in the string representation of M! in base N.

Sample Input

10 500
16 A
0 0

Output for the Sample Input

124
2

Solution in Ruby (judge verdict: Accepted)

while 1
  x = gets.split(' ')
  n = x[0].to_i
  m = x[1].to_s
  if(n==0)
    break
  end
  res = 0
  for i in 0..(m.length-1)
    res*=n
    if(m[i]=='0')
      res+=0
    end
    if(m[i]=='1')
      res+=1
    end
    if(m[i]=='2')
      res+=2
    end
    if(m[i]=='3')
      res+=3
    end
    if(m[i]=='4')
      res+=4
    end
    if(m[i]=='5')
      res+=5
    end
    if(m[i]=='6')
      res+=6
    end
    if(m[i]=='7')
      res+=7
    end
    if(m[i]=='8')
      res+=8
    end
    if(m[i]=='9')
      res+=9
    end
    if(m[i]=='A')
      res+=10
    end
    if(m[i]=='B')
      res+=11
    end
    if(m[i]=='C')
      res+=12
    end
    if(m[i]=='D')
      res+=13
    end
    if(m[i]=='E')
      res+=14
    end
    if(m[i]=='F')
      res+=15
    end
    if(m[i]=='G')
      res+=16
    end
    if(m[i]=='H')
      res+=17
    end
    if(m[i]=='I')
      res+=18
    end
    if(m[i]=='J')
      res+=19
    end
    if(m[i]=='K')
      res+=20
    end
    if(m[i]=='L')
      res+=21
    end
    if(m[i]=='M')
      res+=22
    end
    if(m[i]=='N')
      res+=23
    end
    if(m[i]=='O')
      res+=24
    end
    if(m[i]=='P')
      res+=25
    end
    if(m[i]=='Q')
      res+=26
    end
    if(m[i]=='R')
      res+=27
    end
    if(m[i]=='S')
      res+=28
    end
    if(m[i]=='T')
      res+=29
    end
    if(m[i]=='U')
      res+=30
    end
    if(m[i]=='V')
      res+=31
    end
    if(m[i]=='W')
      res+=32
    end
    if(m[i]=='X')
      res+=33
    end
    if(m[i]=='Y')
      res+=34
    end
    if(m[i]=='Z')
      res+=35
    end
  end
  ans = 2**100
  k = n
  for i in 2..n
    if k%i == 0
      tmp = 0
      r = 0
      while k%i==0
        k/=i
        r+=1
    end
      j=i
      while j<=res
        tmp+=res/j
        j*=i
      end
      ans = [ans,tmp/r].min()
    end
  end
  puts ans
end