[分解质因数/阶乘素因子的幂] C. Trailing Loves (or L'oeufs?) CF1114C
C. Trailing Loves (or L'oeufs?)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis.
Aki is fond of numbers, especially those with trailing zeros. For example, the number 92009200 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 nn and bb (in decimal notation), your task is to calculate the number of trailing zero digits in the bb-ary (in the base/radix of bb) representation of n!n! (factorial of nn).
Input
The only line of the input contains two integers nn and bb (1≤n≤10181≤n≤1018, 2≤b≤10122≤b≤1012).
Output
Print an only integer — the number of trailing zero digits in the bb-ary representation of n!n!
Examples
input
Copy
6 9
output
Copy
1
input
Copy
38 11
output
Copy
3
input
Copy
5 2
output
Copy
3
input
Copy
5 10
output
Copy
1
Note
In the first example, 6!(10)=720(10)=880(9)6!(10)=720(10)=880(9).
In the third and fourth example, 5!(10)=120(10)=1111000(2)5!(10)=120(10)=1111000(2).
The representation of the number xx in the bb-ary base is d1,d2,…,dkd1,d2,…,dk if x=d1bk−1+d2bk−2+…+dkb0x=d1bk−1+d2bk−2+…+dkb0, where didi are integers and 0≤di≤b−10≤di≤b−1. For example, the number 720720 from the first example is represented as 880(9)880(9) since 720=8⋅92+8⋅9+0⋅1720=8⋅92+8⋅9+0⋅1.
You can read more about bases here.
#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int mn = 1e6;
const ll inf = 1e18;
int cnt;
bool prime[mn + 10];
ll pm[mn + 10];
void Prime() /// 素数筛
{
for (int i = 1; i <= mn; i++)
prime[i] = 1;
for (int i = 2; i <= mn; i++)
{
if (prime[i] == 0)
continue;
pm[++cnt] = i;
for (int j = 2; i * j <= mn; j++)
prime[i * j] = 0;
}
}
ll ling;
map<ll, ll> mp;
void find_prime(ll x) /// 分解质因数
{
for (int i = 1; i <= cnt && pm[i] * pm[i] <= x; i++)
{
while (x % pm[i] == 0)
{
x /= pm[i];
mp[pm[i]]++; // 每个质因数数量
}
}
if (x > 1) // 大于根号x的因数只会有一次幂
ling = x;
}
int main()
{
ll n, b;
cin >> n >> b;
Prime();
find_prime(b);
ll ans = inf;
for (int i = 1; i <= cnt; i++)
{
if (mp[pm[i]] == 0)
continue;
ll t = 0;
for (ll j = pm[i]; j <= n; j *= pm[i])
{
t += n / j;
if ((db)j > n / pm[i])
break;
}
ans = min(ans, t / mp[pm[i]]);
}
if (ling != 0)
{
ll t = 0;
for (ll j = ling; j <= n; j *= ling)
{
t += n / j;
if ((db)j > n / ling)
break;
}
ans = min(ans, t);
}
cout << ans << endl;
return 0;
}