Kakezan
Time limit3sMemory limit512 MB
For each nonnegative integer, repeatedly cut its decimal representation at some point, replace it with the largest product of the two parts, and count steps until it becomes one digit, or report -1 if it never does.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math, Implementation, Brute force
- Solved
- No attempts yet
Problem
Taro is an elementary school student who has just learned multiplication. Somehow he has taken a liking to it, and whenever he sees a number he wants to multiply. Lately he seems to enjoy applying the following procedure to nonnegative integers.
- Step 1. If a nonnegative integer has one digit in decimal, the procedure ends. Otherwise, go to step 2.
- Step 2. When a integer is written in decimal, it can be split into two numbers by cutting between any two digits (for example, ). Among all such cuts, take the two resulting numbers, multiply them, and use the largest product as the next , then return to step 1. See "Notes on step 2" below for details.
Taro likes this procedure, but for large numbers he cannot predict how many times step 2 must be repeated, and he thinks it might even have to be repeated infinitely many times. So he asked you, his older brother in university, how many times step 2 must be performed for a nonnegative integer .
Your task is, given nonnegative integers N1..NQ, to determine for each integer how many times step 2 is performed before the procedure ends. If infinitely many repetitions are needed, output -1.
When splitting, cases where the result has a leading zero must also be considered. For example, when , computing , , and gives 24, 240, and 408 respectively, so 408 is chosen and becomes the next .
Input
Q
N1
N2
...
NQ
Qis the number of nonnegative integers given.Niis the i-th nonnegative integer Taro is curious about.
Output
Output Q integers separated by newlines.
R1
R2
..
RQ
Riis the number of times step 2 is executed forNibefore the procedure ends.- If step 2 must be executed infinitely many times for
Ni,Riis -1.
Constraints
1≤Q≤1000≤Ni≤106