Number Tricks
InterviewTime limit1sMemory limit128 MB
For each query, count the trailing zeros of n! in base b by factoring b and finding the minimum over primes of each exponent's count in n!.
- Level
Medium5 of 10
- Topics
- Number theory, Math, Implementation, Brute force
- Solved
- No attempts yet
Problem
Dora loves impressing her friends with number tricks. One of her favorite tricks is to have a friend name a number ; Dora then tells them how many zeroes appear at the end of . Her friends are usually very impressed, because is such a large number. For example, when , even though is larger than the estimated number of atoms in the universe, Dora can instantly say that ends in exactly zeroes.
Some of Dora's newest friends, however, are aliens with more or fewer than ten fingers. Like every life form she has met so far, they count in a number system that has as many digits as they have fingers. Dora wants to impress them too, but she is not sure how to adapt her trick. Can you help her?
Given a base and a number , compute how many zeroes appear at the end of when it is written in base . For example, take and . Since , the answer is .
Input
The first line contains the number of scenarios. Each of the following lines describes one scenario and contains two integers and (, ), both given in base ten.
Output
For each scenario, first print a line Scenario #i:, where is the scenario number starting at . Then print a single line containing (in base ten), the number of trailing zeroes of when it is written in base . Separate consecutive scenarios with a blank line.