Roundabout Rounding
Time limit2sMemory limit2048 MB
Count integers x with 2 <= x <= N where rounding x once to the nearest power of 10 gives a different result than chain rounding through each smaller power first.
- Level
Medium6 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
Bessie the cow is back in school. She is doing math homework in which she rounds positive integers to powers of .
To round a positive integer to the nearest , where is a positive integer, Bessie first finds the -th digit from the right. Call this digit .
If , Bessie adds to .
Then Bessie sets the -th digit from the right and every digit to its right to .
For example, to round to the nearest (hundred), Bessie finds the 2nd digit from the right, which is . So . Since , Bessie adds to . Then she sets the 2nd digit and every digit to its right to , which gives .
Rounding to the nearest gives .
Elsie thinks she has invented a new type of rounding called chain rounding. To chain round to the nearest , Elsie first rounds to the nearest , then to the nearest , and so on, until she reaches the nearest .
Bessie thinks Elsie is wrong, but she is too busy with homework to check. You are asked to count the integers with for which rounding to the nearest gives a different result from chain rounding to the nearest , where is the smallest integer with . Here .
Input
The first line contains a single integer (), the number of test cases. Each of the next lines contains a single integer . All values in the same input file are distinct.
Output
Output lines. The -th line contains the number of integers at least and at most for which the two rounding methods give different results, for the -th test case.
Hint
Consider the second test case in the sample. is counted because chain rounding to the nearest gives (), but rounding to the nearest gives .
In the third test case, the two counted integers are and . chain rounds to instead of , and chain rounds to instead of . However, is not counted, since it chain rounds to , which is also what rounding to the nearest gives.