cf 1114c(数论)
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.
题意:即使求n!在b进制末尾0的个数;
思路:十进制下1500=15*10^2个数2,二进制下100=1*2^2的个数为2;
所以n!在b进制下的个数位:
若n!=a*b^k答案就是k,把b质因数分解为(p1^x1)*(p2^x2).............
故n!=a*[(p1^x1)*(p2^x2).......]^k=a*p1(x1*k)*p2*(x2*k);
由于a的影响,改写为:
n!=(a1*p1^X1)*(a2*p2^X2),所以k=min(X1/x1,X2/x2..............);
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
const int MAXN = 2 * 1000 * 1000 + 10;
ll n, b,cnt=0,num[MAXN],prime[MAXN];
struct node{ int w, id; }a[MAXN];
bool cmp(node &a, node &b) { return a.w > b.w; }
void get_prime(ll x) { //b质因数分解
for (ll i = 2; i*i <= x; i++) {
while (x%i == 0) {
prime[cnt] = i;
num[cnt]++;
x /= i;
}
if (num[cnt])cnt++;
}
if (x > 1)prime[cnt] = x, num[cnt++]=1;
}
ll work(ll p) { //n!中含有p的个数
ll x = n,sum=0;
while (x) {
sum += x / p;
x /= p;
}
return sum;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> b;
ll ans = INF;
get_prime(b);
for (ll i = 0; i < cnt; i++) {
ans = min(ans,work(prime[i])/num[i]);
}
cout << ans << endl;
return 0;
}