#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll findzero(ll n, ll p)
{
ll c = 0, r = p;
while(r <= n)
{
c += (n/r);
r = r * p;
}
return c;
}
vector<pair<int, int>> factorizeofB(ll b)
{
vector<pair<int, int>> ans;
ll i;
for(i=2;b != 1;i++)
{
if(b%i == 0)
{
ll c = 0;
while(b%i == 0)
{
b = b/i;
c++;
}
ans.push_back(make_pair(i, c));
}
}
return ans;
}
ll findzero_Any_base(ll n, ll b)
{
vector<pair<int, int>> vec;
vec = factorizeofB(b);
ll ans = INT_MAX, i;
for(i=0;i<vec.size();i++)
ans = min(ans, findzero(n, vec[i].first)/vec[i].second);
return ans;
}
int main()
{
ll n, m;
// Here n is main Number && m is Base :
while(cin >> n >> m)
{
ll r = findzero_Any_base(n, m);
cout << r << endl;
}
return 0;
}
#define ll long long int
using namespace std;
ll findzero(ll n, ll p)
{
ll c = 0, r = p;
while(r <= n)
{
c += (n/r);
r = r * p;
}
return c;
}
vector<pair<int, int>> factorizeofB(ll b)
{
vector<pair<int, int>> ans;
ll i;
for(i=2;b != 1;i++)
{
if(b%i == 0)
{
ll c = 0;
while(b%i == 0)
{
b = b/i;
c++;
}
ans.push_back(make_pair(i, c));
}
}
return ans;
}
ll findzero_Any_base(ll n, ll b)
{
vector<pair<int, int>> vec;
vec = factorizeofB(b);
ll ans = INT_MAX, i;
for(i=0;i<vec.size();i++)
ans = min(ans, findzero(n, vec[i].first)/vec[i].second);
return ans;
}
int main()
{
ll n, m;
// Here n is main Number && m is Base :
while(cin >> n >> m)
{
ll r = findzero_Any_base(n, m);
cout << r << endl;
}
return 0;
}
Comments
Post a Comment