Question: Seek the number of suffixes 0 in the k system.
The old man pinched his finger, even pinched the std algorithm?
-
Solution
Reasonably, at first sight, this problem will not be done.
But the old man thought that if m=10, he would still do it.
If the number is less than or equal to n, how many multiples of 5 will be enough. 2 is absolutely more than 5.
So I had an idea.
First, prime factor decomposition is indispensable. m=ki=1scntii
Then, for each prime factor si, calculate how many multiples [1,n] can find (but when you encounter 23 in a similar calculation of 2, these are counted three times)
How to calculate? Let's face it? Should I be the only zz to think that way at the beginning?
Later, it was found that adding 1 to each calculation was enough, because the previous calculation had already been done.
After calculating, it is found that si finds xi in [1,n], but the occurrence of at least one m si requires cnti times. So the final answer is minki=1{xicnti}
#include <bits/stdc++.h>
#define LL long long
using namespace std ;
LL n, m, tot ;
LL s[1010], cnt[1010] ;
int main() {
#ifndef ONLINE_JUDGE
freopen ( "a.in", "r", stdin ) ;
freopen ( "a.out", "w", stdout ) ;
#endif
LL i, j, x, ans, gn ;
while ( scanf ( "%lld%lld", &n, &m ) != EOF ) {
memset ( s, 0, sizeof s ) ;
memset ( cnt, tot = 0, sizeof cnt ) ;
for ( x = m, i = 2 ; i*i <= x ; i ++ ) {
if (x%i == 0) s[++tot] = i ;
while (x%i == 0) cnt[tot] ++, x /= i ;
}
if (x != 1) s[++tot] = x, cnt[tot] = 1 ;
/*printf ( "%lld = ", m ) ;
for ( i = 1 ; i < tot ; i ++ ) printf ( "%lld^%lld + ", s[i], cnt[i] ) ;
printf ( "%lld^%lld\n", s[tot], cnt[tot] ) ;*/
ans = 1LL<<60 ;
for ( i = 1 ; i <= tot ; i ++ ) {
gn = 1 ;
x = 0 ;
for ( j = 1 ; gn <= n ; j ++ ) {
gn *= s[i] ;
x += n/gn ;
}
//printf ( "time %lld = %lld\n", s[i], x ) ;
ans = min(ans, x/cnt[i]) ;
}
printf ( "%lld\n", ans ) ;
}
return 0 ;
}
Unshamelessly advertise: My blog