HTML & JS
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll n, b ;
/*
 upsolving : 
       not understood what is happening completely need to understand the code thoroughly
       
       also address the anamoly in the commented code below::

*/

int main() {
    
    cin>>n>>b;
   ll ans= 1000000000000000000 ;

   for(ll i=2 ; i <= b ; i++ )
   {
       if( i*i > b)
        i = b ; 
       
       ll bcnt = 0LL ;
       while(b % i == 0LL )  { b /= i ; bcnt++ ; }
       if(bcnt==0) continue ;
       
      // ll ncnt = 0LL, mul=1 ;
       //while( mul <= n/i ) {  mul *= i ;ncnt += (n/mul) ; }
       
       /*
       (1) why this does not work??
       */
      ll ncnt = 0LL, mul=i;
      while( mul <= n ) {  
          ncnt += (n/mul) ;
          mul *= i ;
         if( mul < 0 ) break ;
         }
             
      ans = min(ans, ncnt / bcnt);
   }
    
  cout << ans ;

   return 0;
}

Generated URL:

Output:

10416666666666661
Report Bug