i am getting crct for given test cases but while submitting wrong answer
I am getting crct for given test cases but while submitting wrong answer
Pls share your code by saving on ide…so that I could check it…
#include
using namespace std;
int main(){
int t;
cin>>t;
while(t–){
long long int n,m;
cin>>n>>m;
long long int x=m;
long long int sum=0;
while(n>=x){
sum=sum+(n/x);
x=x*m;
}
cout<<sum<<endl;
}
}
The approach you are trying to use is not correct…Try to use the approach as :
int ans = inf;
int mp = 1;
for(int i=2; i*i<=k; i++) //prime factors less than sqrt(k)
{
if(k % i == 0) // a prime factor is i.
{
mp = 0; // number of occurences of i in k.
while(k % i == 0)
{
mp++;
k /= i;
}
int tmp = 0;
ll p = i;
while(p <= n)
{
tmp += n / p;
p*=i;
}
ans = min(ans, tmp / mp);
}
}
if(k > 1) // prime factor greater than sqrt(k).
{
int tmp = 0;
ll p = k;
while(p <= n)
{
tmp += n / p;
p *= k;
}
ans = min(ans, tmp);
}
if(ans == inf)
ans = 0;
cout << ans << endl;
}