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 C (judge verdict: Wrong Answer)

#include<stdio.h>
#include<string.h>
unsigned long long m,a;
int main(){
  int i,j,n,min,d[50]={0};
  char s[50],c[]={"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
  while(scanf("%d %s",&n,s),n){
    j=n;
    m=0;
    for(i=0;i<40;i++)d[i]=0;
    min=1000000000;
    for(i=2;i<=j;i++){
      while(j%i==0){
	j/=i;
	d[i]++;
      }
    }//printf("a\n");
    //for(i=0;i<n;i++)printf("%d %d\n",i,d[i]);
    for(i=0;s[i];i++){
      m*=n;
      for(j=0;c[j]-s[i];j++);
      m+=j;
    }//printf("%d\n",m);
    for(i=0;i<=n;i++){
      if(d[i]==0)continue;
      a=0;
      for(j=i;j<=m;j*=i)a+=m/j;
      a/=d[i];//printf("%d %lld\n",i,a);
      if(min>a)min=a;
    }
  
  printf("%lld\n",a);
  }
  return 0;
}