CF BUDDY
← Problems·

1114C · Trailing Loves (or L'oeufs?)

1700 · brute force, implementation, math

Problem: 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 Format: The only line of the input contains two integers nn and bb (1n10181 \le n \le 10^{18}, 2b10122 \le b \le 10^{12}).

Output Format: Print an only integer — the number of trailing zero digits in the bb-ary representation of n!n!

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,,dkd_1, d_2, \ldots, d_k if x=d1bk1+d2bk2++dkb0x = d_1 b^{k - 1} + d_2 b^{k - 2} + \ldots + d_k b^0, where did_i are integers and 0dib10 \le d_i \le b - 1. For example, the number 720720 from the first example is represented as 880(9)880_{(9)} since 720=892+89+01720 = 8 \cdot 9^2 + 8 \cdot 9 + 0 \cdot 1.

You can read more about bases here.

Sample Cases

Case 1

Input

6 9

Output

1

Case 2

Input

38 11

Output

3

Case 3

Input

5 2

Output

3

Case 4

Input

5 10

Output

1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback