Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is.
However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers.
Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n! (factorial of n).
Input
The only line of the input contains two integers n and b (1≤n≤1018, 2≤b≤1012).
Output
Print an only integer — the number of trailing zero digits in the b-ary representation of n!
Examples
input
6 9
output
1
input
38 11
output
3
input
5 2
output
3
input
5 10
output
1
Note
In the first example, 6!(10)=720(10)=880(9).
In the third and fourth example, 5!(10)=120(10)=1111000(2).
The representation of the number x in the b-ary base is d1,d2,…,dk if x=d1bk−1+d2bk−2+…+dkb0, where di are integers and 0≤di≤b−1. For example, the number 720 from the first example is represented as 880(9) since 720=8⋅92+8⋅9+0⋅1.
You can read more about bases here.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cctype>
using namespace std;
#define PI acos(-1.0)
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int INF32M=0x3f3f3f3f;
const ll INF64M=0x3f3f3f3f3f3f3f3f;
const ll maxn=1000000000000000000ll;
int main()
{
ios::sync_with_stdio(false);
ll n,b;
// ll ans=(ll)1e18; //ll ans=maxn;
ll ans=-1;
cin>>n>>b;
//b=7 3*3>7 b!=1 b为素数 除/b本身 1的因子个数一定比 b多 配凑b进制取决b个数
for(ll i=2;i*i<=b;i++)
{
ll cnt=0;
while(b%i==0)
{
b=b/i;
cnt++; //当前最小素因子个数
}
if(cnt)
{
ll sum=0,tmp=n; //sum 求n!的i因子个数 i因子cnt个 两个以上/cnt
while(tmp)
{
sum+=tmp/i; //数因子个数 边除边数 n!=25!=1*2...*5*..10*..15*..20*..25
tmp=tmp/i; //5 10 15 20 25 /5
//1 2 3 4 5(+5)
//0 0 0 0 1(+1) 6个
}
if(ans==-1)
ans=sum/cnt; //b=12=2*2*3 2因子cnt=2 i++后 3*3>b=3 b!=1
else
ans=min(ans,sum/cnt);
}
}
if(b!=1) //b=7素数 不可再分 只有1个
{
ll sum=0,tmp=n; //sum素因子个数
while(tmp)
{
sum+=tmp/b;
tmp/=b;
}
if(ans==-1)
ans=sum; //最大素因子个数 越大越少
else
ans=min(ans,sum); //b=10 n=25 ans=6 到这一步b=5 for1次
}
cout<<ans<<endl;
return 0;
}
最大素因子个数 为0的个数 在n!里配凑b
n!=25! b=10进制=2*5 n!中提取2的因子(22)肯定比提取5的因子(6)个数多
凑成10进制下10 即尾随0个数 因子分解b 素因子越大越少
最大素因子5的个数(因子2比5多)等价于 尾随0的个数