I have already used the prime factorization, but it is failing one test case. Please help with the same.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int comp(int n,int x){
int k = x;
int ans = 0;
while(n/k > 0){
ans = ans + (n/k);
k = k*x;
}
return ans;
}
int prime(int n, int k){
int ans = INT_MAX;
for(int i=2;i*i<=k;i++){
if(k%i == 0){
int cnt = 0;
while(k%i == 0){
k = k/i;
cnt++;
}
int x = comp(n,i);
x = x/cnt;
ans = min(ans,x);
}
}
if(k!=1){
ans = min(ans,comp(n,k));
}
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
cout<<prime(n,k)<<endl;
}
return 0;
}